theAnonymous
theAnonymous

Reputation: 1804

Firebase Service Lifecycle

When Firebase Service receives a RemoteMessage, and the app has previously been killed by swiping it away, what must I do so that the app will still process the message?

It's a data message, so the behaviour is to process and not put to the GUI as a notification.

Upvotes: 3

Views: 1551

Answers (4)

mhenryk
mhenryk

Reputation: 551

EDIT Below solution works in old Google Play Services (i.e. 11.0.2), newer ones like (11.6.0) have handleIntent declared as final making this solution no longer valid.


You have to override handleIntent(Intent) without calling super.handleIntent(Intent) in it. The problem with this approach is that the RemoteMessage object was not created. I personally create it myself like this:

RemoteMessage message = new RemoteMessage.Builder(FirebaseInstanceId.getInstance().getToken())

and pass the data into the map and then to onMessageReceived(RemoteMessage) myself (this is obviously optional, you don't need that object at all).

This is obviously not the best approach but it was the only way I found that allowed me to build the notification on my own or ignore it completely.

Upvotes: 1

Manishoaham
Manishoaham

Reputation: 802

When a firebase message arrives, android OS ( precisely perhaps google services) broadcasts MESSAGING_EVENT intent.

<action android:name="com.google.firebase.MESSAGING_EVENT"/>

You can implement a broadcast receiver having intent-filter filtering for above intent and in it's onReceive() method, your appropriate service should be started (if not running). Only broadcast receivers have capability to respond irrespective of its application's running status.

Upvotes: 1

vikytech
vikytech

Reputation: 1

You can put the task in the onDestroy method to process the message in background as an async service.

Your app's async service will always be running in background by listening to BroadcastReceiver and then once task is done you can finish the activity to kill the app

Upvotes: 0

Feng Lanmao
Feng Lanmao

Reputation: 1

You may register a background daemon service, but i think it will be terminated too when user swiping it away, so basically there is no way to do that, you should handle this as if it's a Exception.

Upvotes: 0

Related Questions