带登录系统的 GCM [英] GCM with login system

查看:17
本文介绍了带登录系统的 GCM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将 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.

  1. 以用户 A"身份登录
  2. 注册 GCM(获取注册 ID)发送到服务器端
  3. 向用户 A 广播通知
  4. 退出注销
  5. 以用户 B"身份登录
  6. 注册GCM(获取注册ID - 偶尔获取与用户A相同的注册ID,有时返回不同的注册ID-)
  7. GCM 向用户 A 推送通知(即使用户取消注册)

我不确定如何让应用程序识别登录到设备的用户并仅向该特定用户推送通知.而不是用户 B 登录并获得用户 A 的通知.任何评论和答案将不胜感激!如果您需要检查我项目中的特定代码,请告诉我.

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.

用户A注销后和用户B登录后得到相同的注册ID没有关系(即使你得到一个新的注册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 登录后,Google 才会将通知发送到您的应用程序.处理此问题的最安全方法情况是在您的应用程序中接收通知并将其丢弃,不向用户 B 显示任何内容.为了知道何时丢弃收到的通知,您可以使用用户名将 user 属性添加到您的通知数据中作为它的价值.在应用中处理通知时,请在显示通知之前检查用户属性是否与登录用户匹配.

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.

这篇关于带登录系统的 GCM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆