首页 文章

如果将“数据”(但“通知”工作)有效负载发送到iOS中的GCM / FCM,则不会收到推送通知didReceiveRemoteNotification

提问于
浏览
2

我正在尝试为我们的iOS应用程序收到“数据”有效负载通知 .

今天我们可以按照以下方式发送GCM notification 推送通知:

https://developers.google.com/cloud-messaging/concept-options

(FCM有相同的文字)

一个简单的测试是使用CURL:

curl -X POST \
  https://gcm-http.googleapis.com/gcm/send \
  -H 'authorization: key=##_GCM_SERVER_ID_##' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'postman-token: ##_POSTMAN_TOKEN_##' \
  -d '{
    "notification": {
        "body": "Test body"
    },
    "to" : "##_DEVICE_TOKEN_##"
}
'

这将成功触发iOS AppDelegate.didReceiveRemoteNotification:fetchCompletionHandler 功能 .

但是,如果将其更改为 data 通知:

curl -X POST \
  https://gcm-http.googleapis.com/gcm/send \
  -H 'authorization: key=##_GCM_SERVER_ID_##' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'postman-token: ##_POSTMAN_TOKEN_##' \
  -d '{
    "data": {
        "body": "Test body"
    },
    "to" : "##_DEVICE_TOKEN_##"
}
'

即使应用程序处于后台/前台,我也看不到从GCM(在 didReceiveRemoteNotification 函数中)发送到应用程序的任何内容 .

即使它在文档中说它应该:

https://developers.google.com/cloud-messaging/concept-options#notifications_and_data_messages

请注意这些进一步特定于平台的详细信息:在Android上,可以在用于启动活动的Intent中检索数据有效负载 . 在iOS上,数据有效负载将在didReceiveRemoteNotification中找到:

GCM可以处理到APN网络的纯 data 推送通知吗?

notification 相比,我是否需要做一些特殊的接收 data ,iOS中的推送通知?

2 回答

  • 0

    将带有FCM的数据类型消息发送到iOS设备时,只有在FCM请求正文中 content_available 设置为 true 时才会收到它们,例如:

    {
        "to": "--fcm-token--",
        "content_available": true,
        "data": {
            "priority": "high",
            "hello": "world"
        }
    }
    
  • 2

    除了您分享的笔记外,请不要错过,

    在iOS上,GCM存储消息并仅在应用程序位于前台并 Build GCM连接时传递消息 .

    有了这个,你可能想检查Establishing a Connection . 然后,当您 Build XMPP连接时,CCS和您的服务器使用普通的XMPP <message> 节来回发送JSON编码的消息 . <message> 的正文必须是:

    <gcm xmlns:google:mobile:data>
        JSON payload
    </gcm>
    

    另请注意, message_id 是数据消息的必填字段 . 检查带有有效负载的消息的此样本请求格式 - 数据消息显示在Downstream Messages中 . 你只需要使用CURL转换它 .

    <message id="">
      <gcm xmlns="google:mobile:data">
      {
          "to":"REGISTRATION_ID",  // "to" replaces "registration_ids"
          "message_id":"m-1366082849205" // new required field
          "data":
          {
              "hello":"world",
          }
          "time_to_live":"600",
          "delay_while_idle": true/false,
          "delivery_receipt_requested": true/false
      }
      </gcm>
    </message>
    

    有关更多信息,请参阅XMPP Connection Server Reference .

相关问题