GCM,注册ID和处理用户登录/注销 [英] GCM , Registration ID and handling user login/logout

查看:821
本文介绍了GCM,注册ID和处理用户登录/注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序,允许用户登录与服务器发出的证书,可以说简单的登录密码和USENAME。
因此,用户启动应用程序,输入用户名和密码,服务器authentificates用户,保存这些信息。 (详见真的不玩在这种情况下,任何角色)。
我需要实现?
还有一些事件,当我的服务器需要通知用户。让我们考虑订单状态改变。如果是这样,我需要告知用户这一点,与通知中的状态栏。
我已经决定使用GCM完成这个任务。
我了解这一点,并得到了一些问题。

  1. 当用户获得注册ID从GCM服务的Andr​​oid,它是恒定的,或者是可以改变的。据我了解有没有保证,这是永远不变的。那么,如何处理这个ID的刷新。 或者,如果我不叫 GoogleCloudMessaging.getInstance(的applicationContext).register 直到我注册新的,甚至如果我拒收讯息登记册的方法它可以返回相同的ID将保持不变。我可以保存这个ID共享preferences例如使用了很长的时间?或者,它可以在一段时间后失效?

  2. 主要的问题是,我需要处理多个用户在同一台​​设备上。据我所知,通过GCM机构出具的注册ID它绑在设备和应用程序。因此,如果新用户注销,并登录使用新的凭证(新用户帐户),我该如何处理呢?我可以例如在我的数据库中存储此ID在服务器上,并使用它,直到用户注销,从里面我的应用程序的账号,之后只是从数据库中删除它,如果用户在此设备上与另一个帐户注册ID登录可以用于通知其他用户?将它的工作?还是我错过了一些东西。

  3. 如何处理用户多个设备。我的群notifaction关键是从用户帐户一些独特的数据(电子邮件,用户名..)。如果我理解正确的话,我需要登记身份证上的所有设备,其中用户登录其我的服务器帐户,并映射这一个通知的关键。我应该处理通知由我解雇,比上游发送消息到GCM服务器,以便通知该通知已被解聘以外的用户设备?

请帮助这个服务的理解。
先谢谢了。

解决方案

我已经在我们的应用程序之一实现这一点。在此,我们将通知发送给每当新品上市得到补充用户。

用户可以从多个设备同时登录。设备可能是Android或IOS东西。 另外多个用户可以登录由同一个设备(后退出previous用户)。

当用户从Android设备登录会得到GCM的注册ID。这GCM ID不是恒定的一个设备,它被改变时,你再从GCM服务器请求它。

我们发送通知到多个设备使用的通知键,它包含了GCM注册的所有设备的标识。如果你失去了一个关键的通知,你不能拿回来。更糟糕的是,你不能再生一个新的通知密钥与previously使用的通知键名。 切勿使用独特的数据从用户帐户(电子邮件,用户名..)作为通知的键名。总是产生一些随机的字符串作为通知的键名。

要克服这种后续以下步骤 1.创建一个表来存储以下内容detsils     列:         ID         用户帐号         deviceGCMId         DEVICEID         notificationKey         notificationKeyName

<醇开始=2>
  • 每当发送以下参数到服务器中的用户日志。 GCMId,DEVICEID,用户id
  • 首先检查用户id和DEVICEID存在与否。 如果不存在,则转到步骤5否则到步骤6
  • 从给定的用户id得到表notificationKey和notificationKeyName。如果没有,再产生新的notificationKeyName,并从该密钥生成新的notificationKey。使用当前GCMRegId生成密钥。在变量存储密钥。
  • 在表中添加新条目notificationKey和步骤4中键名产生此外,根据通知的关键添加GCM ID。
  • 检查的DeviceID是相同的,GCM id是不同的,然后添加更新表中的GCM ID,并添加下通知密钥GCM ID。如果设备ID和GCM ID相同则不需要做任何事情
  • 有时候通知键得到GCM服务器丢弃,就说明没有发现错误的关键。在这种情况下创建新的键名新notificationKey并添加GCM对IDS的关键。

    有关更多信息通过以下有用的链接

    https://medium.com/appunite-edu-collection/notifications-d7df385b0ff4

    https://medium.com/@Bicx/adventures-in-android-user-notifications-e6568871d9be

    希望这将有助于

    I have app that allows user to login with credentials issued by the server, lets say simple login with password and usename.
    So user starts the app, enter username and password, server authentificates the user, save this information. (details are really don't play any role in this case).
    What I need to implement ?
    There some events when my server need to notify user. Lets consider order status changed. If so I need to notify user about this, with notification in the status bar.
    I have decided to use GCM for this task.
    I read about this, and got some questions.

    1. When user gets registration ID from GCM service in android, is it constant or it can be changed. As I understand there is no guarantee that it be always the same. So how can I handle refreshes of this ID. Or if I will not call GoogleCloudMessaging.getInstance(applicationContext).registerit will stay the same until I register new or even if I call register method it can return the same ID. May I save this ID in SharedPreferences for example to use it for a long time ? Or it can be become invalid after some period of time ?

    2. The main problem is that I need to handle multiple users on the same device. As far as I know, registration id issued by gcm service it tied to the device and app. So if new user logged out, and logged in with new credentials (new user account) how can I handle this ? I can for example store this ID in my database on the server and use it until user logout from the account inside my app, and after that just remove it from the database, and if user on this device will login with another account registration ID can be used to notify another user ? Will it work ? Or I have missed something.

    3. How can I handle user multiple device. My group notifaction key will be some unique data from user account (email,username..). If I understand correctly, I need to get registration ID on all devices where user logins with its my server account and map this to one notification key. Should I handle notification dismiss by myself and than send upstream message to the GCM server in order to notify other user devices that notification has been dismissed ?

    Please help with understanding of this service.
    Thanks in advance.

    解决方案

    I have implemented this in one of our app. In this we send the notification to users whenever new listing get added.

    User can login from multiple devices same time. Device may be android or ios anything. Also multiple users can login from same device (After logging out previous users).

    Whenever user log in from Android device will get the registration ID from GCM. This GCM id is not constant for one device, it gets changed whenever you again request it from GCM server.

    We send the notifications to multiple devices using notification key, it contains GCM register ids of all devices. If you lose a notification key, you can’t get it back. To make it worse, You can’t regenerate a new notification key with a previously-used notification key name. Never use unique data from user account (email,username..) as notification key name. Always generate some random string as notification key name.

    To overcome this follow following steps 1. Create one table to store the following detsils Columns : id userId deviceGCMId deviceId notificationKey notificationKeyName

    1. whenever user logs in sent the following parameters to the server. GCMId, deviceId, userId
    2. First check that userId and deviceId exists or not. If not exists then go to step 5 else go to step 6
    3. From given userId get the notificationKey and notificationKeyName from table. If not found then generate new notificationKeyName and from that key generate new notificationKey. Use current GCMRegId to generate the key. Store the keys in variables.
    4. Add new entry in table with notificationKey and keyname generated in step 4. Also add the GCM id under notification key.
    5. Check the deviceId is same and GCM id is different then add update the GCM id in table and add that GCM id under notification key. If device id and gcm id same then no need to do anything

    Sometimes notification key get discarded from GCM server and it shows the key not found error. In this case create the new notificationKey from new key name and add GCM ids against that key.

    For more info go through following useful links

    https://medium.com/appunite-edu-collection/notifications-d7df385b0ff4

    https://medium.com/@Bicx/adventures-in-android-user-notifications-e6568871d9be

    Hope this will help

    这篇关于GCM,注册ID和处理用户登录/注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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