iOS 和 Google Cloud Messaging:在一系列通知中缺少推送通知

Posted

技术标签:

【中文标题】iOS 和 Google Cloud Messaging:在一系列通知中缺少推送通知【英文标题】:iOS and Google Cloud Messaging: Missing push notifications in series of notifications 【发布时间】:2015-11-08 07:39:42 【问题描述】:

我正在使用 Google Cloud Messaging 在 ios 应用中接收推送通知。为了了解系统的可靠性,我进行了两次测试。 1.在应用程序处于前台时接收通知 下面称为 curl 1 2.在应用程序处于后台时接收通知。 iOS 主屏幕可见。然后设备被锁定。 下面称为 curl 2, 3, 4

对于这两个测试,我使用终端程序连续向 GCM 发送 10 个推送通知。

#!/bin/bash
api_key="MYAPYKEYXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
reg_id="MYREGIDXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

for i in `seq 1 10`;
        do
            echo "Sending message $i"
            curl    --header "Authorization: key=$api_key"  --header Content-Type:"application/json"    https://android.googleapis.com/gcm/send  -d "\"registration_ids\":[\"$reg_id\"], \"data\":\"message\": \"This is $i\", \"content_available\":true"
            echo " "
        done

我的 AppDelegate.swift 包含以下代码:对于这个测试,我只是将收到的通知记录到控制台。

func application( application: UIApplication,
        didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) 
            self.log.debug("\(userInfo["gcm.message_id"]): type: \(userInfo) ")

        // This works only if the app started the GCM service
        GCMService.sharedInstance().appDidReceiveMessage(userInfo);
        // Handle the received message
        // [START_EXCLUDE]
        NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
            userInfo: userInfo)
        // [END_EXCLUDE]

func application( application: UIApplication,
    didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
    fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) 
        self.log.debug("\(userInfo["gcm.message_id"]): type: \(userInfo) ")

        // This works only if the app started the GCM service
        GCMService.sharedInstance().appDidReceiveMessage(userInfo);
        // Handle the received message
        // Invoke the completion handler passing the appropriate UIBackgroundFetchResult value
        // [START_EXCLUDE]
        NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
            userInfo: userInfo)
        // [END_EXCLUDE]

        handler(UIBackgroundFetchResult.NewData)


    // [END ack_message_reception]

curl 1 : 在应用处于活动状态时运行

$ sh gcm-push-curl.sh 
Sending message 1
"multicast_id":7601092806058312069,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966587095558%d9588296d9588296"] 
Sending message 2
"multicast_id":5909902520187109640,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966587866963%d9588296d9588296"] 
Sending message 3
"multicast_id":4614499459908171704,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966589361277%d9588296d9588296"] 
Sending message 4
"multicast_id":8134668915396219272,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966590641578%d9588296d9588296"] 
Sending message 5
"multicast_id":4784965394490169762,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966591587472%d9588296d9588296"] 
Sending message 6
"multicast_id":6241530976591223030,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966594764987%d9588296d9588296"] 
Sending message 7
"multicast_id":8978902798439854756,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966595671922%d9588296d9588296"] 
Sending message 8
"multicast_id":6283825418645144982,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966596474541%d9588296d9588296"] 
Sending message 9
"multicast_id":8120719633509436218,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966597242071%d9588296d9588296"] 
Sending message 10
"multicast_id":8512105467086003642,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966597991847%d9588296d9588296"] 

curl 2:在应用处于后台且设备被锁定时运行

$sh gcm-push-curl.sh 
Sending message 1
"multicast_id":5588705638430973508,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966661606755%d9588296d9588296"] 
Sending message 2
"multicast_id":9016701196490961038,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966662368880%d9588296d9588296"] 
Sending message 3
"multicast_id":8811760824655650118,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966664079778%d9588296d9588296"] 
Sending message 4
"multicast_id":5409935873727765078,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966665050940%d9588296d9588296"] 
Sending message 5
"multicast_id":7729732231428218287,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966666508932%d9588296d9588296"] 
Sending message 6
"multicast_id":8188050074826382607,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966667585382%d9588296d9588296"] 
Sending message 7
"multicast_id":5913793804137018252,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966671009307%d9588296d9588296"] 
Sending message 8
"multicast_id":5601975420774004308,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966672984512%d9588296d9588296"] 
Sending message 9
"multicast_id":8457313732470913239,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966673716583%d9588296d9588296"] 
Sending message 10
"multicast_id":5371949804462995150,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966675441327%d9588296d9588296"] 

curl 3:应用在后台运行且设备再次锁定

$ sh gcm-push-curl.sh 
Sending message 1
"multicast_id":5557012639197983768,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966719944546%d9588296d9588296"] 
Sending message 2
"multicast_id":4730409051405748208,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966724620582%d9588296d9588296"] 
Sending message 3
"multicast_id":5530580902639958686,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966725438291%d9588296d9588296"] 
Sending message 4
"multicast_id":9111474059722223027,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966727055882%d9588296d9588296"] 
Sending message 5
"multicast_id":7841377277124478181,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966728970341%d9588296d9588296"] 
Sending message 6
"multicast_id":5594543271725809615,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966730800067%d9588296d9588296"] 
Sending message 7
"multicast_id":8685309308966690464,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966731640101%d9588296d9588296"] 
Sending message 8
"multicast_id":8859997925201491765,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966735618351%d9588296d9588296"] 
Sending message 9
"multicast_id":8830947759563422702,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966737433853%d9588296d9588296"] 
Sending message 10
"multicast_id":5660703487823801785,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446966738664391%d9588296d9588296"] 

curl 4:应用在后台运行,设备仍然再次锁定

$sh gcm-push-curl.sh 
Sending message 1
"multicast_id":4866730914828951532,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446967658074261%d9588296d9588296"] 
Sending message 2
"multicast_id":8385741491167809594,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446967658837166%d9588296d9588296"] 
Sending message 3
"multicast_id":7340882174172500805,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446967660847155%d9588296d9588296"] 
Sending message 4
"multicast_id":8822097918345564456,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446967661759583%d9588296d9588296"] 
Sending message 5
"multicast_id":9155633918940805212,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446967662891127%d9588296d9588296"] 
Sending message 6
"multicast_id":8374566084159111686,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446967663658639%d9588296d9588296"] 
Sending message 7
"multicast_id":5062396269674637650,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446967664476061%d9588296d9588296"] 
Sending message 8
"multicast_id":5632447549491506026,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446967665191507%d9588296d9588296"] 
Sending message 9
"multicast_id":5582459520681321352,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446967665936985%d9588296d9588296"] 
Sending message 10
"multicast_id":7328958609399996860,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1446967668825005%d9588296d9588296"] 

结果:

卷曲 1 后

2015-11-08 08:09:47.718 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446966587095558%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 1, gcm.message_id: 0:1446966587095558%d9588296d9588296] 
2015-11-08 08:09:48.434 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446966587866963%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 2, gcm.message_id: 0:1446966587866963%d9588296d9588296] 
2015-11-08 08:09:49.973 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446966589361277%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 3, gcm.message_id: 0:1446966589361277%d9588296d9588296] 
2015-11-08 08:09:51.401 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446966590641578%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 4, gcm.message_id: 0:1446966590641578%d9588296d9588296] 
2015-11-08 08:09:52.222 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446966591587472%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 5, gcm.message_id: 0:1446966591587472%d9588296d9588296] 
2015-11-08 08:09:55.397 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446966594764987%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 6, gcm.message_id: 0:1446966594764987%d9588296d9588296] 
2015-11-08 08:09:56.317 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446966595671922%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 7, gcm.message_id: 0:1446966595671922%d9588296d9588296] 
2015-11-08 08:09:57.036 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446966596474541%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 8, gcm.message_id: 0:1446966596474541%d9588296d9588296] 
2015-11-08 08:09:57.855 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446966597242071%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 9, gcm.message_id: 0:1446966597242071%d9588296d9588296] 
2015-11-08 08:09:58.569 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446966597991847%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 10, gcm.message_id: 0:1446966597991847%d9588296d9588296] 

卷曲 2 后

2015-11-08 08:11:05.404 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446966661606755%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 1, gcm.message_id: 0:1446966661606755%d9588296d9588296] 
2015-11-08 08:11:07.286 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446966662368880%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 2, gcm.message_id: 0:1446966662368880%d9588296d9588296] 
2015-11-08 08:11:10.109 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446966664079778%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 3, gcm.message_id: 0:1446966664079778%d9588296d9588296] 

卷曲 3 后

2015-11-08 08:12:16.960 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446966730800067%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 6, gcm.message_id: 0:1446966730800067%d9588296d9588296] 
2015-11-08 08:12:18.698 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446966731640101%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 7, gcm.message_id: 0:1446966731640101%d9588296d9588296] 
2015-11-08 08:12:22.159 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446966735618351%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 8, gcm.message_id: 0:1446966735618351%d9588296d9588296] 
    ### 30 seconds later...
2015-11-08 08:20:57.287 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446966738664391%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 10, gcm.message_id: 0:1446966738664391%d9588296d9588296] 

卷曲 4 后

2015-11-08 08:27:41.779 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446967658074261%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 1, gcm.message_id: 0:1446967658074261%d9588296d9588296] 
2015-11-08 08:27:43.798 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446967658837166%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 2, gcm.message_id: 0:1446967658837166%d9588296d9588296] 
2015-11-08 08:27:49.739 [Debug] [main] [AppDelegate.swift:206] application(_:didReceiveRemoteNotification:fetchCompletionHandler:) > Optional(0:1446967662891127%d9588296d9588296): type: [aps: 
    "content-available" = 1;
, message: This is 5, gcm.message_id: 0:1446967662891127%d9588296d9588296]

如您所见,当设备锁定时,并非所有通知都会收到。但是有些,有些延迟(卷曲 3 中的第 10 位)。

我不知道我能做些什么,因为通常会收到通知。

我希望你们中有人有任何想法。

最好的西蒙

Edit1:我查看了一次日志,发现从未调用过 func application( application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])

【问题讨论】:

【参考方案1】:

当应用程序不在前台时,GCM 使用 APNs 传递消息。通知传递是not guaranteed by APNs.

重要提示:发送通知是“尽力而为”,而不是 保证。它并非旨在向您的应用程序提供数据,仅用于 通知用户有新数据可用。

我认为,当应用程序不是您在测试中所做的前台时,通过 APNs 发送内容可用的背靠背消息会导致消息丢失。也许延迟更长的测试会导致丢失的消息更少。

如果您的消息具有高优先级,则将消息的setting the priority value 视为高优先级,但前提是该消息需要用户交互,如聊天消息。

【讨论】:

以上是关于iOS 和 Google Cloud Messaging:在一系列通知中缺少推送通知的主要内容,如果未能解决你的问题,请参考以下文章

适用于 iOS 的 Google Cloud Storage [关闭]

Google Cloud Functions 通知 iOS

Google Cloud Messaging:iOS App 在后台时不会收到警报

Google Cloud Datastore 客户端库创建实体

ios Google Cloud Messaging (GCM) 未收到远程通知

如何使用 Google Cloud Messaging 将通知推送到 iOS