Android Pending intent started from notification doesn't replace the last - android

I've read many posts on the same topic and tried all the given solutions without getting the result I want.
The program should start an intent with extras from a notification:
NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, myActivity.class);
notificationIntent.putExtra("someData", data);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(ID, notification);
The problem is that when a new notification is shown, the extras added to the intent is the same as in the first notification. I've triend with differnt flags in both the intent and the pending intent, without result. What am I getting wrong?
If i just launch the same activity (and the same extras) with a button, everything works as it's supposed to.

I don't know why I've had such problems with getting this to work. The combination of flags I used to get it to work properly was:
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
I also removed all of the flags used when creating the notificationIntent.

Try to add an attribute in AndroidManifest.xml file:
<activity ... android:launchMode="singleTop"/>

Try to set request code for each PendingIntent and it will work
PendingIntent pendingIntent = PendingIntent.getActivity(this, RandomInt, intent,
PendingIntent.FLAG_ONE_SHOT);

Related

Clear all stack when notification is clicked

I'm showing a notification using FCM on my android app. Everything is working as expected except one issue.
When user clicks on the notification, I need to close all background and foreground activities of my app and open the intent specified in the notification.
How can I do it?
I'm using pending intent like this
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
);
YOu can use 2 flags to clear all stacks
intentToLaunch.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intentToLaunch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Try this,
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, HomeActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);

android - How to show a dialog when click on notification

I get notification in my application through BroadcastReceiver, The question is , How can I parse data to an activity and make a dialog with the received data ?
this is my code but when I click on the notification , nothing happens :
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse(link));
PendingIntent pending = PendingIntent.getActivity(ctx, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
Notification myNotification = new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.ic_launcher).setAutoCancel(false).setLargeIcon(remote_picture)
.setContentTitle(onvan).setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg).setContentIntent(pending).build();
notificationManager.notify(1, myNotification);
I've some variablese like link , msg, onvan that contains my data and I need to send these variables to an activity and make a dialog .
How can I do so ?
Try this:
Intent notifyIntent = new Intent(context,YourActivityClassHere.class);
notifyIntent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
//UNIQUE_ID if you expect more than one notification to appear
PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, UNIQUE_ID,
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
just make the PendingIntent open up one of your Activities and have your Activity be complete transparent and just open a Dialog.
EDIT: IF you want to open an Activity from notification click event:
Assuming that notif is your Notification object:
Intent notificationIntent = new Intent(this.getApplicationContext(), ActivityToStart.class);
PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, notificationIntent, 0);
notif.contentIntent = contentIntent;
For more detail visit here. Android - Prompt Dialog window when touch on Notification
You can send data from your notification to another Activity using method putExtra and put this
PendingIntent pending = PendingIntent.getActivity(ctx, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT));
inplace of this
PendingIntent pending = PendingIntent.getActivity(ctx, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);`

Notification not starting a activity in android

I am developing a small app in which I want a background service to send a notification and when the user clicks the notification it must launch the appropriate activity. I am posting the code here for the notification and my problem is that the notification gets displayed on the status bar but when i click it it does not launch the activity. Can somebody suggest where I am going wrong. Please help.
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"A New Message!", System.currentTimeMillis());
Intent notificationIntent = new Intent(NotificationService.this,
com.jeris.android.MetroActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent
.getService(NotificationService.this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(NotificationService.this, "Bullion",
"Rates for "+ parsedDate+"has not been updated. Go to app to check rates",
pendingIntent);
notificationManager.notify(11, notification);
Replace
PendingIntent.getService(NotificationService.this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
With
PendingIntent.getActivity(NotificationService.this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
Notice the getService is replaced with getActivity.

Notification passes old Intent Extras

i am creating a notification inside a BroadcastReceiver via this code:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
int icon = R.drawable.ic_stat_notification;
CharSequence tickerText = "New Notification";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_VIBRATE;
long[] vibrate = {0,100,200,200,200,200};
notification.vibrate = vibrate;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
CharSequence contentTitle = "Title";
CharSequence contentText = "Text";
Intent notificationIntent = new Intent(context, NotificationActivity.class);
notificationIntent.putExtra(Global.INTENT_EXTRA_FOO_ID, foo_id);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
int mynotification_id = 1;
mNotificationManager.notify(mynotification_id, notification);
When I click on the notification, it opens the NotificationActivity and inside the Activity i can retrieve the foo_id from the Intent-Bundle (e.g. 1)
However if another notification is triggered and i click on it again, the activity still receives the "old" value (1) from the Intent-Bundle. I've tried to clear the bundle with clear(), but am receiving the same effect. I think sth is wrong with my code..
You are sending the same request code for your pending intens.
Change this:
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
To:
PendingIntent contentIntent = PendingIntent.getActivity(context, UNIQUE_INT_PER_CALL, notificationIntent, 0);
intents are not created if you send the same params. They are reused.
Alternatively, you can use the following code to generate your PendingIntent:
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
From the doc for PendingIntent.FLAG_UPDATE_CURRENT:
If the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.
You are passing the same ID. In this kind of situation, make a unique id from time like this:
int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
And put it as this:
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),iUniqueId, intentForNotification, 0);
For anyone looking for the best approach after a long time all, you need to pass the PendingIntent.FLAG_UPDATE_CURRENT as the last argument as shown below
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
you don't even need to provide a new unique id.
You need to do this for next time onwards not for the first time
Your request code is 0 for all the notification. Change following line:
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
With:
PendingIntent contentIntent = PendingIntent.getActivity(context, new Random().nextInt(), notificationIntent, 0);
Just wanted to add another option
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);

android: getting same value from Intent extras

I am doing this to create a Intent.
Intent notificationIntent = new Intent(this, Startup.class);
notificationIntent.putExtra("url", contentUrl);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
On getting the extra,
CharSequence url = this.getIntent().getCharSequenceExtra("url");
There are many Intents with their respective url values. But I also get back the same value inside onCreate() of Startup class.
What did I do wrong?
This should answer you query for sure...cheers :)
Android keeps caching my intents Extras, how to declare a pending intent that keeps fresh extras?
Use this
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
instead of this
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
and check the results

Categories

Resources