Push Notification code execution (forced-quit app)

2019-06-19 19:38发布

问题:

I was wondering how applications like Whatsapp were able to give a delivery receipt (double green check) to the sender of the message.

I have seen that even if you force-quit Whatsapp (using the app task switcher and swiping the app away), the sender still gets the delivery receipt (double green check) just at the moment the push notification is received on the phone. Clearly they are able to execute code (make a request to a backend, informing the delivery) when receiving the push notification.

Since iOS7 one can send a push notification payload with "content-available":1, this enables the receiver of the notification to execute user code, so, firstly I thought they were using this feature. However, if the user forced-quit the app then the user code is not executed when receiving the notification. Because of this I'm not able to mimic Whatsapp behavior.

I have enabled Capabilities>Background Modes and checked Remote notifications.

I'm handling the notification with this method:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

I'm sending the notification with this payload:

{
    "aps":{
            "alert":"Hello world",
            "sound":"MySound.wav",
            "content-available":1
    }
}

I have already checked:

  • Will iOS launch my app into the background if it was force-quit by the user?
  • WWDC Video Whats New With Multitasking (#204 from WWDC 2013)

I also read about PushKit (though I didn't try it), that maybe could help here, but my understanding is that the app would need to be a VOIP app. Clearly I don't want to require VOIP on my app to just execute code when receiving a push notification.

Thanks.

回答1:

You just answered your question in your description.

  1. There is no way to wake the app through an regular push notification, if it was force-quit.

    See App Programming Guide for iOS:

    In most cases, the system does not relaunch apps after they are force quit by the user. One exception is location apps, which in iOS 8 and later are relaunched after being force quit by the user. In other cases, though, the user must launch the app explicitly or reboot the device before the app can be launched automatically into the background by the system.

  2. VoIP push notifications can wake the app, even if it was force-quit.

    See Voice Over IP (VoIP) Best Practices:

    Your app is automatically relaunched if it’s not running when a VoIP push is received.

  3. Two blue marks in WhatsApp mean, the recipient read the message. If you get two blue marks, just after the push notification was received, this is a design fault or bug in WhatsApp, since you can't tell, if the user actually read the notification. If you get two grey marks, that just means, the message was delivered to the device. Again, if you get two grey marks, just after the push notification was received and the app was force-quit, either WhatsApp is (mis)using VoIP push for this, or they just assume the push was delivered or will be delivered.