Open specific Activity when notification clicked in FCM

前端 未结 5 704
难免孤独
难免孤独 2020-11-28 08:53

I am working on App in which I am required to show notification. For notification, i am using FireBase Cloud Messaging (FCM). I am able to get Notification

5条回答
  •  爱一瞬间的悲伤
    2020-11-28 09:37

    Open MyFirebaseMessagingService.java file

    inside that file there is a sendNotification() method, there you have to specify your activity you need to navigate to in the Intent as shown below

    Intent intent = new Intent(this, YourActivityName.class);
    

    if you are sending multiple notification and you want to navigate to different Activities on click of a particular Notification you can use any conditional statement to achieve it, my suggestion is to use a switch case as shown below

    private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) {
    
        Intent intent = new Intent();
        switch(condition) {
           case '1': intent = new Intent(this, ActivityOne.class);
                     break;
           case '2': intent = new Intent(this, ActivityTwo.class);
                     break;
           case '3': intent = new Intent(this, ActivityThree.class);
                     break;
           default : intent = new Intent(this, DefaultActivity.class);
                     break;
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
        intent.putExtra("Notification", TrueOrFalse);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);
    }
    

    Using this logic you can open a specific activity on Notification click in FCM. This works perfectly for me. Thanks

提交回复
热议问题