Search code examples
androidkotlinfirebase-cloud-messaging

Are there restrictions with FirebaseMessagingService in Android 10+?


I noticed some crashes recently on Android 10 devices

java.lang.RuntimeException : Unable to start activity ComponentInfo{com.acme.MainActivity}: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.acme/com.google.firebase.messaging.FirebaseMessagingService }: app is in background}

To receive FCM messages, I use a service that extends FirebaseMessagingService and I start the service from MainActvity using

Intent(this, MyFirebaseMessagingService::class.java).also { 
        intent -> startService(intent)
}

of course I have this in the manifest file

<service
        android:name="com.acme.services.MyFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
</service>

With that all in place I am trying to determine how this crash happens intermittently on startup for Android 10 and/or how can I motivate it to manifest itself.


Solution

  • FCM does't need to start, when you intergrate FCM into your app, register it in manifest, then you can receive FCM

    Read the docs carefully: https://firebase.google.com/docs/cloud-messaging/android/client