Google/Apple Push Notification Service (APNS/GCM)

2019-01-22 08:37发布

问题:

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?

回答1:

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.



回答2:

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