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?
4
Answers
You may use it.
You also update your all firebase service and update Plugin
“com.google.gms:google-services:4.3.3”
You may check this for more clear. Link
Your entire logic to upload the FCM token appears to be too complex.
Plus, make sure you aren’t doing anything extra in your FirebaseInstanceID service and in your TokenUploader class. This might also help: https://github.com/firebase/firebase-android-sdk/issues/3052
These ANRs for
act=com.google.android.c2dm.intent.RECEIVE
are very common in Android apps which are sending a lot of push notifications.Most of them are caused by long application startup and processing push notifications on the main thread.
Nickolay Chameyev from the Bumble Tech described it well in article: How we achieved a 6x reduction of ANRs – Part 2: Fixing ANRs.
Add this permission to your manifest file. also no need to call methods setFcmToken function you can directly write pushFcmToken in onNewToken method.
use of this permission is to that This app has permission to register and receive data message.