I have integrated the FCM in my app, I can receive the notification while the App is running or killed etc. but If the app is running then I can be able to navigate the specific screens. but If the app killed or closed, then If I clicked the notification then always it's redirect to the home screen not for the navigated area. this is the code I used :
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
// if (remoteMessage.getData().size() > 0) {
L.d(TAG, "Message data payload: " + remoteMessage.getData());
// if (remoteMessage.getNotification() != null) {
String msg = remoteMessage.getNotification().getBody();
if (!TextUtils.isEmpty(msg)) {
sendNotification(remoteMessage.getData(), msg);
}
}
private void sendNotification(Map<String, String> data, String messageBody) {
String referenceKey = data.get("ReferenceKey");
String referenceValue = data.get("ReferenceValue");
switch (referenceKey) {
case Repository.ModuleCode.BRAND:
intent = new Intent(this, WebViewActivity.class);
intent.putExtra("ID", referenceValue);
intent.putExtra("browser", false);
break;
case Repository.ModuleCode.NEWS:
intent = new Intent(this, NewDetailActivity.class);
break;
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
in the manifest :
<service
android:name=".fcm.MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".fcm.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
in App Gradle
compile 'com.google.android.gms:play-services:9.6.1'
compile 'com.google.android.gms:play-services-maps:9.6.1'
I cannot navigate the exact page if the app killed or closed cases only. Thanks in advance
Finally I changed the Payload from the backend then it solved my problem.
Previously I used the PayLoad like this
{ "notification": {
"text": "Your Text1"
},
"data": {
"ReferenceKey": "PR" ,
"ReferenceValue": "10,"
},
"to" : "pushtoken"
}
then I remove the notification and used like this.
{
"data": {
"Message": "Test",
"ReferenceKey": "PR" ,
"ReferenceValue": "10,"
},
"to" : "pushtoken"
}
Then it's working for foreground / Killed / closed.
After this I cannot get notification in the xiaomi note3 only.
It's working as expected ..onMessageReceived will not get triggered if the app is killed or in background.If your app is in the background or closed then a notification message is shown in the notification center, and any data from that message is passed to the intent that is launched as a result of the user tapping on the notification.
You can use getIntent().getExtras(); for fetching the intent while launching to get the intent.
more info here;
eg:
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
if (bundle.containsKey("data")) {
Intent intent = new Intent(mContext, ExpectedActivity.Class)
intent.putExtras("PUSH_KEY",bundle.get("data").toString());
startActivity(intent)
}
}
Place this code in your launcher activity.And this will navigate you to your expected activity even when the app is killed or is in background.
Or
You can call your customized activity on click of notification if your app is in background by calling rest service api for firebase messaging as given here https://stackoverflow.com/a/37599088/3111083.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With