Sujith S Manjavana
Sujith S Manjavana

Reputation: 1596

ANR on FirebaseInstanceIdReceiver

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?

Upvotes: 2

Views: 3012

Answers (4)

rNkL
rNkL

Reputation: 472

Add this permission to your manifest file. also no need to call methods setFcmToken function you can directly write pushFcmToken in onNewToken method.

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<permission
    android:name="{applicationId}.gcm.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

use of this permission is to that This app has permission to register and receive data message.

Upvotes: 0

lukjar
lukjar

Reputation: 7425

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.

Upvotes: 6

Rahul Shukla
Rahul Shukla

Reputation: 1292

Your entire logic to upload the FCM token appears to be too complex.

  1. In your service, when you already have received the new FCM token, can't you just pass it to setFcmToken() function directly?
  2. Why threads? It would be better if you use work manager instead.

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

Upvotes: 1

Masum Billah
Masum Billah

Reputation: 1103

You may use it.

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
    <permission
        android:name="${applicationId}.permission.C2D_MESSAGE"
        android:protectionLevel="signature" 

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

Upvotes: 1

Related Questions