8

For the application that I am working I need to integrate Google Cloud Messaging. After playing a little with different examples, I was able to send and receive a notification on my deivce.
However, I have come across an interesting situation. As I know (please correct me if I'm wrong), the registration_id is issued per device and per application.

The application that I'm working supports login functionality. When the application is installed and the user logs in for the first time (let it be "UserA"), I request the registration_id from GCM which then I send it to my server.

Now imagine that UserA logs out and gives his device to some UserB to log in. With other words, UserB logs in using UserA's device.

The problem is that if meanwhile UserA receives a notification, UserB will be able to intercept it. And if UserB receives a notification, he won't be able to receive it.
This seems normal because the registration_id is per device and per application, but it does not seem reasonable for my case.

So I'm asking if there is a way I could make the registration_id to be dependent of some user id (besides the device and app)? Or how could I make such that the logged in user to receive only his own notifications?

4

1 回答 1

7

是的,每台设备都有一个应用程序的谷歌注册 ID,这是真的。

但是您始终可以在您自己的服务器上注册和注销用户,该服务器实际上会向 GCM 发送消息,而 GCM 会将其发送到已注册的设备。

为您的服务器定义一些接口,例如 registerOnServer 和 unRegisteronServer ,在此接口上为每个用户发送一些唯一值。

因此,在您的情况下,当 A 使用 Log ins 时,首先在 GCM 上完成注册,并使用 registerOnServer 接口在您的服务器上注册用户,并在用户登录时将与他有关的通知发送到 GCM 以发送到设备。

当 A 注销时,使用 unRegisterServer 注销他,并且不要从您的服务器向 GCM 发送任何消息,因为 A 已注销。

所以,现在如果 B 使用相同的设备登录,请在您的服务器上注册他并发送他的消息。

这将解决您的问题!

于 2013-07-22T12:11:10.140 回答