Open specific Activity when notification clicked in FCM

前端 未结 5 700
难免孤独
难免孤独 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:33

    AndroidManifest.xml

    
        
            
            
        
    
    

    Your FirebaseMessagingService.java file onMessageReceived method:

    public void onMessageReceived(RemoteMessage remoteMessage){
        String title=remoteMessage.getNotification().getTitle();
        String message=remoteMessage.getNotification().getBody();
        String click_action=remoteMessage.getNotification().getClickAction();
        Intent intent=new Intent(click_action);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
        notificationBuilder.setContentTitle(title);
        notificationBuilder.setContentText(message);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notificationBuilder.build());
    }
    

    Your cloud function/server code for notification:

        notification: {
            title: "TITLE OF NOTIFICATION",
            body: "NOTIFICATION MESSAGE",
            sound: "default",
            click_action: "com.example.myapplication_YOUR_NOTIFICATION_NAME"
        }
    

提交回复
热议问题