Ranhot
Ranhot

Reputation: 334

Firebase notifications received only after relaunching app

I have setup Firebase using Firebase assistant in Android Studio. I am facing one small, but irritating issue. The notifications doesn't work on first launch, but working on all subsequent launches. e.g.

I suspect that subscription request is not working on first request. following is my token refresh code in android FirebaseInstanceIdService:

 @Override
 public void onTokenRefresh() {
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        if(refreshedToken != null){
            Log.d(TAG, "Refreshed token: " + refreshedToken);
            FirebaseMessaging.getInstance().unsubscribeFromTopic("news"); 
            sendTokenToServer(refreshedToken);
            FirebaseMessaging.getInstance().subscribeToTopic("news");
        }
    }

I do get the registration token in first launch and it is sent to server successfully. But the notification is received only after restarting the app. Any ideas why it is not working in first launch?

Edited code as suggested in comments. I have saved new token to sharedPrefs and called the Subscription function from activity. BUT THIS IS STILL NOT WORKING. App getting notification only after relaunching the app.

 @Override
    public void onTokenRefresh() {
       String refreshedToken = FirebaseInstanceId.getInstance().getToken();
       if(refreshedToken != null){
            Log.d("FCGM", "Refreshed token: " + refreshedToken);
            sendTokenToServer(refreshedToken);
            myPrefs settings = new myPrefs(); //my class to handle prefs, working fine
            settings.putBool("isNewToken",true);
            settings.putString("token",refreshedToken);
        }
    }

then, in my activity onCreate function, i called the subscription function after a 5 sec delay to make sure that the token is generated and saved.

new Timer().schedule(new TimerTask() {
  @Override
  public void run() {
    myPrefs settings = new myPrefs();
    if (settings.getBool("isNewToken")){
        Log.d(TAG,"new token found");  //this is reaching 
        FirebaseMessaging.getInstance().unsubscribeFromTopic("news");
        FirebaseMessaging.getInstance().subscribeToTopic("news");
        settings.putBool("isNewToken",false);
    }
  }
}, 5000);

Upvotes: 3

Views: 2062

Answers (2)

Ranhot
Ranhot

Reputation: 334

Turned out the issue was GET request in my sendToken function, which was not finishing off. It should have been a POST method. I have now changed it and is working perfectly. my current onTokenRefresh code is:

 @Override
public void onTokenRefresh() {
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    if(refreshedToken != null){
        FirebaseMessaging.getInstance().unsubscribeFromTopic("news");
        sendTokenToServer(refreshedToken);
        myPrefs settings = new myPrefs();
        settings.putBool("isNewToken",true);
        settings.putString("token",refreshedToken);
        FirebaseMessaging.getInstance().subscribeToTopic("news");
        Log.d(TAG, "Subscribed");
    }
}

the send token code looks like this now:

 client = (HttpURLConnection) url.openConnection();
 client.setRequestMethod("POST");
 client.setDoOutput(true);
 OutputStream outputPost = new BufferedOutputStream(client.getOutputStream());
 outputPost.write(params.getBytes());
 outputPost.flush();
 outputPost.close();

Hope it helps someone with similar issue.

Upvotes: 0

FrankR
FrankR

Reputation: 206

Try moving your topic subscription into your Application subclass or your main activity, the code to subscribe to the topic looks after sending the correct token in the call.

Also by only having the subscription call in onTokenRefresh() what would happen if your user unsubscribed from the topic at a later stage? They wouldn't be able to re-subscribe at a later point.

Upvotes: 1

Related Questions