Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unregistered token error after sending one notification

I am facing this dramatic problem with my android app while using Firebase. 1. My app receives token on the first launch 2. I am able to send notification from firebase console to the registered token 3. If I try to send a notification again with the console just after step 2. It shows me "unregistered token" after 2nd attempt onwards.

I have all necessary configuration already there in the manifest and google service.json file is also in place with the correct configuration. I believe things are correct because the app is able to receive notification once and the problem starts coming only after that.

Update 1: If I uninstall app and reinstall it then also I am able to receive notification only once.

Those who wants to look at the code, this is how I am getting token:

@Override
public void onTokenRefresh() {

    //Getting registration token
     refreshedToken = FirebaseInstanceId.getInstance().getToken();

    //Displaying token on logcat
    Log.d(TAG, "Refreshed token: " + refreshedToken);
     saveDeviceToken(refreshedToken);
}

This gets calls only first launch and after that, I haven't seen it getting called (which I think is expected behavior).

OnMessage received also gets called on first notification and then it never gets called off:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d("FCM", "From: " + remoteMessage.getFrom());

    if (remoteMessage.getNotification() != null) {
        Log.d("FCM", "Notification Message Body: " + remoteMessage.getNotification().getBody());
        sendNotification(remoteMessage.getNotification().getBody());
    }
}

Update 2 : Tried to hit the HTTP api of FCM using the same server key and token and got following response:

   {
  "multicast_id": 6286279702096230688,
  "success": 0,
  "failure": 1,
  "canonical_ids": 0,
  "results": [
    {
      "error": "NotRegistered"
    }
  ]
}

Just to avoid cross questions here are few more details:

  • Android studio : v2.3.1
  • Google play service version : 10.0.1
  • Libraries included : core, database, storage, messaging - all is having same version as google play service 10.0.1

Update 3 : Firebase crash, database and storage is working in the same project (which shows that google service.json file is correct).

Please help me to get it fixed.

like image 387
Irfan Raza Avatar asked Apr 21 '17 00:04

Irfan Raza


People also ask

What is registration token in FCM?

An FCM Token, or much commonly known as a registrationToken like in google-cloud-messaging. As described in the GCM FCM docs: An ID issued by the GCM connection servers to the client app that allows it to receive messages. Note that registration tokens must be kept secret.

What does registration token mean?

A registration token is tied to a certain group of senders. When a client app registers for FCM, it must specify which senders are allowed to send messages.


1 Answers

I remember facing the similar problem before. Here is what I did and its working smooth for me. Hope this helps,

PreferenceStore is a helper class to save, edit and delete shared preferences.

@Override
public void onTokenRefresh() {
    String token = FirebaseInstanceId.getInstance().getToken();
    Log.i(TAG, "New FCM Token: " + token);

    setup_flag = PreferenceStore.getBoolean(getApplicationContext(), PrefKeys.IS_FIRST_FCM_TOKEN);
    if (!setup_flag) {
        // save token and boolean value in shared preferences for 
        // the first time you run the app after install
        PreferenceStore.saveString(getApplicationContext(), "FCM_TOKEN", token);
        PreferenceStore.saveBoolean(getApplicationContext(), "IS_FIRST_FCM_TOKEN", true);
    } else {
        // other time your app loads, update the token (like call the API endpoint).
    }
}
like image 71
denizen Avatar answered Oct 11 '22 17:10

denizen