Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCM with login system

I'm currently implementing GCM into an application with login system. I wanted to send notification to the application based on the user that logged in to the application (one device, multiple user). I go through these processes.

  1. Login as "user A"
  2. Register GCM (get Registration ID) send to server side
  3. Broadcast Notification out to user A
  4. Logout un-register
  5. Login as "user B"
  6. Register GCM (get Registration ID -occasionally get back same registration ID with user A, sometimes return as different registration ID-)
  7. GCM push notification to user A (even if the user un-register)

I'm not sure how to let the application identify the user that logged in to the device and push notification to that specific user only. Instead of user B logged in and get user A's notification. Any comments and answers will be highly appreciated! If you need check on specific codes from my project, please do let me know.

like image 344
IssacZH. Avatar asked Apr 11 '13 10:04

IssacZH.


1 Answers

The registration ID identifies a specific application on a specific device. It has no knowledge on log-in of users within your application. Therefore, when you un-register GCM (when user logs out), you should call your server to let it know that the user logged out.

This will let your server know that this user is logged out, and the server will stop sending GCM messages to it.

It doesn't matter if you get the same registration ID after user A logs out and user B logs in (even if you get a new registration ID, the old one may still work. That's why GCM returns a canonical registration ID when the device has more than one registration ID for the application).

EDIT :

Lets consider the special scenario (which should be relatively rare) where user A logs out after your server sent it a notification but the notification gets delivered by Google to your application only after user B logs in. The safest way to handle this case is to receive the notification in your application and discard it, showing nothing to user B. In order to know when to discard a received notification you can add a user property to your notification data with the user name as its value. When you handle the notification in the app, check that the user property matches the logged in user before displaying the notification.

like image 111
Eran Avatar answered Nov 07 '22 09:11

Eran