我正在尝试使用GCM向我的iOS应用发送推送通知。我尝试关注(Google GCM)和(GCMServerDemo)我的应用,但我只能收到打印在xcode输出上的通知:[body: hello, it's me, sound: default, collapse_key: do_not_collapse, badge: 2, from: 629354528047]
但我的测试手机上没有任何内容。我的服务器在python上运行,我把它实现为:
from gcm import *
gcm= GCM("123...")
DEV_TOKEN = "l0NOTncXJXQ:APA91bGPVHxvF-PCL-PPNic6zhfnpv0aAe5KhvoYOOF_HfLZlCAquMGQb196J5_4zUEzWEirSOWP86d-n4-DJws4nPs5ZXR1c9UOQOPPuuCAjXFz2VIZ-5_SRz8G6D_MzKHv1W7yRrmZ"
reg_ids = [DEV_TOKEN]
notification = {"body": "hello, it's me", "sound": "default", "badge": 2}
response=gcm.json_request(registration_ids=reg_ids, data=notification)
print(response)
我的AppDelegate在我的应用客户端上检测到通知:http://swiftstub.com/621889661/
我的ViewController:http://swiftstub.com/89730359
我希望我的客户端应用程序在应用程序处于活动状态和屏幕关闭时都会收到通知(但是现在我只能在xcode输出屏幕上收到我的服务器上发送的消息,如[body: hello, it's me, sound: default, collapse_key: do_not_collapse, badge: 2, from: 629354528047]
)
我猜解析收到的消息时出现问题。任何人都可以帮我修复它吗?谢谢。
答案 0 :(得分:1)
如果您只想向用户显示消息,那么这就是gcm有效负载的样子:
{
"to":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"body":"This text message will be seen by the user",
"badge": 2,
"sound": "default"
}
}
只有当应用程序不在前台时,它才会默认显示。如果是,您需要使用AppDelegate
didReceiveRemoteNotification
方法阅读邮件,并自行向用户展示。
编辑:
您可能希望尝试使用gcm.plaintext_request(registration_id=reg_id,data = data)
,而不是使用notification = {'body': "hello, it's me", "sound": "default", "badge": 2}
DEV_TOKEN = '**********************************'
reg_ids = [DEV_TOKEN]
response = gcm.json_request(registration_ids=reg_ids, notification=notification)
:
notification
$('#team option[value="' + teamlist[i].team_name + '"]').length
应该在数据密钥之外