GCM(Google 云消息传递)将通知从 python 应用服务器推送到 iOS 应用

GCM (Google Cloud Messaging) push notification from python app server to iOS app

我正在尝试使用 GCM 向我的 iOS 应用程序发送推送通知。我尝试为我的应用程序遵循 (Google GCM) and (GCMServerDemo),但我只能收到打印在 xcode 输出上的通知:[body: hello, it's me, sound: default, collapse_key: do_not_collapse, badge: 2, from: 629354528047] 但在我的测试 phone 中没有弹出任何消息。我的服务器 运行 在 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]) 我猜是在解析接收到的消息时出现了问题。任何人都可以帮我解决它吗?谢谢。

如果您只想向用户显示一条消息,那么 gcm 负载应该是这样的:

{
  "to":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
  "notification":{
    "body":"This text message will be seen by the user",
    "badge": 2,
    "sound": "default"
  }
}

只有当应用程序不在前台时,它才会默认可见。如果是,您需要阅读 AppDelegatedidReceiveRemoteNotification 方法中的消息,并自己将其显示给用户。

编辑:

您可能想试试这个,而不是使用 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应该在数据键之外