GCM: send notifications only to the current user registered in the application

For the application that I work for, I need to integrate Google Cloud Messaging. After playing a little with different examples, I was able to send and receive a notification about my case.
However, I came across an interesting situation. As I know (please correct me if I am mistaken), registration_id is issued for each device and application.

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

Now imagine UserA logging out and giving your device access to some UserB. In other words, UserB is registered using the UserA device.

The problem is that if UserA receives a notification in the meantime, UserB will be able to intercept it. And if UserB receives a notification, he will not be able to receive it.
This seems normal, because registration_id applies to the device and the application, but for my business this does not seem reasonable.

So, I ask, is there a way to make register_id be dependent on some user ID (other than device and application)? Or how can I make a registered user receive only his own notifications?

+4
source share
1 answer

Yes, it’s true that you have one google login id for the application on the device.

But you can always register and unregister users on their own server who will send messages to GCM, and GCM will send them to registered devices.

Define some interfaces for your server, such as registerOnServer and unRegisteronServer, send a unique value for each user in this interface.

So, in your case, when A uses Log ins, registration is first performed on GCM and registers the user on your server using the registerOnServer interface, and when the user registers in the sending notifications related to him, the GCM is sent to the device.

When A logs out, unregister with unRegisterServer and do not send any messages from your server to GCM, since A is not registered.

So now, if B registers even with the same device, register it on your server and send its messages.

This will solve your problem!

+4
source

Source: https://habr.com/ru/post/1492688/


All Articles