I am struggling with FCM notifications on Android. The issue is only when the app is closed and not running. I am trying to achieve no click activity I don\'t want the app
One way is to use data
instead of notification
to get onMessageReceived() callback both in app foreground or background. The json part is:
{
"data": {
"title": "notification_title",
"body": "notification_body"
},
"to" : "jh578_gsh....jhHGFJ76FH"
}
For detail, follow this link: https://stackoverflow.com/a/37845174/5829624
Edit:
Method 2:
Use a click_action payload in your notification payload. In json:
{
"notification": {
"click_action": "OPEN_ACTIVITY_1",
},
}
And add this intent filter in the Activity (which you want to load when you tap on the notification) in your Manifest:
<intent-filter>
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Hope this one will solve your problem.
When application in background "OnMessageReceived" overridden method wont get called in general scenario.
If you want to make "OnMessageReceived" method to call all the time. Ask your web service team to send only "DATA" payload. Dont use "Notify" or "Notification" payload.
If "DATA" payload is send properly from backend then Android application will work on "OnMessageReceived" with a List of Data from RemoteMessage.getDATA();
I struggled the same issue for nearly 2 days and finally got the above solution.
The notification is getting disappeared when you click on it because you did not add any action when onClick
is performed.
To give onClick
functionality on notification, you have to use PendingIntent
with the target activity. You can also add additional data into it if needed.
Before initializing notificationManager
, add this code:
Intent openIntent = new Intent(mContext, TargetActivity.class);
// Falg to clear the stack.
openIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,
openIntent, 0);
notificationBuilder.setContentIntent(pendingIntent);
FCM displays my notification the way it wants to when the app is closed. That's ok with me. I just want to make sure the entire notification gets read and not lost. Here is my solution... When an app user clicks on the notification it opens the app and displays the notification in an alert Dialog.
c# server-side code to send the message
var msg2send = new
{
to = deviceId,
priority = 10,
notification = new
{
body = msg,
title = "Red Hill Car Wash",
icon = "myicon"
},
data = new
{
notice = msg
}
};
And in the onCreate of MainActivity
String notice = "";
try {
notice = getIntent().getStringExtra("notice");
if (notice != null)
{
Log.i("myStuff", notice);
///// DISPLAY NOTIFICATION IN AN ALERT DIAGLOG all good!
}
}
catch (Exception ex) {
Log.i("myStuff", ex.getMessage());
}
FCM passes the message to my MainActivity, if a message exists then I grab it and can display it how ever I want. Thank for the comments and input to help solve this issue.