Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onNewToken() is not called

I was trying to get the FCM working in my app in the past few days, and I saw that the onTokenRefreshed() function and FirebaseInstanceIdService in general, is deprecated. So I followed some firebase documentations and tutorials online, but none of them seemed to be working for me. My MyFirebaseMessagingService class is:

package com.example.android.aln4.Classes;  import android.util.Log; import com.google.firebase.messaging.FirebaseMessagingService;  public class MyFirebaseMessagingService extends FirebaseMessagingService {      private static final String TAG = "MyFirebaseMessaging";      @Override     public void onNewToken(String token) {         super.onNewToken(token);         Log.d(TAG,"Refreshed token: "+token);     } } 

and my manifest contains this following code:

<service android:name=".Classes.MyFirebaseMessagingService">     <intent-filter>         <action android:name="com.google.firebase.MESSAGING_EVENT" />     </intent-filter> </service> 

Plus, all of my firebae-related implemetations are up-to-date, as the following:

  //Firebase     implementation 'com.google.firebase:firebase-crash:16.2.1'     implementation 'com.firebase:firebase-client-android:2.5.2'     implementation 'com.google.firebase:firebase-core:16.0.6'     implementation 'com.google.firebase:firebase-database:16.0.6'     implementation 'com.firebase:firebase-client-android:2.5.2'     implementation 'com.firebaseui:firebase-ui-database:2.1.1'     implementation 'com.google.firebase:firebase-storage:16.0.5'     implementation 'com.google.firebase:firebase-firestore:18.0.0'     implementation 'com.google.firebase:firebase-messaging:17.3.4' 

My problem in general, is that whenever I run the app, doesn't matter if it's after uninstalling and installing or on a regular run, the onNewToken() function is not called, or at least I don't see it in the Logcat. Any sort of help would be appriciated :)

like image 696
Adi Harel Avatar asked Feb 01 '19 08:02

Adi Harel


People also ask

When call onNewToken?

onNewToken() method is called even though the token is not updated, it gives the same token. I don't maintain a copy of token in App's local DB. Hence whenever FirebaseMessagingService. onNewToken() method is called, App again sends token to the server even though it is actually not updated.

What is onNewToken?

onNewToken(String token) Called when a new token for the default Firebase project is generated. void. onSendError(String msgId, Exception exception) Called when there was an error sending an upstream message.


2 Answers

onNewToken method is just called when the token is generated, you should retrieve the token in an activity.

Add this to your activity:

FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(SplashActivity.this, new OnSuccessListener<InstanceIdResult>() {         @Override         public void onSuccess(InstanceIdResult instanceIdResult) {             String token = instanceIdResult.getToken();             Log.i("FCM Token", token);             saveToken(token);         }     }); 
like image 87
Helton Malambane Avatar answered Sep 17 '22 19:09

Helton Malambane


December 2020 update : Using the new Firebase SDK (21.0.0) , you can get the token by FirebaseInstallations.getInstance() within your scope :

FirebaseInstallations.getInstance().getToken(false).addOnCompleteListener(new OnCompleteListener<InstallationTokenResult>() {           @Override           public void onComplete(@NonNull Task<InstallationTokenResult> task) {               if(!task.isSuccessful()){                   return;               }               // Get new Instance ID token               String token = task.getResult().getToken();            }       }); 
like image 22
ismail alaoui Avatar answered Sep 18 '22 19:09

ismail alaoui