I noticed the in com.google.firebase:firebase-messaging:17.1.0
FirebaseInstanceIdService
has been deprecated so tried overriding onNewToken()
in FirebaseMessagingService
but it seems like it never gets called.
and FirebaseInstanceId.getInstance().getInstanceId().getResult().getToken()
return an exception
java.lang.IllegalStateException: Task is not yet complete`
is there anything in particular that I'm missing?
So I have followed the docs and here is my code
Manifest:
<!--<service android:name=".network.AppFirebaseInstanceIdService">-->
<!--<intent-filter>-->
<!--<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />-->
<!--</intent-filter>-->
<!--</service>-->
<service
android:name=".network.AppFirebaseMessagingService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
AppFirebaseMessagingService:
@Override
public void onNewToken(String s) {
Log.d(TAG,"Refreshed token: " + s);
sendRegistrationToServer(s);
}
In Activity:
_btnTemptest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseMessaging.getInstance().setAutoInitEnabled(true);
Toast.makeText(LoginActivity.this, FirebaseInstanceId.getInstance().getInstanceId().getResult().getToken(), Toast.LENGTH_SHORT).show();
// which throws java.lang.IllegalStateException: Task is not yet complete`
}
});
Update tokens on a regular basis This requires you to: Add app logic in your client app to retrieve the current token using the appropriate API call (such as token(completion): for Apple platforms or getToken() for Android) and then send the current token to your app server for storage (with timestamp).
An FCM Token, or much commonly known as a registrationToken like in google-cloud-messaging. As described in the GCM FCM docs: An ID issued by the GCM connection servers to the client app that allows it to receive messages. Note that registration tokens must be kept secret.
To grab the FCM token of the user's device with React Native, we can use Firebase's getToken() function, which is available in the @react-native-firebase/messaging package. In App. js, create an asynchronous function to get the FCM token of the user's mobile.
is there anything in particular that I'm missing?
No you are not missing anything
Suggestion
Don't Use FirebaseInstanceId.getInstance().getInstanceId().getResult().getToken()
sometime it throw an exception( i have also face the same issue)
how to get Token
You need to Use
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MyActivity.this, new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String newToken = instanceIdResult.getToken();
Toast.makeText(LoginActivity.this, newToken, Toast.LENGTH_SHORT).show();
}
});
For more information check it here firebase github link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With