Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android-GCM: How to take care of a logged out user?

In my app, once I register a device, it's stored as persistent data in order to check whether the device is already registered.

On the server side I store them in a database along with a user_id which is a unique id for a user.

[user_id] [gcm_registration_id]

Now I am unable to tackle this case:

If the User logs out:

  1. The registration_id which is stored in SharedPreferences must be cleared because another user might log in the next time the app is launched.

  2. In addition, I have to delete the row corresponding to that registration_id from my database because that device (which has no currently logged in user) must not receive any more notifications.

The problem is that my database gets updated using the canonical_ids or say the latest registration_ids I get when calling

https://android.googleapis.com/gcm/send

So, there is a chance that I cannot find the old registration_id of the logged out user to delete, which still allows sending notifications to a non logged in user.

Also I cannot delete a row my DB matching the user_id because a user might have logged out from only 1 device and there are few more devices on which he/she is still logged in.

How will I know that for this particular user and device, the registration id is changed?

Should I store all the old registration ids and their corresponding canonical_ids in my database?

like image 395
user1537779 Avatar asked Oct 03 '22 23:10

user1537779


1 Answers

You have two options :

  1. As suggested in the comments - keep the registration ID of the user that logged-out in your DB, but mark it with a flag that indicates it's a logged-out user. Your server won't send notifications to registrations IDs marked as logged-out.

  2. When a user logs out from your app on a device, the app on that device will un-register from GCM and send a log-out request to your server with the user-id and the registration ID. The server will delete that user-id/registration-id combination from the DB. Even if for some reason your server contains a newer registration ID for that device (causing the delete attempt to fail), you won't be able to send notifications to the device with that registration ID, since the app unregistered the device from the GCM service. But this case is not likely to happen anyway, since whenever GCM changes the registration ID for a given app on a given device, is sends that registration ID to the app, and the app should send in to your server. Only failing to do so may cause the situation in which your server sends a notification with an old registration ID and gets a new canonical registration ID in the response.

like image 117
Eran Avatar answered Oct 13 '22 09:10

Eran