Reputation: 11
I am using fcm to deliver notification in my app. I want to launch MsgViewActivity whenever a user click on the notification. Now this works just fine when app is in foreground but when app is not running it just take me to the mainactivity. Also note that i am using data messages so onMessageRecived is called even in the background.
here's my code of FirebaseMessagingService.class
public class FirebaseMessagingSer extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher);
Intent intent = new Intent(this, MsgViewActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
if(remoteMessage.getData().size()>0) {
String id = remoteMessage.getData().get("id");
Bundle bundle = new Bundle();
bundle.putString("id",id);
intent.putExtras(bundle);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle("FCM NOTIFICATION");
notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setLargeIcon(bmp);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
}
}
Upvotes: 1
Views: 487
Reputation: 12717
From your example you are using a message with both notification: {..}
and data: {..}
payload.
this means that your message is considered a notification-message
and as result the method onMessageReceived()
is executed only when the app is in foreground. (this explains your issue)
You have two solutions:
use data only messages. Don't sent anything in the notification: {..} part of the message. You can set body and title as additional key-value in the data payload.
you can use notification-messages with the parameter click_action="intent_to_activity2".
You will also need to add the intent filter to your activity manifest.
Upvotes: 1