0

I am using firebase to send notification. It will open ResultActivity when user click on notification. It is working fine when app is in foreground. But when app is in background, it open the HomeActivity (which is the launcher activity of the app) rather than ResultActivity. I can't understand whats the problem?

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        notificationBuilder.setContentTitle(getResources().getString(R.string.app_name));
        notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);

        Intent intent = new Intent(getApplicationContext(), ResultActivity.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);

        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • http://stackoverflow.com/questions/40718155/open-specific-activity-when-notification-clicked-in-fcm – Tim Nov 22 '16 at 09:11
  • 1
    Take a look [here](http://stackoverflow.com/questions/37407366/firebase-fcm-notifications-click-action-payload). It might help you. – Prashant Sable Nov 22 '16 at 09:15
  • 1
    Possible duplicate of [How to handle notification when app in background in firebase](http://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase) – Prashant Sable Nov 22 '16 at 09:48

1 Answers1

0

This is an nice way to test click_action mapping. It requires an intent filter like the one specified in the FCM docs:

<intent-filter>
    <action android:name="OPEN_ACTIVITY_1" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Also, remember, this will only work if the app is in the background. If it is in the foreground you will need to implement an extension of FirebaseMessagingService. In the onMessageReceived method,you will need to manually navigate to your click_action target

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
    //This will give you the topic string from curl request (/topics/news)
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    //This will give you the Text property in the curl request(Sample     Message): 
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    //This is where you get your click_action 
    Log.d(TAG, "Notification Click Action: " + remoteMessage.getNotification().getClickAction());
    //put code here to navigate based on click_action
    }
Asad
  • 1,260
  • 13
  • 19