I'm having issues with mutiple notification while putExtra using PendingIntent. In Activity getStringExtra fom Intent is returning last putExtra while generating Notification.
Let me explain complete scenario first and make me correct if i'm wrong.
First of all my Activity's (let's say its MainActivity) launchMode is singleTop. i'm setting it into manifest.i.e.
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
... />
Now i'm generating notifaction using this code,
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent notifyIntent = new Intent(context,
MainActivity.class);
notifyIntent.putExtra("message", msg);
Log.i(TAG, msg);
notifyIntent.putExtra("title", notification_title);
Log.i(TAG, notification_title);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(
BeaconService.this, 0, notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(
context).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(notification_title).setContentText(msg)
.setAutoCancel(true).setContentIntent(pendingIntent).build();
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notificationManager.notify(NOTIFICATION_ID, notification);
NOTIFICATION_ID++;
I'm also using flag PendingIntent.FLAG_UPDATE_CURRENT. but, while clicking on any Notification (let's say we've 5 notifactions) it just returns the fifth extra put while generating Notification all top four notification extra are just like lost somewhere.
In my MainActivity i've also override onNewIntent like,
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// getIntent() should always return the most recent
setIntent(intent);
if (intent.hasExtra("message")) {
String message = intent.getStringExtra("message");
}
if (intent.hasExtra("title")) {
String title = intent.getStringExtra("title");
}
}
this extras returned are always from last notification's extra. I don't know where am i going wrong?
I've also tried some links but didn't find helpful solution.
Intent extras being lost when sending with PendingIntent for GCM
Please help.
You aren't creating unique PendingIntents. There must be a thousand answers to exactly this question on Stackoverflow.
When you call this:
PendingIntent pendingIntent = PendingIntent.getActivity(
BeaconService.this, 0, notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
The second time, it will find the first PendingIntent and return you a reference to it (Android will not create another one). Also, since you set FLAG_UPDATE_CURRENT, Android will modify the underlying Intent by overwriting the extras in the old Intent with the extras you provided in the new Intent. That's why it always only delivers one Intent and that Intent always has the last set of extras.
You need to make each PendingIntent unique. There are many ways to do this (setting different extras is NOT one of the ways). Try providing a unique number as the requestCode parameter to PendingIntent.getActivity().
#David Wasser thanks for your answer!
I just wanted to share a piece of code which i've used and solved my issue.
providing a unique number as the requestCode parameter to PendingIntent.getActivity() is the right solution with Multiple Notifications while launchmode="singleTop".
To provide unique code as the requestCode what i've done is, placed NOTIFICATION_ID as requestCode i.e,
PendingIntent pendingIntent = PendingIntent.getActivity(
BeaconService.this, NOTIFICATION_ID, notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
I've also found same solution at here.
That's the perfect unique requestCode in my case. I hope it will be helpful to others is well who are facing issue.
Related
I have an alarm receiver that does some checks and then creates a Notification for each check. This means it can create more than one Notification. This all works fine. However, I have an Intent connected to the notification to start an activity when the notification is tapped.
This is the notification code:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_OUTPUT_LEVELS)
.setSmallIcon(icon)
.setContentTitle(title)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(longmessage))
.setContentIntent(getPendingIntent(solarEdge, reason))
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
// notificationId is a unique int for each notification that you must define
// we use the installation's ID to make sure all notifications get sent
notificationManager.notify(solarEdge.getInfo().getId(), mBuilder.build());
The method that creates the PendingIntent is:
private PendingIntent getPendingIntent(SolarEdge solarEdge, int reason) {
String apikey = solarEdge.getApikey();
int installationId = solarEdge.getInfo().getId();
Intent intent = new Intent(context, InstallationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(EXTRA_API_KEY, apikey);
intent.putExtra(EXTRA_INSTALLATION_ID, installationId);
intent.putExtra(EXTRA_REASON, reason);
return PendingIntent.getActivity(context, 0, intent, 0);
}
Problem I have, is that even I create a different intent, it still always calls the Activity with the same extras (apikey and installid). It always takes the first one created.
It seems that the problem is caused by the arguments you passed to the getActivity method.
Try the following code
PendingIntent.getActivity(context, <unique_value_per_every_call>, intent, 0);
The second argument is requestCode so it should be unique.
The accepted answer is one way to solve the problem, but it isn't the only way.
To get a unique PendingIntent on every call, the call to PendingIntent.getActivity() needs to provide either a unique requestCode or a unique Intent. To make the Intent unique it is necessary to provide either a unique ACTION, DATA, or COMPONENT. There are therefore several ways to solve this problem.
Known "problem"/quirk/design and we programmers keep running into it.
The request code parameter must be something other than 0 if the intent is the same (action, data or component the same, not only extra data differs).
int requestCode = 1;
PendingIntent.getActivity(context, requestCode, intent, 0);
Additionally:
If you want to update a notification/pending intent later on you can use the same requestCode.
If you add multiple pending intents they must all be different. Example: A stop and a pause action.
I usually follow this pattern:
requestCode = 1 for the content intent.
requestCode = 2 for first action
requestCode = 3 for second action
etc
Hello currently I have setup a application which allows the user to set an alarm. When this alarms reaches it designated time it will then send a notification to the user. Now currently each time this notification is activated it sends a static message e.g. "You have a notification". I want to be able to change this notifications title to the alarms name which I have stored inside of SQLOPENHELPER when the user made the alarm.
I have also given each alarm a specific ID which is the current time in milliseconds and then also stored this inside of my SQL. Now I know you require a new ID for each notification but how would I do this and relate it back to my SQL?
I have included a small diagram just to clarify what I'm doing and my current alarm and broadcast receiver...
Diagram................
Set alarm.....
// id - relates to the custom id given to that alarm
// receiver - is our intent
pendingIntent = PendingIntent.getBroadcast(v.getContext(), id, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, myCalendar.getTimeInMillis(), pendingIntent);
// Intent array list is used to store multiple alarms
intentArrayList.add(pendingIntent);
Broadcast receiver...
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent moveActivity = new Intent(context, AlarmActivity.class);
moveActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Works with moveActivity to move the user back to main application.
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, moveActivity, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setContentTitle("title")
.setContentText("text")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true);
notificationManager.notify(0, builder.build());
// Notification ID has been left at 0 because I don't know what to do as this point and after trying to figure it out for 2 days I thought I should ask.
//// UPDATE
Main Activity
I have implemented a put extra just under my intent Array List
receiver.putExtra("title", id);
Broadcast
Bundle bundle = intent.getExtras();
if (bundle != null) {
pass = bundle.getString("title");
//I have put my notification in this section
}
I tested it by putting "pass" which is the String for retrieving the put extra into the title of my notification and it comes up blank?
Update your receiver intent as below to pass the ALARM_ID:
Intent receiver = new Intent(getApplicationContext(), Receiver.class);
receiver.putExtra("KEY_ALARM_ID", ALARM_ID); // ALARM_ID = id
In PendingIntent.getBroadcast(), Use ALARM_ID as requestCode.
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, ALARM_ID, receiver, PendingIntent.FLAG_UPDATE_CURRENT );
Always use different requestCode for different notification to get
correct result.
In notify() method, use ALARM_ID as id:
notificationManager.notify(ALARM_ID, builder.build());
Notification id should be unique within your application. If a
notification with the same id has already been posted by your
application and has not yet been canceled, it will be replaced by the
updated information.
In your BroadcastReceiver class onReceive() method get the ALARM_ID:
#Override
public void onReceive(Context context, Intent intent)
{
int alarmId= intent.getExtras().getInt("KEY_ALARM_ID");
// Do something with alarmId
}
Hope this will solve your problem.
// Main activity
receiver.putExtra("title", id);
pendingIntent = PendingIntent.getBroadcast(v.getContext(), id, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, myCalendar.getTimeInMillis(), pendingIntent);
intentArrayList.add(pendingIntent);
Broadcast
Bundle bundle = intent.getExtras();
int pass = bundle.getInt("title" , 1);
Thanks psKink you gave me the kick I needed
I am seriously confused now, I have been reading several SE examples and they all seem to be doing slightly different things.
What i want to do: Is have a single Activity called NotificationActivity, when I click on a notification it must open that activity and provide the activity with a DeviceId. I don't want to override or update any pending activities. Each activity should be its own intent.
There should only be once instance of NotificationActivity.
here is the code I have so far:
MyGcmListenerService:
Intent intent = new Intent(this, NotificationActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
//ADD My Extras
intent.putExtra(CommonBundleAttributes.CONNECTING_ACTIVITY_DEVICE_ID, content.DeviceId);
intent.putExtra(CommonBundleAttributes.CONNECTING_ACTIVITY_DEVICE_TYPE_ID, content.DeviceTypeId);
intent.putExtra(CommonBundleAttributes.CONNECTING_ACTIVITY_DEVICE_NAME, content.DeviceName);
//
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(content.DeviceName)
.setContentText("Notification")
.setAutoCancel(true)
.setSound(ingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setGroup("Mi:" + content.DeviceId)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NotificationID++, notificationBuilder.build());
and my NotificationActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
deviceId = getIntent().getExtras().getLong(CommonBundleAttributes.CONNECTING_ACTIVITY_DEVICE_ID, -1);
deviceName = getIntent().getExtras().getString(CommonBundleAttributes.CONNECTING_ACTIVITY_DEVICE_NAME, "");
deviceTypeId = getIntent().getExtras().getInt(CommonBundleAttributes.CONNECTING_ACTIVITY_DEVICE_TYPE_ID, 0);
What am I missing here/ I think I am getting mixed up with all the different flags and launcher types.
If there is already an NotificationActivty in memory, I want to close it and open a new one with the latest intent. If there are 3 notifications on the users phones, and they click on all three. it must open the NotificationActivty for the last clicked Notification.
There must be an issue with my pending intent?
In NotificationActivity, you collect int or long value using following code i.e.
notificationID = getIntent().getExtras().getInt(CommonBundleAttributes.CONNECTING_ACTIVITY_NOTIFICATIONID, 0);
then please pass a proper value with putExtra() i.e. if collect int then pass Integer.parseInt(content.DeviceId) or collect long then pass Long.parseLong(content.DeviceId)
hope it's helpful to you
In Notification class add this line
intent.putExtra(CommonBundleAttributes.CONNECTING_ACTIVITY_NOTIFICATIONID, NotificationID);
In NotificationActivity
notificationID = getIntent().getExtras().getInt(CommonBundleAttributes.CONNECTING_ACTIVITY_NOTIFICATIONID, 0);
When creating a new notification with a new PendingIntent, the extras in its intent override any previous notification's PendingIntent Intent extras.
For example, lets say I create Notification1 with PendingIntent1, which has Intent1 and its extras.
When I create Notification2 with PendingIntent2, which has Intent2 with its own different extras, Intent1 will now have the same extras as Intent2. Why is this happening? How do I work around this?
There are two ways to solve this:
One is to set a different action on the Intent. So in your example, you could set Intent1.setAction("Intent1") and Intent2.setAction("Intent2"). Since the action is different, Android will not override the extras on the intent.
However, there may be a case where you actually need to set a specific action on this intent (i.e. your action corresponds to a specific broadcast receiver). In this case, the best thing to do is set the request code to something different in each PendingIntent:
PendingIntent pendingIntent1 = PendingIntent.getActivity(context,
(int) System.currentTimeMillis() /* requestCode */, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context,
(int) System.currentTimeMillis() /* requestCode */, intent2,
PendingIntent.FLAG_UPDATE_CURRENT);
By setting a new request code on the PendingIntent, Android will not override the extras on each of their corresponding intents.
In my case it worked with by using unique notification id each time like below:
mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
Generate the unique id each by using (int) System.currentTimeMillis() and give this to NotificationManager object.
I have developed an application to download a video file and store it in the SD card. In the process I also update the progress and status of the download as a status bar notification using the NotificationManager.
My class called the DownloadTask.java extends the AsyncTask. So here I update the progress using the onProgressUpdate() method where in I use the NotificationManager for the purpose. Everything works like a charm except, on completion of download I want to click the notification to open the specific video file. So this is what i have done:
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
int icon = android.R.drawable.stat_sys_download_done;
long when = System.currentTimeMillis();
mNotification = new Notification(icon, "", when);
mContentTitle_complete = mContext.getString(R.string.download_complete);
notificationIntent = new Intent(mContext,OpenDownloadedVideo.class);
notificationIntent.putExtra("fileName", file);
mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
mNotification.setLatestEventInfo(mContext, file, mContentTitle_complete, mContentIntent);
mNotification.flags = Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
Note that the fileName and NOTIFICATION_ID are unique in my case.
The Activity OpenDownloadedVideo.java opens the file by:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
fileName = getIntent().getExtras().getString("fileName");
Intent i = new Intent(Intent.ACTION_VIEW);
File videoFileToPlay = new File(Environment.getExternalStorageDirectory()+"/MyFolder"+"/"+fileName);
i.setDataAndType(Uri.fromFile(videoFileToPlay), "video/*");
startActivity(i);
finish();
} catch(Exception e) {
//
}
}
So when I download a video for the first time and click on the notification the appropriate video file will be opened. However next time when I download another video, and click on the notification the first file which was downloaded will be opened again.
This is because getIntent inside OpenDownloadedVideo returns the first Intent created and not the latest. How can I correct this?
Also, please note that the problem scenario exists when I download more than one video, e.g. if I download five different video files and there are five notifications in the status bar. The same file will be opened each time a notification is clicked.
/**
* Override super.onNewIntent() so that calls to getIntent() will return the
* latest intent that was used to start this Activity rather than the first
* intent.
*/
#Override
public void onNewIntent(Intent intent){
super.onNewIntent(intent); // Propagate.
setIntent(intent); // Passing the new intent to setIntent() means this new intent will be the one returned whenever getIntent() is called.
}
actually you just need create PendingIntent with PendingIntent.FLAG_UPDATE_CURRENT ,like this:
mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
From the Android Activity.onNewIntent() documentation
Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.
So when you get the new Intent, you need to explicitly set it as the activity intent.
#Alex and #Codinguser, thank you for your replies. Much appreciated. However I found a different answer that worked for me. When creating a PendingIntent for the Intent pass a unique value to it. In my case I was doing this:
mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
but now I'm using NOTIFICATION_ID since it was unique. I've changed the above call to:
mContentIntent = PendingIntent.getActivity(mContext, NOTIFICATION_ID,
notificationIntent, 0);
That's all and it works.
I found some information on it in the question
Mulitple Instances of Pending Intent
What if you make your OpenDownloadedVideo activity SINGLE_TOP and override onNewIntent?
in the activity you're intent is launching.
Add (override) the following function:
#Override
protected void onNewIntent(final Intent intent) {
super.onNewIntent(intent);
this.setIntent(intent);
}