-1

I tried to reinstall my application multiple times, I noticed that just about 4 from 10 times that the onTokenRefresh() is called (log output)

public class MyFirebaseInstanceIDService  extends FirebaseInstanceIdService {


@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

        sendRegistrationToServer(refreshedToken);
    }
}

Is there any way to guarantee that onTokenRefresh() to be called so the application will be subscribed to a topic when there is a good internet connection,something like launching a Service that check every time whether the application has subscribed to a topic,if not, relaunching firebase service.

In onCreate() function I did :

mRegistrationBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                // checking for type intent filter
                if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
                    // gcm successfully registered
                    // now subscribe to `global` topic to receive app wide notifications
                    FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);



                } else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
                    // new push notification is received


                }
            }
        };
HZDI
  • 127
  • 3
  • 8
  • The `onTokenRefresh()` method only gets invoked when the token changes. If you re-install the app, the token apparently doesn't change. For that reason you should **also** determine the token in the regular startup flow of your app. See http://stackoverflow.com/questions/37451395/firebase-ontokenrefresh-is-not-called – Frank van Puffelen Feb 26 '17 at 21:26
  • Is there any way to guarantee that the application will be subscribed to a topic? If not,lot of users will not receive push notifications sent from a server ! – HZDI Feb 26 '17 at 22:17
  • @HZDI Call `subscribeToTopic()` in `onCreate()` of your initial Activity. – AL. Feb 27 '17 at 02:53
  • @AL I edited my question. – HZDI Feb 27 '17 at 04:01

1 Answers1

0

According to docs

The registration token may change when:

  • The app deletes Instance ID
  • The app is restored on a new device
  • The user uninstalls/reinstall the app
  • The user clears app data.

So you should send the new token to this server when these events happens!

Atef Hares
  • 4,715
  • 3
  • 29
  • 61
  • So we wait that one of those events to be done,we wait a miracle,if 1 million user has installed the application,about 400 000 would not be subscribed. I'd never faced this problem with GCM. – HZDI Feb 26 '17 at 20:51
  • calm down and think about it!, me myself while working on one of my projects I had to send FCM token only at some certain single event that covers all of above points! that is because of the way of how my app works [in details if any of above events happened then the user will definitely have to go through that certain single event]. You also have to think of when you should send the token to your server and cover all these points, most probably send it on app start! – Atef Hares Feb 26 '17 at 21:11
  • Calling onTokenRefresh() is very important not only for saving the token on the server,but, to be able to send push notification to all users that have subscribed to a topic from the server. – HZDI Feb 26 '17 at 21:16