Firebase onTokenRefresh() is not called

后端 未结 13 2087
天命终不由人
天命终不由人 2020-11-27 16:59

In my MainActivityin my log, I can see the token using FirebaseInstanceId.getInstance().getToken() and it display the generated token. But it seem

13条回答
  •  情深已故
    2020-11-27 17:26

    I've been struggling with that for over an hour, then I changed the following code and it suddenly worked.

    handle your refreshedToken as such:

    String refreshedToken;
        try {
            refreshedToken = FirebaseInstanceId.getInstance().getToken();
            Log.d(TAG, "Refreshed token: " + refreshedToken);
            Localytics.setPushRegistrationId(refreshedToken);
        } catch (Exception e) {
            Log.d(TAG, "Refreshed token, catch: " + e.toString());
            e.printStackTrace();
        }
    

    Try adding android:exported="true"> to both MyFirebaseMessagingService and MyFirebaseInstanceIDService in manifest so it looks like that:

    
            
                
            
        
    
        
            
                
            
        
    

    Uninstall your app, Install again, worked. Tested on real device.

    android:exported="true" is a default option so you can also remove that entirely and it will be set to true.

    Also if you want to call onTokenRefresh more explicitly, you can simply call that anywhere in your code:

    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Localytics.setPushRegistrationId(refreshedToken);
    

    You don't have to rely on broadcast being received any more

提交回复
热议问题