I want to update incoming unread messages badge count on app's icon if device receives push notification from fcm and app is in killed state. I am able to do so if app is in background but not able to do so if app is in killed state. If i receives fcm push notification then it is shown only in notification tray but some how i want to pass or communicate with the app that is killed. So is there any solution for this & how can this be achieved. Thanks, Roshan
1 Answers
yes , please refer https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
and note that , notification mesaages (app in killed state) are handled by android system
but , if the notification received is a data payload notification , then it is handed over to the app's onMessageRecieved method of the FCMlistener, so change the type of notification you send to your device from a notification message to data message, Once you extract your message from the data message , you can then build a notification there to display or run any process or method even if the app was in killed state
public class MyFcmListenerService extends FirebaseMessagingService {
String TAG = "FCM";
@Override
public void onMessageReceived(RemoteMessage message){
String from = message.getFrom();
Map data = message.getData();
Log.e(TAG, "From: " + from);
Log.e(TAG, data+"Notification Message To : " + data.get("name") +"\n body:"+data.get("body")+"\n time to live:"+data.get("time_to_live"));
sendNotification( data);
}
create this class in your app , in MyFcmListenerService.java and use data message as your fcm notification ,it would work as you need

- 990
- 6
- 12
-
I think you misunderstood the *killed* state from *background* state. There is nothing mentioned on the docs you provided about handling notifications in *killed* state. – AL. Oct 25 '16 at 13:32
-
it says in second para itself of the doc About FCM Messages Message types With FCM, you can send two types of messages to clients: Notification messages, sometimes thought of as "display messages." Data messages, which are handled by the client app. – Ak9637 Oct 25 '16 at 13:59
-
" handeld by the client app " means android won't interfere and simply give the message to app , no matter what state it is in . – Ak9637 Oct 25 '16 at 14:02
-
Yep. That's the behavior for when it's on **background**. Not when it's **killed**. This [documentation](https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages) shows it. – AL. Oct 25 '16 at 14:21
-
Assuming that your app is in killed state(i.e not loaded in memory) ,the fcm data message contains your package name , so it will look for that package and then deliver it to the app,without waking up the app,you also need google play services to be inculed in your build.gradle – Ak9637 Oct 25 '16 at 14:23
-
I had the same issue with my app too, change the notification from a notification message to data message solved the issue – Ak9637 Oct 25 '16 at 14:24
-
I get what you're saying. It's the same scenario said by @tomacco in the comments section [here](http://stackoverflow.com/a/39505298/4625829). It's hard to say what may actually happen when an app is in killed state for Android. Some devices tend to totally restrict services related to the killed app. Anyways, I just don't think that docs you provided corresponds to your answer. If you can find a more suitable doc or a similar good post as reference, I'll re-vote. – AL. Oct 25 '16 at 14:40
-
i can not say about any restriction on any particular OEM , but for most devices , the Firebase service is never restricted as its a part of google play services,for my app its perfectly working. – Ak9637 Oct 25 '16 at 14:52
-
1and i agree with you @AL,since android is an open platform ,various OEMs provide custom setting and restriction that may hinder working of FCM ,i would say that for most OEM my FCM notifications are getting delivered to clients of my apps, if you find a better solution or more convincing document somewhere , it would be great if you share that here , my app totally relies on FCM. – Ak9637 Oct 25 '16 at 15:01