this is a common situation that lot's of apps facing, but I'm straggling to understand how to implement:
let's say that my application is a social network with a current logged in user. I'd like to send GCM messages to that user, for all of his devices that currently logged in with my app.
that's mean that my server holds for each user a list of all of his registration ID's - one for each of his devices.
the problem: how can I track uniquely each one of his devices? seems there is no reliable way to get specific device identifier
without storing registration id for each unique device - I don't see how can I manage it.
things get messy when user will uninstall/logout and acquire new registration id afterwards, that suppose to replace one of the existing known id's, but which one of them??
things will get even more problematic if Id like to send message only to a specific device, and not all of them...
please help me understand what I missing, and what is the right way handling multiple devices registration id's for same user.
Google handles pretty much all the hard work for you when working with GCM. And they provided a simple method to always keep the registration id up to date. There is an extra field on each message sent called canonicalRegistrationId
. If there is an id in that field, than the registration id has changed and needs to be updated. This field exists on every message and every time you send one, you have to check that field. If there is a new canonicalRegistrationId
then you should update the registrationId as soon as possible. The old one may continue to work for some time, but there is no telling when it becomes invalid.
For example in a Google App Engine backend the code which handles the changing registration ids would look something like this:
// We want to send a message to some devices
// Get registered devices and then loop through them
List<RegistrationRecord> records = ofy().load().type(RegistrationRecord.class).limit(10).list();
for(RegistrationRecord record : records) {
// Send the message to one device
Result result = sender.send(msg, record.getRegId(), 5);
// If the messageId is not null, then the message has been sent successfully
if (result.getMessageId() != null) {
log.info("Message sent to " + record.getRegId());
// Check for canonical message id
// If it is not null, then the registrationId has changed
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// The registrationId has changed! We need to update it in the database
log.info("Registration Id changed for " + record.getRegId() + " updating to " + canonicalRegId);
record.setRegId(canonicalRegId);
ofy().save().entity(record).now();
}
} else {
... // Irrelevant error handling
}
}
So what you have to do is pretty simple: Every time you send a message, check if there is a canonicalRegistrationId
, if yes then update the old registrationId with the canonical one.
Google supports device group messaging. Refer to this link: https://developers.google.com/cloud-messaging/notifications#managing_device_groups
To handle multiple users you can create a group for that user and add the registration id to that group. To create a group send a request like the following to https://android.googleapis.com/gcm/notification:
Content-Type:application/json Authorization:key=API_KEY project_id:SENDER_ID
{ "operation": "create", "notification_key_name": "appUser-Chris", "registration_ids": ["4", "8", "15", "16", "23", "42"] }
This API returns a notification key in response which you can store in your db and later use to send notifications to that group. Subsequently whenever user logs in to some other device you can get the reg_id
of that device and add it to the group. To add a device with the registration ID 51 to appUser-Chris
, you would send this request:
{ "operation": "add", "notification_key_name": "appUser-Chris", "notification_key": "aUniqueKey", "registration_ids": ["51"] }
And when user logs out of the device you can remove the reg_id
from the group.
Sending a notification to a group is same as sending notification to a registration id.
One issue I have observed here is if you fail to store the notification_key
to your db while creating a group there is no way you can get it again. And a new group cannot be created for that same notification_key_name
again. A workaround to this problem is choosing your notification_key_name
wisely. So when you fail to store the notification_key
and try again to create a group with the same noitification_key_name
, you will get an error. In that case you can simply change the notification_key_name
for that user and create a new group. Hope this helps.
i think you have to get Device IMEI no and save it to your server with Registration id and when user ReRegister him self with other device then get Check IMEI Number Replace it with old one. Main thing Rise now in old device check send IMEI no with message and IF IMEI no is Match then no issue otherwise block application.and inform user...
Thats it...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With