quickstart-android icon indicating copy to clipboard operation
quickstart-android copied to clipboard

onMessageRecieved not working when app is killed.

Open DanikKamilov opened this issue 9 years ago • 137 comments

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.)

DanikKamilov avatar Nov 04 '17 18:11 DanikKamilov

Now (for APP IS CLOSED case) I write Notification text to file and read this text if extras == null and notificationText.txt is exists... its stupid solution but it works. How can I catch this extras when app is closed in other way?((

DanikKamilov avatar Nov 05 '17 12:11 DanikKamilov

Hello guys I solved this inserting this lines in Manifest.xml

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
 <uses-permission android:name="android.permission.WAKE_LOCK" />

My app received notifications when is foregorund, background and is closed.

carlosalexandresmo avatar Nov 07 '17 00:11 carlosalexandresmo

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.

kroikie avatar Nov 10 '17 19:11 kroikie

@kroikie Are you guys keeping a running list of devices with this problem, and if so can you make it public?

opus1269 avatar Nov 11 '17 05:11 opus1269

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

Adityavns avatar Dec 05 '17 14:12 Adityavns

@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.

rajeshjakhar1092 avatar Dec 06 '17 11:12 rajeshjakhar1092

Hi Raj,

Thanks for the reply! It is still not working in Vivo and one plus 5 phones that i have tested.

Regards, Aditya Vns

On Wed, Dec 6, 2017 at 5:06 PM, rajeshjakhar1092 [email protected] wrote:

@Adityavns https://github.com/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.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/firebase/quickstart-android/issues/368#issuecomment-349613753, or mute the thread https://github.com/notifications/unsubscribe-auth/AGEyDnSee13lT7lsO7A_fZJi429_m6a0ks5s9nxTgaJpZM4QSHEZ .

-- aditya369

Adityavns avatar Dec 06 '17 14:12 Adityavns

@Adityavns yeah you are right ,actually i am now again facing the same issue, I still not able to figure out what is the main issue, actually I have a app in which i have implemented the same code and there I am getting notifications all the time even app is killed... but in another app it is not working.. I will let you know if I get a solution :)

Regards Rajesh Jakhar

rajeshjakhar1092 avatar Dec 07 '17 05:12 rajeshjakhar1092

@Adityavns please let me know also if you find any solution.

Regards, Rajesh Jakhar

rajeshjakhar1092 avatar Dec 07 '17 06:12 rajeshjakhar1092

@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

rajeshjakhar1092 avatar Dec 07 '17 06:12 rajeshjakhar1092

@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

rajeshjakhar1092 avatar Dec 07 '17 07:12 rajeshjakhar1092

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,

samtstern avatar Feb 07 '18 20:02 samtstern

List of devices: OnePlus, Oppo, Lenovo

skfaisal93 avatar Feb 11 '18 23:02 skfaisal93

Same issues with asus a007 zenphone!!

kimnamcham avatar Mar 20 '18 03:03 kimnamcham

@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

rajeshjakhar1092 avatar Mar 23 '18 06:03 rajeshjakhar1092

@rajeshjakhar1092 We all know this. You should read the whole thread.

skfaisal93 avatar Mar 23 '18 08:03 skfaisal93

@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.

aldo-abvr avatar Mar 28 '18 04:03 aldo-abvr

how to handle notification when app in kill in firebase android

saprahits avatar Apr 07 '18 05:04 saprahits

is this issue resolved?

manishbutola avatar Apr 23 '18 07:04 manishbutola

No. Google developers closing issue without solution on there own OS.

aldo-abvr avatar Apr 23 '18 07:04 aldo-abvr