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:
The registration_id which is stored in SharedPreferences must be cleared because another user might log in the next time the app is launched.
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?