quickstart-android: onMessageRecieved not working when app is killed.
onMessageRecieved not working when app is killed. It works perfect in foreground and in background and I can receive Extras in MainActivity. But when app is killed I receive notification and after click on it- Extras == null.
I send only DATA (to, data) notification. What I do wrong?
` @override public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> map = remoteMessage.getData();
String message="";
for (Map.Entry<String, String> entry : map.entrySet()) {
message= entry.getValue();
}
sendNotification(message);
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("notification",messageBody);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_name);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notifications_active_black_24dp)
.setContentTitle("MyTitle")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
} `
and I’ve tried to use different flags in intent and pending intent and it not works… (I can’t catch intent.putExtra(“notification”,messageBody); in MainActivity when app is closed.)
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 137 (10 by maintainers)
Hello guys I solved this inserting this lines in Manifest.xml
My app received notifications when is foregorund, background and is closed.
Hi @DanikKamilov and @carlosalexandresmo FCM does not process messages if an app is “killed” or force stopped. When a user kills an app it is an indication that the user does not want the app running so that app should not run till the user explicitly starts it again.
Note that swiping an app from recents list should NOT “kill” or force stop it. If messages are not being received after swiping from recents list then please identify these devices and we will work with the manufactures to correct this behaviour.
Only being able to handle messages when the app is in the foreground or background is working as intended.
I don’t think @fcm guys will ever fix this issue. I am complaining about this from the beginning, but instead of fixing it, they only blame it on vendors. The whole FCM team should be sacked for their pathetic development and ignoring developer issues.
Maybe my words are too harsh but I could not control myself anymore.
@Adityavns try adding these two permissions also in manifest
"uses-permission android:name=“com.google.android.c2dm.permission.RECEIVE” "uses-permission android:name=“android.permission.WAKE_LOCK”
I also have oneplus 3 mobile and i was having same issues ,but after adding the permissions my issue got resolved.
@FCM guys: There’s a major architectural issue with the way push notifications work on android. I just do not understand why some part of the app is required to be running in background to receive and display the message. Can’t the FCM service itself do that when the app isn’t alive.
APNs on iOS does exactly that and it works great. It doesn’t matter if the device was rebooted / app swiped / crashed / terminated. It works flawlessly if the user has permitted app to show notifications.
AFAIK FCM service is always alive on every phone that has google play services installed. Why can’t it do the same thing.
In my country these days around 80% users own phones manufactured by Chinese vendors and it is impossible to use push notifications with acceptable performance of message delivery, FCM / anything is just not an option here. Plain old SMS are very handy here but expensive.
FCM guys blame it on vendors and these vendors don’t care about small app / developers. I wonder how on earth did this happen to world’s most popular os.
It is really very sad 😦
Hi @kroikie on messageReceived is not getting called for the below FCM when the app has been removed from recents by swiping on devices manufactured by VIVO, ONE PLUS. { “to”:“erWZDlJg9Fo:APA91bFUe_fc6X1kzg7bmZTool7dpBp41AcSPPBEpQVPUihzYR9Q1uBVlUcCkmqj5bs4ObgcgfPjuGCDIiU22DMUxVSArj0aD91WBFMs591To9ge0oIKbCvSuii9WoPFCk2fhC8KeGSD”,“priority”:“high”,“data”:{“message”:“Some Message”} } Any help is appreciated. Thanks in advance.
PLease
Solution plz sir?..
No. Google developers closing issue without solution on there own OS.
even though opmization is enabled apps like whatsapp recieve message when in background and after being swiped out. Any solution for an app targeting oreo ?
@kroikie why this restrictions implemented first place. If user allowing push notifications settings than blocking notifications by FCM is stupid decision. I am very frustrated to see no solution from past 2 years. APNs notifications works flawlessly and my users just complain about not seeing notifications at critical time. Can we please get at least ETA on the fix?
@kimnamcham This is not solution. There are apps like Whatsapp, iMobile, Facebook,etc which is set to “optimise” in Battery Optimisation Setting but still it gets notification without missing any.
@samtstern @kroikie It can’t be intended behaviour if other apps in background still gets notifications. Something bug with firebase push notification sdk.
I tested with firebase 4.5 package using unity. Not working on Oneplus 5.
Any solutions for infinix or different chinese devices?
Been 2 years, any update? Please fix this, my year end bonus depends on this 😃
Try this solution: When app is killed, you won’t get data from “notification” object means (remoteMessage.getNofication()== null).
Try to send a custom notification which means do not send “nofication” object in notification and you will get a Push notification when an app is killed.
Example:
{ “to”: “token”, “collapse_key”: “type_a”, “data”: { “custom_key”: “value”, “body”: “Body of Your Notification”, “title”: “Title of Your Notification” } }
@rameshvoltella outside has 2 meanings.
Managed Applications
in settings. Or on some devices swiping the app from the recents menu.In 1. case we deliver the notification. In 2. case we don’t (should not).
From Android docs:
We do not add the
FLAG_INCLUDE_STOPPED_PACKAGES
flag since we are not sure why the app has been stopped. Thus we don’t deliver messages to stopped applications.List of devices: OnePlus, Oppo, Lenovo
@Adityavns try this one, go to settings in battery>> battery optimization>>select your app>>you will see that your app is optimised>>> click on dont optimise>> then try pushing notificaiton… hope this helps. PS:- I then cliked on optimise app… now I am getting notifications
Please let me know if this works…
Regards Rajesh Jakhar
@All, this issue has been raised here and in many other places. The FCM team is aware of the issue and is working on it. Thanks for the feedback.
@Adityavns It is because of DOZE mode and battery optimisation,you just have to turn off battery optimisation for all apps or particular app. Enjoy 😃 😃
Regards Rajesh Jakhar
If someone is looking for a detailed explanation please visit: https://github.com/shahzadafridi/FCM-Notification-Test-Cases I am sure this will helps you.
@fcm Guys Are you really going to provide solution? Its been around 2 years and you have not fixed this in a single Device there are too many manufacturers(Oppo, VIVO, One Plus, Samsung, Honor, Huawei, NOKIA, LENOVO, REDMI) doing it, they dont think about small developers and kill apps whenever they go in the background(Even not swiping app from recent app list) It is so critical issue for every android app developer
We’re facing issue with Oppo & Vivo Phones. Our application is a VoIP Application and push delivery is critical for us to show the incoming call screen on the receiver end. On the Oppo and Vivo phone just removing the app from recent application kills the app completely and render our app useless. The caller gets frustrated. It’s created a lot of problems for our users.
It’s been a year since this thread was opened. The affected devices share about 40% of our customer base. It’s hurting our business. I hope you guys find a solution fast ( 1 year is a very long time for solving a critical issue like this ). Looking forward to the update.
@kimnamcham It is because of DOZE mode and battery optimization,you just have to turn off battery optimization for all apps or particular app. Go to Settings>> apps >> select your app>> battery>> battery optimization> select your app>> select don’t optimise. Problem solved.
I have OnePlus 3 and I had the same issue . I did this and my problem was solved. battery optimization blocks incoming notifications from apps so that the wont kill battery.
Thanks Regards
https://dontkillmyapp.com/ it says what needs to be done to make the pushies work. only there is one but !! the user must do it! this cannot be done programmatically!
@michaelr524 likely they have partnership with the manufacturers so that their app gets priority and isn’t killed by the system. Even WhatsApp can’t receive messages if it has been killed by the user.
Everyday i am watching this page any solution provided by FCM. Totally disappointed move to other solutions. any one help me?
As @kroikie said this is intended behavior, although we are still interested to know when developers run into it. If you find devices that you think behave strangely please continue to tell us.
I am going to close this issue as there’s nothing we can do to “fix” it besides continuing to discuss new places you find it,
@kroikie Are you guys keeping a running list of devices with this problem, and if so can you make it public?
@satyajiit my message is
data
only already. Strangely! It works inrelease
builds on Pixel 3Tested on a production app, over 100+ devices. Just don’t Pass the “Notification” object . Issue arises when Passed the “Notification” object. ( I know this is strange 😷)
What I am doing? Sending notification to devices group/id , on new document (creation) trigger of Firestore.
check this cloud function https://github.com/satyajiit/PWA_TO_NATIVE_ANDROID_APP/blob/master/Cloud_Functions_For_FCM/functions/index.js
FirebaseService
https://github.com/satyajiit/PWA_TO_NATIVE_ANDROID_APP/blob/master/app/src/main/java/com/argonlabs/satyajit/FirebaseService.java
in manifest
Don’t forget to subscribe topic “all” , for the above code snip.
This is what working for me. I was having the same issue earlier.
Of course, we tried
you should give developers some solution around this, no one to blame but google. You decide wrong behaviors and your documentation is a mess, I’ve been reading a lot to understand why it works only on some devices and get only this ugly response.
@pembeci correct
@kroikie I have try
shell pm dump com.kliklabs.chat | grep stopped
, on VIVO and OPPO manufacturer, I got responsestopped=true
although I only swipe up the recent apps list. ThanksNobody can solve this issue right now. maybe need to fix packages
Hi @NidhinVasudev no update on this issue yet, but we are working on a solution. Thanks for providing these device OEMs.
Same issues with asus a007 zenphone!!