Android 谷歌/苹果推送通知服务 (APNS/GCM)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20685560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:17:26  来源:igfitidea点击:

Google/Apple Push Notification Service (APNS/GCM)

androidiospush-notificationapple-push-notificationsgoogle-cloud-messaging

提问by Dmitry R

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.

我正在尝试为 Android 和 iOS 创建应用程序,我想在这两个应用程序上使用推送通知。我将有一个服务器应用程序来发送通知。

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.

我想弄清楚的是我们如何存储用户的设备,以便我知道需要使用 APNS 或 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.

其中一个方向是获取应用设置的手机类型,并将这些信息存储在服务器端,但是如果用户将手机从iOS换成Android会发生什么,需要涉及用户的数据存储和收集信息对于每个用户,不包括用户拥有 Android 平板电脑和 iOS 手机的场景。

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

使它更通用并同时向服务 APNS 和 GCM 发送通知,其中之一会返回错误?

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

很想听听这种情况下的最佳实践是什么?

回答by Devel

It is very simple to implement APNS and GCM:

实现APNS和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.
  • 当 APNS(iOS 设备)和 GCM(Android 设备)在 Apple 和 Google 服务器上注册推送通知时,它会为每个设备生成一个唯一的令牌。
  • 之后,您需要保存该设备令牌,以及您的设备 ID 或用户 ID(设备服务器上的唯一 ID)以及设备的操作系统。

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

就像 iOS 设备在您的服务器(后端)上发送此信息一样,您可以使用此 JSON 格式- {"token":"abcdedfgehik2bd3d3ff3sffssdff","os":"iOS","userid":34}

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

对于安卓设备,它将是 - {"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.

通过这种方式,您可以识别设备的操作系统,以及用于发送推送通知的用户信息和唯一令牌。

回答by tyczj

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

当设备向推送服务注册时,您可以创建一个Unique id (UUID),然后将唯一 ID 连同注册密钥和您想要的任何其他信息一起发送到服务器。这样你就拥有了用户拥有的所有设备并向所有设备发送通知