Reputation: 1517
I'm using this code, when the app is in the foreground it was working perfectly. but the notification coming during app is in background Intent does not have any data.
Intent intent = new Intent(this, SplashActivity.class);
intent.putExtra(Constant.device_id,deviceId);
intent.putExtra(Constant.isFromPush,true);
intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title_)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
getString(R.string.app_name),
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(new Random().nextInt( 2000 ) /* ID of notification */, notificationBuilder.build());
Upvotes: 2
Views: 4398
Reputation: 350
We have two type of Payload in case of send-downstream both are optional.
data
This parameter specifies the custom key-value pairs of the message's payload.
notification
This parameter specifies the predefined, user-visible key-value pairs of the notification payload.
[https://firebase.google.com/docs/cloud-messaging/http-server-ref#send-downstream][Find More detail here]
When you are in background, FCM will showing notification in system tray based on the info from notification payload. Title, message, and icon which used for the notification on system tray are get from the notification payload.
{
"notification": {
"title" : "title",
"body" : "body text",
"icon" : "ic_notification",
"click_action" : "OPEN_ACTIVITY_1"
}
}
You need to use data payload instead of notification payload ,your issue get resolved .
Here is the example JSON i'm reciving :
{
"to": "FCM registration ID",
"data": {
"someData" : "This is some data",
"someData2" : "etc"
}
}
Here is my java code.
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage == null)
return;
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
try {
JSONObject json = new
JSONObject(remoteMessage.getData().toString());
handleDataMessage(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}
Messages with both notification and data payload:
A message can also contains both notification and data payload. When these kind of messages are sent, it will be handled in two scenarios depending upon app state (background / foreground). For these message we can use both notification and data keys.
When in the background – Apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.
When in the foreground – App receives a message object with both payloads available.
Upvotes: 1
Reputation: 2211
To Handle intent data when app is in background you need to do something extra. There should have "data" key in your response to get it in activity. Like,
{
"notification": {
"key_1": "value_1",
"key_2": "value_2"
},
"data": {
"key_1": "value_1",
"key_2": "value_2"
},
}
And you need to get the values inside your launching activity's onCreate method.
Launching activity is which <intent-filter>
contains
<category android:name="android.intent.category.LAUNCHER" />
Receive data,
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
//bundle contains all info of "data" field of the notification
}
In background, app receives notification payload in the notification tray, and only handle data payload when the user taps on the notification
Upvotes: 3