Firebase messaging does not work when initialized in a separate Android process
See original GitHub issueI’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:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top GitHub Comments
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.
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.