Android device: Nexus 5 or emulator
Android OS version: Android N or Android M
Google Play Services version: 9.2.1
Firebase/Play Services SDK version: 9.2.1
Steps to reproduce:
Using the Firebase console (or via the REST API), send a notification with custom data (for e.g. key "discount" value "10" )
app is in background or not running
Observed Results:
though, the notification does appear in the system tray, the data is NOT available in the intent... either in onNewIntent or in onCreate, the extras is null.
Expected Results:
According to the Firebase Notifications docs, when App State is Background and a Notification with data is sent, the notification appears in the system try and the data will be available in the extras of the intent.
@Override
public void onCreatet() {
Bundle extras = getintent().getExtras();
// ---> extras is always null when app is not running or when app is in background
}
@Override
public void onNewIntent(Intent intent) {
Bundle extras = intent.getExtras();
// ---> extras is always null when app is not running or when app is in background
}
Remarks: The data is available when App State is in the Foreground. But the notification console is pretty much unusable with this bug because ALL (background, app not running, foreground) users need to be able to get the data in a notification. Otherwise, how does a developer know what data a user saw or didn't see (e.g. whether a user was shown the discount 10% or not).
Hi @ashishparmar please use the latest version of the SDK 10.0.1. The custom data would only be in the extras if the intent is launched as a result of the user tapping the displayed notification. Could you confirm that the user is tapping the notification to launch the intent?
@kroikie I'm having a similar problem, I'm using the SDK 10.0.1 and sending a notification with both notification and data. When I receive the notification in background, I tap the notification to launch the intent and the intent extras is null.
@kroikie I'm also having a similar issue while using the SDK 10.0.1. The notification and data is sent through the Firebase web console. When the notification is tapped while the app is in background, but not killed, the app is re-opened normally and the intent extras doesn't have the payload. The FirebaseMessagingService onMessageReceived() was never called.
Hi @kroikie yes i have try but as experienced by @amarantedaniel same i have. so can you please suggest a demo example for handle fcm notification so it is easy to understand.
Thnks in advance
Same issue here.
SDK: 10.0.1
Device: Google Pixel - Android 7.1.1
When the app is running (Activity in foreground) I can read the extras from the intent, but as soon as the app is in background or removed from the app switcher and the user taps on a notification the intent won't have any of my custom extras.
I create my notification like so (written in Kotlin):
/**
* Create and show a simple notification containing the received FCM message.
* @param messageBody FCM message body received.
*/
private fun sendNotification(title: String, messageBody: String, payload: Map<String, String>, topic: String?) {
val intent = Intent(this, ActivityStart::class.java)
intent.putExtra(KEY_TITLE, title)
intent.putExtra(KEY_BODY, messageBody)
(try { JSONObject(payload) } catch (e: Exception) { null })?.let {
intent.putExtra(KEY_PAYLOAD, it.toString())
}
topic?.let { intent.putExtra(KEY_TOPIC, it) }
intent.action = UUID.randomUUID().toString()
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) // Intent.FLAG_ACTIVITY_SINGLE_TOP or
val pendingIntent = PendingIntent.getActivity(this, Random().nextInt(), intent,
PendingIntent.FLAG_ONE_SHOT)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(Random().nextInt(), notificationBuilder.build())
}
Any solution for this? I have same issue, I get data when app is killed thou, but not when in background
I got this problem too, any solution for this guys ?
same thing to me, any solution ?
I'm solved it. no problem.~~
나의 SOL에서 보냄
—You are receiving this because you are subscribed to this thread.Reply to this email directly, view it on GitHub, or mute the thread.
@jogilsang , how did you solve it ? Could you share it ?
Thanks advance
@jogilsang could you please give us how to solve it ?
thks !
Sorry. I dont`t remember that.
Because, my app is startversion. many problem is accured and i solveed that.
Recently my issues are belong.
m startup developer. im actually dont' know proguard. so,Thank you for feedback :)
—You are receiving this because you were mentioned.Reply to this email directly, view it on GitHub, or mute the thread.
@kroikie
Excuse me, I have the same issue.
Firebase sdk is the newest one - 10.2.4.
Btw, the launch mode of my activity is singletask.
If I removed the launch mode of singleTask, extras could be fetched.
Thanks a lot.
Hi All,
Data being passed to the intent launched as a result of tapping the notification only applies to notification messages when the app is in the background. If you are handling the incoming message in onMessageReceived then you will have to extract the data and pass it to the intended intent yourself.
The launch mode of the intent should not affect the passing of the data to it.
Could someone confirm that they are seeing this issue with notifications generated by the SDK and not notifications that are generated by your app?
Thanks.
@kroikie @ashishparmar @amarantedaniel @wilsondezapp @Querschlag
I am also getting the same problem even after generating the notification from Firebase console(Notifications) and Insomnia also. Anyone have something to solve it.
Thanks in advance
Hello,
I did not found how to solve it using firebase console. But to go around this, i used to send notification with the API (PHP). Exemple of script here: https://gist.github.com/MohammadaliMirhamed/7384b741a5c979eb13633dc6ea1269ce
Hope that helps you!
@MohitTest1 thanks for confirming, could you let me know what device you are seeing this with? Also could you try with an emulator and let me know if you are seeing the same?
@kroikie
Thanks for reply.
I got the solution.
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("Msg", "Message received ["+remoteMessage+"]");
//Toast.makeText(this, "onDeletedMessages1", Toast.LENGTH_LONG).show();
// Create Notification
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("body",remoteMessage.getNotification().getBody());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1410, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle(remoteMessage.getNotification().getTitle()).setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1410, notificationBuilder.build());
}
@Override
public void handleIntent(Intent intent) {
super.handleIntent(intent);
Intent intent1 = new Intent(this, MainActivity.class);
Bundle extras = intent.getExtras();
String body = extras.getString("gcm.notification.body");
String Nick = extras.getString("Nick");
intent1.putExtra("body",body);
SharedPreferences.Editor editor = getSharedPreferences("MY_PREFS_NAME", MODE_PRIVATE).edit();
editor.putString("body", body);
editor.commit();
//startActivity(intent1);
Log.d("handleIntent", "handleIntent "+body);
}
Need to override handleIntent() method which will give you all the payload and notification.
Glad you found a solution.
For others using this approach, generating the notification yourself means
you are responsible for passing data to the launched intent. The automatic
passing of data from the FCM data payload only happens when the
notification is automatically generated by the SDK.
On Sat, Jul 1, 2017, 11:59 AM MohitTest1 notifications@github.com wrote:
@kroikie https://github.com/kroikie
Thanks for reply.
I got the solution.
@override https://github.com/override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);Log.d("Msg", "Message received ["+remoteMessage+"]"); //Toast.makeText(this, "onDeletedMessages1", Toast.LENGTH_LONG).show(); // Create Notification Intent intent = new Intent(this, MainActivity.class); intent.putExtra("body",remoteMessage.getNotification().getBody()); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 1410, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_stat_name) .setContentTitle(remoteMessage.getNotification().getTitle()).setContentText(remoteMessage.getNotification().getBody()) .setAutoCancel(true) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1410, notificationBuilder.build());}
@override https://github.com/override
public void handleIntent(Intent intent) {
super.handleIntent(intent);
Intent intent1 = new Intent(this, MainActivity.class);
Bundle extras = intent.getExtras();
String body = extras.getString("gcm.notification.body");
String Nick = extras.getString("Nick");
intent1.putExtra("body",body);
SharedPreferences.Editor editor = getSharedPreferences("MY_PREFS_NAME",
MODE_PRIVATE).edit();
editor.putString("body", body);
editor.commit();
//startActivity(intent1);
Log.d("handleIntent", "handleIntent "+body);
}Need to override handleIntent() method which will give you all the payload
and notification.—
You are receiving this because you were mentioned.Reply to this email directly, view it on GitHub
https://github.com/firebase/quickstart-android/issues/205#issuecomment-312449775,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACQqmLnlpW2NAtXlBjVVU8FSmIhudXxyks5sJpcggaJpZM4LfV2t
.
@MohitTest1 Thanks for the solution!
But how to know it is notification be clicked or just the logo?
I handle the notification strings in MainActivity's onResume() and in this way the "handleNotification" function will called every time MainActivity start, which is wrong. :(
@kroikie as @pollyfat mentions, the workaround proposed by @MohitTest1 will result in weird behaviours by the app, if the user taps the notification or the launcher (app icon) we will have the information saved on Shared Preferences and as far as I've been researching there is no clear way to check if the user entered from notification or from app icon.
I thinks the thread should not be closed, the problem is still active.
This issue should not be closed. Data is not being sent in the launcher intent when a user taps the notification. That workaround is not a solution.
EDIT:
There is no bug after all. It was a very stupid mistake from my part. Sorry.
Any solution please !!! If messages contain both notification and data payload ,when I receive the notification in background, I tap the notification to launch the intent and the intent extras is null.
@kroikie, I am also facing this issue and I ran all possible scenarios and observed that it's not the App state (i.e. Background or Killed) at the TIME OF TAP that causing the issue but it's App state (i.e. Background or Killed) at the TIME OF NOTIFICATION CREATION that is causing this issue. Find this graph below:

send DATA payload to fcm endpoint.
like,
{
"to" : "deviceid...",
"data" : {
"Title" : "Notification title",
"body" : "Notification body"
},
}
If app is in Foreground you can receive the "Notification" as well as "Data" in "onMessageReceived"
and there you can create PendingIntent including the click action.
But if the app is running in background the "Notification" is delivered to SystemTray but the "Data" is delivered to "Intent Extra" if you deliver Notification and Data both at same time. So instead in you "onMessageRecieved" method, what you need to do is. In you Activity where you are redirecting users to when they click notification.
Just create :
String data_message = getIntent().getStringExtra("data-key");
Replace "data-key" with the key of data.
Say for example :
const payload = {
notification: {
title : "Notification Title",
body: "Notification Message",
icon: "default",
click_action: "TARGETNOTIFICATION"
},
data: {
message : "Notification Message"
}
};
If you want detailed explaination, you can checkout :
https://www.youtube.com/playlist?list=PLGCjwl1RrtcRHjHyZAxm_Mq4qvtnundo0
you can catch your intent extra by implementing :
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.getParcelableExtra(Notification.Extras)!=null)
{
LogUtility.debug("GOT EXTRA");
}
}
@soufianeEssabbane solutions works nicely
I found a solution that worked for me: https://github.com/firebase/quickstart-android/issues/822#issuecomment-507231930
Use Bundle instead of Intent to pass data. This will solve the problem.
I had this same issue and I solved it by changing json payload
{
"to":"xxxxx:...",
"data":{
"title":"Title",
"body":"Body",
"click_action":"MyActivity",
"item_id":"123"
}
}
I had the same issue, solved it by below code
Intent notificationIntent = new Intent(getApplicationContext(), viewmessage.class);
notificationIntent.putExtra("NotificationMessage", notificationMessage);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(getApplicationContext(),notificationIndex,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(getApplicationContext(), notificationTitle, notificationMessage, pendingNotificationIntent);
In the above code using
Intent flags Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP,
and Pending intent flag as PendingIntent.FLAG_UPDATE_CURRENT .
Then we can get the intent data in Activity onNewItentmethod.
For me it's solved by using the below code that Bundle data is not available in the onResume() method.
@Override
protected void onNewIntent(Intent objIntent) {
super.onNewIntent(getIntent());
Log.e(TAG, "onNewIntent() called. neww intenet");
Log.e(TAG, objIntent.getExtras().size() + " total nodes");
Bundle extras = null;
if (objIntent != null && objIntent.getExtras() != null && objIntent.getExtras().size() > 0) {
extras = objIntent.getExtras();
Log.i(TAG, extras.getString("action"));
Log.i(TAG, extras.getString("message"));
Log.i(TAG, extras.getString("clickAction"));
Log.i(TAG, extras.getString("module"));
Log.i(TAG, extras.getString("mainMenu"));
Log.i(TAG, extras.getString("subMenu"));
}
}
Most helpful comment
@kroikie I'm having a similar problem, I'm using the SDK 10.0.1 and sending a notification with both
notificationanddata. When I receive the notification in background, I tap the notification to launch the intent and the intent extras is null.