Android Pending Intent Notification Problem

android pending intent notification problem

The problem is that when I create more
than one alarm then the activity
launched from the notification gets
the same extras as the first one.

Correct.

How can I make it to launch the
correct intents?

That depends on whether you have two alarms that will be registered at once, or not.

If not, you can use FLAG_ONE_SHOT or one of the other PendingIntent flags to have your second PendingIntent use the newer extras.

If, however, you will have two alarms registered at once, with different Intent extras, you will need to make the two Intents be more materially different, such that filterEquals() returns false when comparing the two. For example, you could call setData() or setAction() and provide different values for each Intent.

Pending intent in notification not working

try this:

private void setNotification(String notificationMessage) {

//**add this line**
int requestID = (int) System.currentTimeMillis();

Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mNotificationManager = getApplication().getSystemService(Context.NOTIFICATION_SERVICE);

Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity2.class);

//**add this line**
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

//**edit this line to put requestID as requestCode**
PendingIntent contentIntent = PendingIntent.getActivity(this, requestID,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.logo)
.setContentTitle("My Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notificationMessage))
.setContentText(notificationMessage).setAutoCancel(true);
mBuilder.setSound(alarmSound);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

}

Notification PendingIntent not working when app is in background state

I solved this problem. The problem was with remoteMessage.notification.let { which is triggered only when app is in foreground state, thus I removed this part and it worked like a charm)

Pending Intent are not Working as Expected

First of all change resultIntent.putExtra inside onMessageReceived like:

resultIntent.putExtra("from_user_id", from_user_id);

And in your ChatActivity fetch user_id like below:

String user_id = getIntent().getStringExtra("from_user_id") 

Hope this solve your problem.

Or change your notification payload:

const payload = {
notification: {
title: userName,
body: message,
icon: "default",
click_action : "com.example.chitchat_TARGET_MESSAGE_NOTIFICATION"
},
data : {
user_id : from_user_id
}
};

And inside onMessageReceived change:

String from_user_id = remoteMessage.getData().get("from_user_id");

To

String from_user_id = remoteMessage.getData().get("user_id");

Reason: During background, System generates the notification without executing your code inside onMessageReceived. That's why it puts the extra as it gets from notification payload which is from_user_id, that's you send from server.



Related Topics



Leave a reply



Submit