0

For push notifications in android, FCM creates token for each device. On that scenario if multiple users use the same device for login, how the token works for push notification? If push notification sends only for current user, what about the other user who already used the device? will this notification be in queue and send if the user is active again the same device? Please help me in this scenario. Thanks.

PrasathBV
  • 303
  • 4
  • 11
  • yes, notification is sent to all users if your server not deleting these tokens for old users, But notifications received on only that device that are all users – J Ramesh Sep 11 '17 at 12:30
  • Possible duplicate of [Is FCM (firebase cloud messaging) Token for one device or for one account?](https://stackoverflow.com/questions/37693932/is-fcm-firebase-cloud-messaging-token-for-one-device-or-for-one-account) – Vishvendu Palawat Sep 11 '17 at 12:32

2 Answers2

1

Actually, You can manage by checking your login user id compare with received notification UserId in your FCM message receive service method onMessageReceived().

Full Detail with Example : When you generate notification from server please UserID with extra parameter like following way,

{"to":"[add your token]","data":{"title":"[add title]","body":"[add your message]","userId":"[your userid]"},"priority":"high"}

Know after doing this please compare your login user Id and notification received and if both match then generates your local notification another wise don't generate, check below.

@Override
    public void onMessageReceived(final RemoteMessage remoteMessage) {
        Log.d("onMessageReceived-->", "getData ->" + remoteMessage.getData());
        super.onMessageReceived(remoteMessage);
        try {

            String userId=remoteMessage.getData().get("userId");
            if(userId==loginUserId){
                genrateLocalNotification();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39
0

When your token is generated, it will store in refreshedToken string. This statement is execute first because we are creating a service.So when app is launch ....service is call and we should store that string into shared preference and than whenever we need that string ,we want to use at that time.

 @Override
public void onTokenRefresh() {
    //Getting registration token
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    // store the refreshedToken into shared preferencees.
}
Arjun Solanki
  • 236
  • 1
  • 11
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Rosário Pereira Fernandes Sep 11 '17 at 18:11
  • When your token is generated , it will store in refreshedToken string. This statement is execute first because we are creating a service.So when app is launch ....service is call and we should store that string into shared preference and than whenever we need that string ,we want to use at that time. – Arjun Solanki Sep 12 '17 at 06:00