Use Broadcast Channels and forget about device tokens.
Published 2014-09-23 by Stefan Natchev

Keeping track of users' device tokens can be a real hassle. Given the right use-case, you may not need to.
What would it take to reduce that added complexity? Let's see if we can solve it using broadcast channels.

Let's assume we have a chat app where users log in with their email address. Whenever a user messages another user, we want to deliver a push notification to the recipient.

Here are some of the requirements we will need to account for:

  1. Users can login to our app from multiple devices (iPad, iPhone, etc.) and should receive notifications to all of them.
  2. When a user logs out from the app, they should stop receiving notifications on that device.
  3. When the user deletes the app from their phone, they should stop receiving notifications on that device.
  4. When a user disables notifications for your app, they should stop receiving notifications.

All of these requirements can be implemented by subscribing the users to a broadcast channel
that matches the email address of the user.

Client Side

Let's start with the iOS app.

//Assume we have some callback after a user logs in
- (void)userDidLoginSuccessfully
{
  [[ZeroPush shared] registerForRemoteNotifications];
}

//in our AppDelegate.m
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)tokenData
{
  [[ZeroPush shared] registerDeviceToken:tokenData channel:self.currentUser.email];
}

- (void)userDidLogOut:(User *)user
{
  [[ZeroPush shared] unsubscribeFromChannel:user.email];
}

Let's take a closer look at each part that example.

- (void)userDidLoginSuccessfully
{
  [[ZeroPush shared] registerForRemoteNotifications];
}

In our hypothetical app, a delegate gets called as soon as the user logs in. At this point we
can ask them if they want to receive push notifications by calling registerForRemoteNotifications.

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)tokenData
{
  [[ZeroPush shared] registerDeviceToken:tokenData channel:self.currentUser.email];
}

If the user accepts to receive push notifications, Apple will call us back with application:didRegisterForRemoteNotificationsWithDeviceToken
is called. Here we are given the tokenData from Apple which we send to ZeroPush along with the user's
email address as the channel. This will associate the two identifiers.

- (void)userDidLogOut:(User *)user
{
  [[ZeroPush shared] unsubscribeFromChannel:user.email];
}

Finally, remember to unsubscribe the user from the channel whenever they log out of the app or switch users.

Server side

In your web app, whenever you want to notify the user, make the HTTP request to the intended channel:

$ curl https://api.zeropush.com/broadcast \
    -d channel=recipient@address.com      \
    -d info[from]=sender@address.com      \
    -d alert=Hello!

This will broadcast to all devices that are logged in as recipient@address.com.

Using ZeroPush to keep track of the device tokens frees the web-app from having to keep track of:

  1. A user adding or removing a device
  2. Monitoring Apple's feedback service
  3. Delivering to multiple devices

For more information about broadcast, check out our API Documentation