I am using FCM to provide notifications in my app. Everything worked well, but now I realised that, when I install my application using Android Studio (not from GooglePlay) the token is null at first run. When I close my app and restart it, the token is not null anymore. What cause this problem and how can I avoid it?
InstanceIDService:
public class InstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
String token = FirebaseInstanceId.getInstance().getToken();
registerToken(token);
}
private void registerToken(String token) {
OkHttpClient client = new OkHttpClient();
RequestBody body = new FormBody.Builder()
.add("Token",token)
.build();
Request request = new Request.Builder()
.url("url_to_registration_script")
.post(body)
.build();
try {
client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In MainActivity:
FirebaseMessaging.getInstance().subscribeToTopic("topic");
String token = FirebaseInstanceId.getInstance().getToken();
Log.d("TOKEN", token);
Last log returns null when app is installed and started for the first time
Registration script:
<?php
if (isset($_POST["Token"])) {
$_uv_Token=$_POST["Token"];
$conn = mysqli_connect("localhost","user","pass","db") or die("Error connecting");
$q="INSERT INTO users (Token) VALUES ( '$_uv_Token') "
." ON DUPLICATE KEY UPDATE Token = '$_uv_Token';";
mysqli_query($conn,$q) or die(mysqli_error($conn));
mysqli_close($conn);
}
?>
I was facing the same problem. I looked through a lot of SO posts and other forums and I found a solution that worked for me. FCM documentation says to use this method to get a token.
I found a post online (I apologize, I don't remember which one. I found it a while ago) that used this method instead:
FCM documentation describes the first method as:
While the second one is described as:
I have been looking into why the first method call takes a longer time to return a token, but I haven't found an answer yet. Hope this helps.
Sometimes network issues may occur, even internet is working fine.... check your
FirebaseInstancId
class defined in Manifest file in your android project.I just fell in same issue and this is the way I fixed it. Call the block somewhere in
onCreate()
method of your activity. I want to sent the Token to my Parse server so you must change it based on your needs.This is the sample I followed.
I was having the same problem in a Android 4.2 device. So, to solve the problem, first, check if your service, in the manifest, if it have a priority like this:
Second, use a broadcast intent to notify your MainActivity that changed the token:
Then in MainActivity, in the onCreate, add this:
and, in the MainActivity, add a new BroadcastReceiver:
Even I had the same issue. I had given the Log statement to print token in onCreate of my launcher activity. It takes time to refresh the token once you uninstall the app. Thats why you get null. You have to wait for a bit to get the token refreshed.
The biggest issue i faced after writing the correct code is:- not the latest version of google play services in build-gradle, So always use the google-play-services version shown by firebase in setting up dialog