18
votes

I'm trying to create application for Android and iOS and I want to use push notification on both the application. I'm going to have a server app that will be sending the notification.

What I'm trying to figure out is how we can store the device of the user so I'll know which service need to be used APNS or GCM.

One of the directions is to get the phone type to be set by the app and store this information on the server side, but what happens if the user changes his phone from iOS to Android, need to involve data storage of the user and collect information for every user, not covers scenario when user has Android tablet and iOS phone.

Make it more generic and dispatch the notification to both services APNS and GCM at the same time, one of them will return error?

Would love to hear what is the best practice for such scenarios?

2
You do have device tokens stored somewhere? We store device-tokens and device-types (and app-versions running on devices...) in dedicated table. Each user can thus have as many devices registered as one wants. Notification-worker-threads then know which process & server to use while sending a message to any device. This (token,type,version) is sent to server each time user logs in.Rok Jarc
Any reason why you wouldn't use www.parse.com to manage your pushes? It can do a lot of pushes each month for free and it handles all of this for you.Stephen Johnson
you can test using pushtry.comArvind

2 Answers

29
votes

It is very simple to implement APNS and GCM:

  • When APNS (iOS Devices) and GCM (Android Device) registers for Push Notification on Apple and Google Server it generates a unique token for every device.
  • After that, you need to save that device token, with your device id or user id (unique id on your server for device) and the OS of device.

Like and iOS device is sending this information on your server (backend) you can use this JSON format- {"token":"abcdedfgehik2bd3d3ff3sffssdff","os":"iOS","userid":34}

For android device it will be - {"token":"erydnfbdbdjskd76ndjs7nnshdjs","os":"Android","userid":35}

By this you can identify the OS of device, as well as user information and unique token which will be used for sending push notification.

3
votes

when the device registers with the push service you could create a Unique id (UUID) and then send the unique id to the server along with the registration key and any other information you want. That way you have all the devices the use has and dispatch notifications to all of them