Firebase onTokenRefresh() is not called

后端 未结 13 2199
天命终不由人
天命终不由人 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:39

    onTokenRefresh in FirebaseInstanceIdService is only called when a new token is generated. If your app was previously installed and generated a token then onTokenRefresh would not be called. Try uninstalling and reinstalling the app to force the generation of a new token, this would cause onTokenRefresh to be called.

    Also be sure that your FirebaseInstanceIdService is properly defined in your AndroidManifest.xml

    In your Manifest File.

     
            
                
            
        
    
        
            
                
            
        
    

    FirebaseInstanceIdService class

    public class MyFireBaseInstanceIDService extends FirebaseInstanceIdService {
    
    
    private static final String TAG = MyFireBaseInstanceIDService.class.getSimpleName();
    
    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);
    
        if (refreshedToken!=null) {
            SettingPreferences.setStringValueInPref(this, SettingPreferences.REG_ID, refreshedToken);
        }
        // TODO: Implement this method to send any registration to your app's servers.
        sendRegistrationToServer(refreshedToken);
    }
    // [END refresh_token]
    
    /**
     * Persist token to third-party servers.
     *
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
    }}
    

    FirebaseMessagingService class.

    public class GCMNotificationIntentService extends FirebaseMessagingService {
    // Sets an ID for the notification, so it can be updated
    
    
    public GCMNotificationIntentService() {
        super();
    }
    
    
    @Override
    public void onMessageReceived(RemoteMessage message) {
    
    }}
    

提交回复
热议问题