Home > database >  Go to different activities when the FCM notification is clicked
Go to different activities when the FCM notification is clicked

Time:12-18

Guys I'm using FCM to send notifications to my android app, and it works fine but when I click on the notification it sends me to a specific activity I set in here :

 @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        Intent intent = new Intent(this, activity_togo_to.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "101")
                .setSmallIcon(R.drawable.ic_add)
                .setContentTitle(remoteMessage.getNotification().getTitle())
                .setContentText(remoteMessage.getNotification().getBody())
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                // Set the intent that will fire when the user taps the notification
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// notificationId is a unique int for each notification that you must define
        notificationManager.notify(1, builder.build());
    }

My problem I don't want to go to the same activity when a notification is clicked.

So my notification system works like this :

I have two tables in MySQL database A and B :

When a row is added to table A --> push notification with the title: "there is a new item A"

When a row is added to table B --> push notification with the title: "there is a new item B"

When I click on a notification :

with the title: "there is a new item A" --> go to activity A

with the title: "there is a new item B" --> go to activity B

How can I achieve this guys, I really need it.

Appreciate any help. if it's not possible just let me know.

CodePudding user response:

The pending intent of your notification must be to activity A or B. You can pass argument to your notification from your remote server to remoteMessage.getData().

For example:

with(remoteMessage.data) {
    // Extract data
    type = get(PARAM_TYPE)
    variation = get(PARAM_VARIATION)
    priority = getNotificationPriorityFromString(get(PARAM_PRIORITY))
    title = get(PARAM_TITLE)
    message = get(PARAM_MESSAGE)
}
  • Related