0

I am sending Data only messages to my FirebaseMessaging receiver, but It stops receiving messages when the app is killed(swiped from the tray). I've been looking at ways to make it always listen as Whatsapp does. According to this answer, I have to make it start in sticky mode. How I start my service now is by simply adding it to the manifest.

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

How can I make this start in Sticky Mode instead?

1 Answers1

1

The short answer is that you can't. The answer you found was from 8 years ago, and has nothing to do with Firebase -- it just refers to starting a regular service as sticky. Firebase has its own service as a part of that library, and you can't change how it starts.

Your second issue is that Google has systematically removed the ability of apps to always run in the background over the past couple of API versions, although there a few hacky workarounds, as described here.

And finally, they explicitly don't want an app to be able to "wake up" if the user intentionally shut it down (like swiping it away from the recent apps list).

Having said all that, you don't actually need to have your app running in the background. FCM notifications are designed specifically to handle these kinds of scenarios. I'm guessing that you're just using them incorrectly. You need to set up FCM notifications to be shown to the user (and not be hidden), and when the user opens that notification, it'll "wake up" your app. In your Manifest, add the following to your Application:

<application
    ...
        <meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/notification_channel_name" />
    ...
</application>

You'll also need to create a dedicated notifications channel, and in my example, @string/notification_channel_name refers to it.

user496854
  • 6,461
  • 10
  • 47
  • 84
  • Thanks for the clarification! What I'm doing is starting an intent when I receive the push notification. I don't want to show the notification but instead wake up the app without user interaction. you can see my Handler code [here](https://stackoverflow.com/questions/73570643/flutter-app-stops-receiving-fcm-messages-after-a-certain-time-when-the-app-is-ki) – Ashen Jayawardena Sep 07 '22 at 18:25
  • And Google has specifically declared their intent to stop that kind of behavior. That's why all they pretty much killed most old ways of running things in the background. They do not want your app to be active, unless a user specifically chooses to do so – user496854 Sep 08 '22 at 02:22