Okay so I have an app which on first start takes you through a few welcoming slides, then takes you to a login/register page and then to MainActivity
.
I have just implemented FCM
and the services generate a token
before any of those pages have been seen by the user. How could I make it so that the service runs after I get to MainActivity
?
The problem is I'm trying to send the token as soon as it is refreshed to the MySQL DB
to the appropriate user account, but since the user hasn't signed in yet, that is null
and my message to the server fails. What's a good way to design this? I thought of saving the token in SharedPreferences
and sending it to the server after the user has logged in but that creates lots of complications when the token is refreshed at some later point?!
Possible solution:
I'm not sure I completely understand how the 2 services run but say in onTokenRefresh
I just save the token into SharedPreferences
and in MainActivity I get the value from SP
and then I send it to the server. In that case when the token is refreshed the new value will immediately go into SharedPreferences
again. But I would still need to check if it's a new value in SP and then reupload it to the server. This is confusing!
Ensuring registration token freshness To cover all cases, you should adopt a threshold for when you consider tokens stale; our recommendation is two months. Any token older than two months is likely to be an inactive device; an active device would have otherwise refreshed its token.
Firebase Cloud Messaging (FCM) is a messaging solution that lets you reliably send messages at no cost to both Android & iOS devices. Using FCM, you can send data payloads (via a message) to a device for a specific application. Each message can transfer a payload of up to 4KB to a client.
On Android and Web/JavaScript, you can specify the maximum lifespan of a message. The value must be a duration from 0 to 2,419,200 seconds (28 days), and it corresponds to the maximum period of time for which FCM stores and attempts to deliver the message.
Note that you can always retrieve the token with:
FirebaseInstanceID.getInstance().getToken();
This will return null
if the token has not yet been generated or the token if it has been generated. In your case it is very likely that the token will be generated by the time the user has signed in. So you should be able to send it to your app server as soon as the user has signed in. If it is not available then you would send it in the onTokenRefresh callback as Chintan Soni mentioned.
Edit
Using the new Firebase SDK (21.0.0) , you will get your token this way :
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();
}
});
You better add a listener for more handling on the response .
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