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
Start an Activity from a Fragment
Disconnect a Bluetooth Socket in Android
Navigation Drawer: Set as Always Opened on Tablets
Difference Between APK (.Apk) and App Bundle (.Aab)
Memory Leaks with Custom Font for Set Custom Font
Toggle Button Using Two Image on Different State
Trying to Get the Display Size of an Image in an Imageview
What Is the Use of the Res/Values/Public.Xml File on Android
How to Animate a View with Translate Animation in Android
Fragments Onresume from Back Stack
Android Recyclerview:Notifydatasetchanged() Illegalstateexception
Android: Background Image Size (In Pixel) Which Support All Devices
Which Websocket Library to Use in Android App
How to Copy Text to Clip Board in Android
How to Change the Color of a Switchcompat from Appcompat Library