Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I generate android GCM registration ID app specific or user specific?

I am developing an app which has login functionality.I am generating only one gcm registration ID per app. If other user sign's in in same app he will start receiving notifications intended for the previous user. How to handle this situation, so that each user will get notification intended for his/her? Should I generate gcm reg id for each user? What is the standard way to handle this situation?

like image 898
Rushikesh Talokar Avatar asked Sep 24 '15 12:09

Rushikesh Talokar


People also ask

What is GCM registration ID?

The GCM Registration ID is a randomly-generated identifier that does not contain any personal or device information that could allow a developer to discover the personal identity of the user.

Does WhatsApp use GCM?

WhatsApp(and several other Apps) rely upon GCM/FCM(as option 1 - the default) as it is present as a system App on lot of devices and therefore holds a special status where it is very less likely to be killed unlike a normal App. For devices that do not have play services, your custom socket connection is relied upon.

How does GCM work in Android?

GCM stands for Google Cloud Messaging. Every push notification receive on any Android device is sent by the GCM only. when sender sends an push notification then it goes to GCM. GCM receives that push and forward it to particular Android Device by its Unique device id.


2 Answers

You could try the following things:

  1. When the user logs off, send a request to delete the token on your server, and erase it on your app;
  2. Once the user logs off, you could simply remove the association of "User ID" to "GCM Token" (Or Registration ID) on your server. And when someone logs in again, you make a new association to that Token.

The GCM Token is app specific, but the association you make on your server is totally up to you.

And I can't stress it enough, the token generated by GCM is APP SPECIFIC. If your user logs in on multiple devices, your server should handle that, associating the user ID to multiple Registration ID Tokens.

An ID issued by the GCM servers to the Android application that allows it to receive messages. Once the Android application has the registration ID, it sends it to the 3rd-party application server, which uses it to identify each device that has registered to receive messages for a given Android application. In other words, a registration ID is tied to a particular Android application running on a particular device.

And from the docs, your server should also:

Before you can write client apps that use GCM, you must have an application server that meets the following criteria:

  • Able to communicate with your client.
  • Able to send properly formatted requests to the GCM connection server.
  • Able to handle requests and resend them using exponential back-off.
  • Able to store the API key and client registration tokens.
  • Able to generate message IDs to uniquely identify each message it sends. Message IDs should be unique per sender ID.

EDIT: You can read more about GCM here.

For further reading, you could also read more on Device Group Messaging.

You can also download some code samples here.

like image 165
Mauker Avatar answered Oct 12 '22 20:10

Mauker


You should store regID as per user.

This is because there is test cases that user1 logs out and user2 logs in. In that case if you have stored regID app specific and not binding with user then user2 will also get notification.

So you need to store regID as per user , app and as well as device specific.

like image 44
KishuDroid Avatar answered Oct 12 '22 21:10

KishuDroid