22

I want to store FCM token in my android app user's table in database when user register to the app . When i want to send notification to specific user then i will fetch Token from corresponding row and send push notification.But i am confused that whenever app user logout from his app and register new account from same device ,then there will be two row and hence two Token for same device on database. How to handle such case? What happen to old Token in device? please anyone can help me??

Roshan Sharma
  • 513
  • 2
  • 7
  • 20
  • 1
    there shouldn't be two row. if you are implementing login/logout - clear the user previous token before setting the new one – Shubhank Jun 08 '16 at 06:34
  • 5
    what happen if two user use one device but they have different account and they exchange device daily by sign in and sign out? – Roshan Sharma Jun 08 '16 at 06:50
  • they shouldn't get each other notifications hence on logout - clear the token from your backend. – Shubhank Jun 08 '16 at 06:54
  • 1
    So , then i have to delete token column from user table in backend database when they logout and insert new token when they login?? – Roshan Sharma Jun 08 '16 at 07:02
  • 1
    yes, that is right. – Shubhank Jun 08 '16 at 07:02
  • what happen to the old token on android device when new user register for new account?? – Roshan Sharma Jun 08 '16 at 07:04
  • its still the same token- you clear the token in your db for the previous user and set it to the new user. Token hasn't changed – Shubhank Jun 08 '16 at 07:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114094/discussion-between-roshan-sharma-and-shubhank). – Roshan Sharma Jun 08 '16 at 07:06
  • 1
    I faced same issue but solved as when user logout in same device it can delete previous token from database and get new token automatically at the time of login again in same device. because token is not present in database new token will be generated for same device . – Hiren Dec 13 '16 at 05:42

2 Answers2

19

The Instance ID token used to send FCM messages represents the instance of and app on a device. The current user of the app would not affect the token that represents the app instance. Uninstalling and reinstalling the app would cause a new token to be generated but not for in app behaviour like switching users.

You should still remove the mapping of the token to the user on logout and add a new mapping with the same token to the user that logs in but it will be the same token.

Arthur Thompson
  • 9,087
  • 4
  • 29
  • 33
3

I probably feel this may help you,

When user logs out clear his device token and make him Logout so that he won't be able to receive the updates and hence only one user at a time can get the updates