GCM与登录系统系统、GCM

2023-09-12 06:38:26 作者:浪荡绅士

我目前正在实施GCM与登录系统的应用程序。我想要发送通知到基于该登录到应用程序(一个设备,多个用户)的用户应用程序。我经历这些过程。

I'm currently implementing GCM into an application with login system. I wanted to send notification to the application based on the user that logged in to the application (one device, multiple user). I go through these processes.

在登录为用户A 注册GCM(获取注册ID)发送到服务器端 广播通知了用户A 注销注销 在登录为用户B 注册GCM(获得注册ID -occasionally找回相同的注册ID与用户A,有时会返回不同的注册ID - ) GCM推送通知给用户A(即使用户未注册)

我不知道如何让应用程序识别登录到设备的用户,并推通知,只有特定的用户。相反,用户B的登录,并得到用户A的通知。任何意见和答案将是非常美联社preciated!如果您需要检查特定的codeS从我的项目,请让我知道。

I'm not sure how to let the application identify the user that logged in to the device and push notification to that specific user only. Instead of user B logged in and get user A's notification. Any comments and answers will be highly appreciated! If you need check on specific codes from my project, please do let me know.

推荐答案

注册ID识别特定设备上的具体应用。它在应用程序中对登录用户毫不知情。因此,当您注销GCM(当用户注销),你应该打电话给你的服务器,让它知道该用户已注销。

The registration ID identifies a specific application on a specific device. It has no knowledge on log-in of users within your application. Therefore, when you un-register GCM (when user logs out), you should call your server to let it know that the user logged out.

这将让您的服务器知道这个用户登录了,服务器将停止发送GCM消息吧。

This will let your server know that this user is logged out, and the server will stop sending GCM messages to it.

如果你得到了相同的注册ID后用户A注销和用户B日志中(即使你得到一个新的注册ID,旧的可能仍然工作没关系。这就是为什么GCM返回一个规范的注册ID当该装置具有用于应用程序多于一个注册ID)

It doesn't matter if you get the same registration ID after user A logs out and user B logs in (even if you get a new registration ID, the old one may still work. That's why GCM returns a canonical registration ID when the device has more than one registration ID for the application).

编辑:

让我们考虑的特殊情况下(这应该是比较少见的),其中用户A注销后,您的服务器发送过一个通知,但通知得到由谷歌只传递到用户B登录后​​,你的应用程序。处理这个最安全的方式情况是接收通知您的应用程序并将其丢弃,深藏不露用户B.为了知道什么时候丢弃收到​​的通知中,您可以添加一个用户属性设置为您通知数据的用户名作为它的值。当您处理的应用程序的通知,检查用户属性相匹配的登录用户显示的通知之前。

Lets consider the special scenario (which should be relatively rare) where user A logs out after your server sent it a notification but the notification gets delivered by Google to your application only after user B logs in. The safest way to handle this case is to receive the notification in your application and discard it, showing nothing to user B. In order to know when to discard a received notification you can add a user property to your notification data with the user name as its value. When you handle the notification in the app, check that the user property matches the logged in user before displaying the notification.