question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Firebase messaging does not work when initialized in a separate Android process

See original GitHub issue

I’ve been trying to offload Firebase messaging in our app to a separate Android process and I’ve had no luck so far.

Using Firebase-messaging:18.0.0

The first thing I tried was to make sure the FirebaseListenerService (our subclass of FirebaseMessagingService) runs in a separate process. Because of our application design, we use a CustomFirebaseInitProvider and override the default FirebaseInitProvider. This CustomFirebaseInitProvider also initializes Firebase app in the auxiliary process.

<application>
    <service
        android:name=".push.fcm.FirebaseListenerService"
        android:process="com.xxx.yyy.someprocess"
        android:enabled="true"
        android:exported="false">
      <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
      </intent-filter>
    </service>

    <!-- Disable default FirebaseInitProvider -->
    <provider android:name="com.google.firebase.provider.FirebaseInitProvider"
        tools:node="remove" />

    <provider android:authorities="${applicationId}.firebaseinitprovider"
        android:name=".push.fcm.provider.CustomFirebaseInitProvider"
        android:process="com.xxx.yyy.someprocess"
        android:exported="false"
        android:initOrder="100">
    </provider>
  </application>

This has the effect of creating the FirebaseListenerService in a separate process upon receiving the MESSAGING_EVENT intent. I am also able to verify that the FirebaseApp instance was instantiated in the auxiliary process.

However, on sending notifications, the onMessageReceived handler in FirebaseListenerService never gets called. Although a MESSAGING_EVENT intent is always sent to create the FirebaseListenerService everytime.

After some investigation I realized the com.google.android.c2dm.intent.RECEIVE intent (which causes message delivery to FirebaseMessagingService and thus to onMessageReceived) was being received only in the main process, but not in the auxiliary process. This is because the Firebase SDK internally adds the FirebaseInstanceIdReceiver to the app manifest and it comes up in the main process.

<receiver
        android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
        android:permission="com.google.android.c2dm.permission.SEND"
        android:enabled="false"
        android:exported="true">
      <intent-filter>
        <action
            android:name="com.google.android.c2dm.intent.RECEIVE" />
      </intent-filter>
    </receiver>

So I also override this to be brought up in the auxliary process. Also realized it might be necessary to bring up the ComponentDiscoveryService in the auxiliary process too:

<receiver
        android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
        android:process="com.xxx.yyy.someprocess"
        android:permission="com.google.android.c2dm.permission.SEND"
        android:enabled="false"
        android:exported="true"
        tools:node="merge">
      <intent-filter>
        <action
            android:name="com.google.android.c2dm.intent.RECEIVE" />
      </intent-filter>
    </receiver>

<service
        android:name="com.google.firebase.components.ComponentDiscoveryService"
        android:process="com.xxx.yyy.someprocess"
        android:exported="false"
        android:directBootAware="true"
        tools:node="merge">

      <meta-data
          android:name="com.google.firebase.components:com.google.firebase.dynamicloading.DynamicLoadingRegistrar"
          android:value="com.google.firebase.components.ComponentRegistrar" />

      <meta-data
          android:name="com.google.firebase.components:com.google.firebase.iid.Registrar"
          android:value="com.google.firebase.components.ComponentRegistrar" />
    </service>

After this change, the auxiliary process never comes up and I do not see any com.google.android.c2dm.intent.RECEIVE intents being received on notification send. I would have expected this to initialize FirebaseInstanceIdReceiver on notification send, but maybe my limited understanding of the Firebase SDK is totally off. Any pointers on how to initialize Firebase on a separate process?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
gsakakiharacommented, Apr 1, 2021

I assumed that:

<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:process="com.xxx.yyy.someprocess" android:permission="com.google.android.c2dm.permission.SEND" android:enabled="false" android:exported="true" tools:node="merge"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> </intent-filter> </receiver>

was the part that you added to the manifest. Can you check the manifest of the apk you’re testing and see how the FirebaseInstanceIdReceiver is being declared there?

https://firebase.google.com/support/troubleshooter/fcm/delivery/diagnose/android

If you’re able to view the FCM Diagnostics, you should be able to see from the logs whether the broadcast was delivered to the app.

Messages are sent from Google Play services.

0reactions
google-oss-botcommented, Apr 15, 2021

Since there haven’t been any recent updates here, I am going to close this issue.

@abhinavchdhry if you’re still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

android - Initializing Firebase in a different process (in multi ...
I'm trying to offload Firebase messaging into a standalone process in my app, different from the main app process. I've read this blog...
Read more >
Troubleshoot initialization options | Firebase - Google
If new users of your app are experiencing issues with FCM, it's possible that you are initializing Firebase without the required set of...
Read more >
FirebaseMessaging does not support senderId or secondary ...
FirebaseMessaging does not support senderId or secondary app initialization, as the documentation suggests #3987.
Read more >
Initializing Firebase in a different process (in multi-process ...
I'm trying to offload Firebase messaging into a standalone process in my app, different from the main app process.
Read more >
Custom WorkManager Configuration and Initialization
If you don't use App Startup in your app, you can remove it completely. <!-- If you want to disable android.startup completely. -->...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found