I'm getting the following error in my play console without any stack trace (on android 7 and 10).
Broadcast of Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000010 pkg=com.mycompany.myapp cmp=com.mycompany.myapp/com.google.firebase.iid.FirebaseInstanceIdReceiver (has extras) }
Firebase version used,
implementation 'com.google.firebase:firebase-iid:21.1.0'
implementation 'com.google.firebase:firebase-messaging:23.0.6'
I don't know which part of my code exactly causing this error, I'm posting the code that is responsible for uploading the FCM token to my server.
MyFirebaseMessagingService,
@Override
public void onNewToken(@NonNull String s) {
super.onNewToken(s);
new TokenUploader().setFcmToken();//send to server
}
TokenUploader class
//this function is called from MyFirebaseMessagingService and MainActivity
public void setFcmToken() {
FirebaseMessaging.getInstance().getToken().addOnCompleteListener(task -> {
if (task.isSuccessful()) new Thread(() -> pushFcmToken(task.getResult())).start();
else if (task.getException() != null)
new Thread(() -> pushFcmToken(task.getException().getMessage())).start();
});
}
//push token to server
private void pushFcmToken(String token) {
if (account == null) return;
try {
JSONObject params = new JSONObject();
params.put("email", account.getEmail());
params.put("password", getPassword());
params.put("key", getApiKey());
params.put("fcm_token", token);
JSONObject obj = null;
for (int i = 0; i < 4; i++) {//try 4 times
obj = sendRequestToApi("POST", params, "/fcm_route");
if (obj != null && obj.has("fcmId")) break;//successfully uploaded
}
} catch (Exception e) {
}
}
I strongly believe that I'm doing something wrong here, because a good number of devices have failed to upload their token to my server. How can I prevent the above error and get a 100% success rate in uploading the token?