0

How can i open a specific activity on clicking push notification received from firebase cloud messsage. I have set to open required activity with new Intent under onMessageReceived method. Its working fine while app is in foreground.But while app is in background it is not working.

can't we open an Activity on clicking push notification, while using Firebase FCM console?

public class MyFireBaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "FCM Service";
    private static int count = 0;

    @Override
    public void onNewToken(@NonNull String s) {
        super.onNewToken(s);
        Log.e(TAG, "onNewToken: " + s);

    }

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {

        Map<String,String> opendata = remoteMessage.getData();
        String actionValue = opendata.get("openactivity");

        Intent intent=new Intent();

        assert actionValue != null;
        switch (actionValue){

                case "Activity1":
                    intent=new Intent(this, Activity1.class);
                    break;
                case "Activity2":
                    intent=new Intent(this, Activity2.class);
                    break;
                
               


        }

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("pushnotification","True");
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_LOW;
            NotificationChannel mChannel = new NotificationChannel("MyID", "Myapp", importance);
            mChannel.setDescription(remoteMessage.getData().get("message"));
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.RED);
            mChannel.enableVibration(true);
            mNotifyManager.createNotificationChannel(mChannel);
        }
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "MyID");
        mBuilder.setContentTitle(remoteMessage.getData().get("title"))
                .setContentText(remoteMessage.getData().get("message"))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.maft_logo))
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setColor(Color.parseColor("#FFD600"))
                .setContentIntent(pendingIntent)
                .setChannelId("Myid")
                .setPriority(NotificationCompat.PRIORITY_LOW);

        mNotifyManager.notify(count, mBuilder.build());
        count++;
}

}

Manifest

 <service
            android:name=".MyFireBaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
Irfan
  • 35
  • 1
  • 7

1 Answers1

4

For Redirect for a different activity, you need an extra one or more parameters to define when it should redirect. so with the remote message, as an additional parameter, you can send with the action keys. action key-value like BROWSER_ACTION, PAYMENT_ACTION, UPGRADE_APP, and more you have to define in your app.

Additional Fields in the cloud Message

Based on that you can call to specific Activity.

@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
     HashMap<String,String> additionalFields = remoteMessage.getData();
     String actionValue = additionalValues.get("actionKey")
     
     switch(actionValue){
         case "BROWSER":
            //redirect to one activity
            break;
         case "HOME":
            //redirect to another activity
            break;
         case "PAYMENT":
            break;
      }
}

based on the action value you have to define your pending intent to redirect.

  • ....Please check my updated code. Its working fine when app is in foreground. But not in background. The message showing when app is in background is Notification message of FCM, Not Data payload. Can't we open specific activity on clicking push notification?. I am directly using FCM console. – Irfan Jun 28 '20 at 18:14