2
votes

How do I sync push notifications across multiple iOS devices?

The scenario is: The user has an app installed on two devices. Both devices receive a remote notification. The user opens the app on one device. The push notification on the other device should disappear.

This is done by many apps, e.g. Facebook Messenger, but how is this implemented?

(Swift)

2

2 Answers

2
votes

I would think the following should work (with the usual caveats):

  1. Send a silent push notification (content-available set, no alert/sound/badge in aps, but the details of the notification for use in 2.)
  2. Create a local notification with the data from 1, present it immediately, and keep a reference to it
  3. When the user acts on the local notification, send a request to the server to let it know
  4. Server sends another silent notification to all other devices for the same user, letting them know to remove the notification
  5. On receipt, cancel the local notification created in 2.
5
votes

Here is one way I have implemented exactly that:

  • upon receipt of a push notification (it has to be of the kind that has the 'content-available' flag, so that it is delivered to you app, even if the user does not tap on it), the app should make a call to your server, notifying the server it is received. Note that having that flag means your app might get the pushnotificatins twice: once when it is received by the system, and a second time when the user taps it. Or it might not come in at all, but thats another discussion.
  • in response to this call, the server should send a silent push notification (with optionally a custom payload) to the other devices registered for this account
  • Upon receipt of this push notification, the other clients set the application-banner count to zero (this will clear out the push notifications from the notification center. This is not officially documented, so it might stop working in a future version of iOS).