I am using the same notification id to show the notification of my app.
I am passing an object with the intent to launch the activity.
The issue is for same notification id, android caches the initial object which is passed along the intent.
So how can I make sure that the latest object is added in the intent.
Or how can I check if a notification with the id is not cleared in the notification panel and if it's there then I can delete the notification and create a new one.
Note :- I want to avoid the scenario to use new notification id as that keeps creating new notification without removing the older ones.
Use PendingIntent.FLAG_UPDATE_CURRENT in your Pending Intent like below
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
NotificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
call notificationManager.cancel(NOTIFICATION_ID); before calling notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
Related
I am getting multiple Local notifications of range 1-10..
I am getting getting notifications with its content and title.. but when i click the notification only first notification open and when i click rest the notification disappear and it show activity but the content remain the same that one of first notification.. Here is my code
on clicking notification i applied this code
Intent intent1 = new Intent(context, Message_activity.class);
intent1.putExtra("randomStr", randomStr);
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pndng = PendingIntent.getActivity(context, id, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
and getting like this in message activity
String message = getIntent().getStringExtra("randomStr");
Log.e("randomStrrandomStr", getIntent().getStringExtra("randomStr"));
fullmsg.setText(message);
Please suggest something Thanks in advance
I guest you used same id for each time you notify a notification by call notify method of NotificationManager. So try to use different id for each time you call notify.
My Android application receives push notification with some text messages.If I tap a push it redirects me to desired activity with latest push message (intent message) but I want to show my desired activity with corresponding push messages.
For example If I receives 10 push notifications and I tap 3rd notification, my code redirects me to the specified activity with 10th push notification's message, but I want to show 3rd intent push notification's message.
I know PendingIntent.FLAG_UPDATE_CURRENT replace the intent message, how can I redirect with corresponding message instead last message?
I have tried the following:
Intent intent = new Intent(this, TestActivity2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("uid", uid);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
getApplicationContext());
Notification notification = mBuilder
.setSmallIcon(R.drawable.ic_launcher)
.setTicker(textMsg)
.setWhen(0)
.setAutoCancel(true)
.setContentTitle(textMsg)
.setStyle(
new NotificationCompat.BigTextStyle().bigText(textMsg))
.setContentIntent(resultPendingIntent).setContentText(textMsg)
.build();
NotificationManager notificationManager = (NotificationManager) getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) System.currentTimeMillis(),
notification);
Use FLAG_ONE_SHOT instead and change the second parameter in getActivity which is set to 0. This answer will make it clear.
You should Change the pendingIntend's content for Different Messages.
Here is the snippet from PendingIntent document.
"A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it."
In simple words...try passing different id(Something like currentEpochTime) for each pendingIntent.
com push notification for Android app, All is working well but lets say if i send 5 push notification a day, i dont want user to see 5 notification icons at once on his phone. Its best if he sees only one.So is their any way where old notification is deleted and only latest is shown.
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
The method that displays the notification has an int parameter which represents the notification identifier. If you use a constant identifier, each new notification will replace the previous one.
you have to set the notification ID to be the same value. So then they get replaced as each one arrives.
int notificationId = 1 ;
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setAutoCancel(true);
builder.setSmallIcon(R.drawable.gcm_logo);
builder.setContentTitle("Test Title");
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, builder.build());
So you can maintain notification Id for different type of notifications. If you keep same notification ID for each notification then it would be updated in the same .
If you have any extras in the pending intent and you want to update those extras with the latest ones then generate pending intent with :
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(),
PendingIntent.FLAG_ONE_SHOT);
I have a working statusbar notification for Android already, but to create it I must set an Activity for it to open. I don't want any activity to open; however, this small project doesn't need an interface at all.
mNotificationManager = (NotificationManager) aContext
.getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
// Create the pending intent, which is basically NOW.
PendingIntent contentIntent = PendingIntent
.getActivity(aContext, 1, new Intent(aContext, BlankActivity.class),
PendingIntent.FLAG_CANCEL_CURRENT);
// Create notification
notificationBuilder = new NotificationCompat.Builder(aContext)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher) // required for launch
.setTicker("Downloading video...") // required for launch
.setWhen(System.currentTimeMillis()) // should be set for now.
.setContent(remoteView);
BlankActivity is what it sounds like -- an activity that has no content and closes upon being opened. But it still shows up on the list of recently opened windows. I cannot set this to null.
Is there a way I can avoid setting an intent for the status notification at all?
You don't need PendingIntent in that case. Just setting it null in setContentIntent() might help.
Another way can be
PendingIntent contentIntent = PendingIntent.getActivity(aContext(),0,new Intent(),PendingIntent.FLAG_CANCEL_CURRENT);
Try this.
Setting it to null does not work for early versions like SDK 2.3 which requires an intent to be set, otherwise android will choke at receiver's start with the exception:java.lang.RuntimeException: Unable to start receiver com.movistar.android.mimovistar.widgets.WidgetIntentReceiver: java.lang.IllegalArgumentException: contentIntent required
see this post
A few days ago I was struggling to find a way to use custom intents for my alarms. Although I got clear answer that I have to customize the Intents based on some unique ID eg. setAction() still have some problems.
I define a PendingIntent this way:
Intent intent = new Intent(this, viewContactQuick.class);
intent.setAction("newmessage"+objContact.getId());//unique per contact
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK ).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP );
intent.putExtra("id", Long.parseLong(objContact.getId()));
intent.putExtra("results", result.toArray());
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
then this is used by a notification manager
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
// first try to clear any active notification with this contact ID
mNotificationManager.cancel(Integer.parseInt(objContact.getId()));
// then raise a new notification for this contact ID
mNotificationManager.notify(Integer.parseInt(objContact.getId()), notification);
This works like this:
application creates a message for a contact
an intent is provided with the contact id and details about the message
notification is raised with the message
user actiones on the notification and the app displays the message passed by the intent
The problem
This can happen more than once for a contact. And when the second message is generated, the notification is raised well (message is fine there) but the intent when the user actions the notification it uses old data, so previous message is passed and not the brand new message.
So someway the intent is caching and reusing previous extras. How can I make it unique per contact and per action?
If only one of your PendingIntents for this contact will be outstanding at any point in time, or if you always want to use the latest set of extras, use FLAG_UPDATE_CURRENT when you create the PendingIntent.
If more than one contact-specific PendingIntent will be outstanding at once, and they need to have separate extras, you will need to add a count or timestamp or something to distinguish them.
intent.setAction("actionstring" + System.currentTimeMillis());
UPDATE
Also, the lightly-documented second parameter to getActivity() and kin on PendingIntent apparently can be used to create distinct PendingIntent objects for the same underlying Intent, though I have never tried this.
I usually specify unique requestCode to prevent my PendingIntents from overriding each other:
PendingIntent pending = PendingIntent.getService(context, unique_id, intent, 0);
And in your case I agree with CommonsWare you just need FLAG_UPDATE_CURRENT flag. New extras will override old values.