在 Python 中格式化 APNS 样式的 JSON 消息以用于 Amazon SNS [英] Format APNS-style JSON message in Python for use with Amazon SNS

查看:21
本文介绍了在 Python 中格式化 APNS 样式的 JSON 消息以用于 Amazon SNS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 iOS 应用,对于我们的推送通知,我们使用了亚马逊的简单通知服务 (SNS).

I'm creating an iOS app, and for our push notifications, we're using Amazon's Simple Notification Service (SNS).

SNS 很棒,但文档很少.我正在使用 boto,亚马逊的 Python 库,并且我已经想出了如何发送纯文本- 文本推送通知:

SNS is wonderful, but the documentation is pretty sparse. I'm using boto, Amazon's Python library, and I've figured out how to send plain-text push notifications:

device_arn = 'MY ENDPOINT ARN GOES HERE'
plain_text_message = 'a plaintext message'
sns.publish(message=plain_text_message,target_arn=device_arn)

然而,文档中没有明确说明如何创建 Apple 推送通知服务 (APNS) 消息.我需要在推送通知的同时发送声音和徽章,但不知道如何格式化消息的 JSON.

However, what's not clear from the documentation is how to create an an Apple Push Notification Service (APNS) message. I need to send a sound and a badge along with the push notification, but can't figure out how to format the JSON for the message.

这是我目前最好的猜测:

Here's my best guess so far:

message = {'default':'default message', 'message':{'APNS_SANDBOX':{'aps':{'alert':'inner message','sound':'mySound.caf'}}}}
messageJSON = json.dumps(message,ensure_ascii=False)
sns.publish(message=messageJSON,target_arn=device_arn,message_structure='json')

但是,当我运行此代码时,我在通知中看到的只是默认消息"——这意味着 Amazon SNS 拒绝了我的消息格式,而是显示了 default.

When I run this code, though, all I see on the notification is "default message" - which means that Amazon SNS rejected my message's format, and displayed the default instead.

如何正确格式化此 JSON?

推荐答案

我想通了!事实证明,APNS 有效载荷必须在较大的有效载荷中编码为字符串 - 它完全有效.

I figured it out! Turns out, the APNS payload has to be encoded as a string within the larger payload - and it totally works.

这是最终的工作代码:

apns_dict = {'aps':{'alert':'inner message','sound':'mySound.caf'}}
apns_string = json.dumps(apns_dict,ensure_ascii=False)
message = {'default':'default message','APNS_SANDBOX':apns_string}
messageJSON = json.dumps(message,ensure_ascii=False)
sns.publish(message=messageJSON,target_arn=device_arn,message_structure='json')

以下是这段代码中发生的事情的演练:

首先,为APNS创建python字典:

First, create the python dictionary for APNS:

apns_dict = {'aps':{'alert':'inner message','sound':'mySound.caf'}}

其次,取出字典,将其转换为 JSON 格式的字符串:

Second, take that dictionary, and turn it into a JSON-formatted string:

apns_string = json.dumps(apns_dict,ensure_ascii=False)

第三,将该字符串放入更大的负载中:

Third, put that string into the larger payload:

message = {'default':'default message','APNS_SANDBOX':apns_string}

接下来,我们将那个编码成它自己的 JSON 格式的字符串:

Next, we encode that in its own JSON-formatted string:

messageJSON = json.dumps(message,ensure_ascii=False)

然后可以使用 boto 发布结果字符串:

The resulting string can then be published using boto:

sns.publish(message=messageJSON,target_arn=device_arn,message_structure='json')

这篇关于在 Python 中格式化 APNS 样式的 JSON 消息以用于 Amazon SNS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆