Search code examples
androidkotlinfirebase-cloud-messagingandroid-14

Android 14 - Foreground service type for FirebaseMessagingService


When targeting android 14, we should provide at least one foregroundServiceType for each service in manifest - https://developer.android.com/about/versions/14/behavior-changes-14#fgs-types

Which foregroundServiceType should be used for FirebaseMessagingService?

Should I use this https://developer.android.com/about/versions/14/changes/fgs-types-required#remote-messaging ?


Solution

  • FirebaseMessagingService by itself is not a foreground service. It operates in the background and posts a notification whenever it receives one, but it does not do any foreground work.

    The Android 14 changes state that "If your app targets Android 14, it must specify at least one foreground service type for each foreground service within your app." Since it's not a foreground service adding a foregroundServiceType should not be required.

    I tested this with my own app by sending both foreground and background test messages on Android 14 (target SDK 34) and no MissingForegroundServiceTypeException was thrown.

    Furthermore the messaging documentation does not include foregroundServiceType and I was not able to find any issues related to the topic in the Firebase Github project.

    Important Note:

    The exception to this would be if you do use your FirebaseMessagingService explicitly to do long foreground work and it lives longer than it should (which you probably shouldn't do), then an appropriate foregroundServiceType should be required and you should be seeing a MissingForegroundServiceTypeException in your logs.

    If you're having difficulties with setting up FCM, this example might help.