iOS push notifications behavior when app is delete

2019-02-01 04:35发布

问题:

I've encountered an unusual scenario where a user is continuing to receive notifications when my app has been deleted and then reinstalled. The scenario is as follows:

  • user installs the app from the appstore
  • user logs onto our app and we register them for notifications
  • user deletes the app from their device
  • user reinstalls the app from the appstore
  • user receives a notification even though they have not started the app up yet, logged in, etc

My understanding of the APNs architecture was that once your app is deleted from your device, it is de-registered from APNs by the OS itself. A reinstall of the app would not re-enable notifications until you called "registerForRemoteNotificationTypes" -- so if a note was sent to the device using the old token, APNs would not deliver it since the above method has not been called.

Is that not accurate?

回答1:

The token remains the same. It's even the same for all applications on the device. It only changes when you change the OS version. However, that still doesn't explain the strange behavior you encountered.

I believe that only after Apple identifies that the application has been uninstalled from the device (which usually happens when you send a notification to the app on a specific device after the device uninstalled it) it un-registers the app. If no notifications were sent to the app on the device that uninstalled the app between the time of uninstall and the time of the new installation, Apple don't know the device uninstalled the app, and therefore it is still registered for APNS.

The behavior of the feedback service supports my theory - if you uninstall the app and call the feedback service, you won't get the device token of the device that uninstalled the app. Only after you try to send a notification to that device, Apple will detect the uninstall and return that device token in the feedback service.

One last thing I should add - if the uninstalled app is the last one on the device that was registered for push notifications, the APN service will never know that the app was uninstalled, and therefore it will still be registered for APN after being re-installed.



回答2:

When you delete the app, the OS doesn't "de-register" it from APNS. You have to send a notification to a device that has deleted the app in order to "de-register" it. This may take a few tries from APNS until it figures out the app has been deleted.