I have below code creating from service for notification.
I am trying to send different values in extra still getting the last set value every time in receiver.
Intent closeButton = new Intent(this,DownloadCancelReceiver.class);
closeButton.putExtra("id",id);
closeButton.setAction("Action"+Long.toString(id));
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, id, closeButton, PendingIntent.FLAG_UPDATE_CURRENT);
notificationView = new RemoteViews(getPackageName(), R.layout.notification_view);
notificationView.setProgressBar(R.id.pb_progress, 100, 5, false);
notificationView.setOnClickPendingIntent(R.id.btn_close, pendingSwitchIntent);
The only things that you are changing in Pending Intent are Extra and Action, and then for optimization purposes Android is reusing the old intent. Try to change PendingIntent.FLAG_UPDATE_CURRENT with PendingIntent.FLAG_CANCEL_CURRENT
To create the PendingIntent try:
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, id, closeButton, PendingIntent.FLAG_CANCEL_CURRENT);
Related
I have a notification with following content intent:
clickIntent = PendingIntent.getService(
getApplicationContext(),
Constants.REQUEST_CODE_NOTIFICATION_OTHER,
new Intent(getApplicationContext(), MainService.class)
.putExtra(Constants.EVENT, Constants.EVENT_TOGGLE_BLACK),
PendingIntent.FLAG_UPDATE_CURRENT);
and one action with following action intent:
PendingIntent closeIntent = PendingIntent.getService(
getApplicationContext(),
Constants.REQUEST_CODE_NOTIFICATION_OTHER,
new Intent(getApplicationContext(), MainService.class)
.putExtra(Constants.EVENT, Constants.EVENT_DISABLE_SERVICE),
PendingIntent.FLAG_UPDATE_CURRENT);
When I click the notification, the close intent is fired. Why?
Here is how I create the notification:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(text))
.setStyle(new NotificationCompat.BigTextStyle().bigText(getString(details)))
.setSmallIcon(R.drawable.ic_not)
.setContentIntent(clickIntent)
.setOngoing(true);
boolean showCloseButton = MainApp.getPrefs().enableCloseServiceButtonInNotification();
if (showCloseButton)
builder.addAction(R.drawable.ic_close_black_24dp, getString(R.string.stop_service), closeIntent);
This is my guess... If does not work, I'll delete the answer...
I believe problem is that they are sharing same update number:
This is created:
clickIntent = PendingIntent.getService(
getApplicationContext(),
Constants.REQUEST_CODE_NOTIFICATION_OTHER,
new Intent(getApplicationContext(), MainService.class).putExtra(Constants.EVENT, Constants.EVENT_TOGGLE_BLACK),
PendingIntent.FLAG_UPDATE_CURRENT);
Then, this new one is created:
PendingIntent closeIntent = PendingIntent.getService(
getApplicationContext(),
Constants.REQUEST_CODE_NOTIFICATION_OTHER,
new Intent(getApplicationContext(), MainService.class).putExtra(Constants.EVENT, Constants.EVENT_DISABLE_SERVICE),
PendingIntent.FLAG_UPDATE_CURRENT);
It is similar to previous one and it is set PendingIntent.FLAG_UPDATE_CURRENT.
So, the second one UPDATES the first one.
Try to use a different code for different PendingIntents. Both are using Constants.REQUEST_CODE_NOTIFICATION_OTHER.
Then, Android will use that code to differentiate them.
I am creating a notification in my Game. I want to show the notification if the player is not playing the game for sometime. I am using the below PendingIntent with alarm setup to create a notification. With this I am able to create notifications.
Question: If the player plays the game I need to cancel the existing pending Intent and create a new Pending Intent. I hope the Pending Intent flags will be used to achieve ? Please advice. Also it would be a great help if you could throw some light on the available Flags.
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
The only way to cancel a PendingIntent that has been created with FLAG_ONE_SHOT is to hold on to a reference to that PendingIntent and then call cancel() on the reference.
If you no longer have a reference, you cannot cancel this PendingIntent.
You don't need to use FLAG_ONE_SHOT, so I wouldn't do that. It causes more problems than it solves.
Based on your answer to my question in the comments, what you want to do is to cancel the alarm. You don't need to cancel the PendingIntent. To cancel the alarm, just do this:
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(pendingIntent);
If you don't have any "extras" in the PendingIntent, then you can just create your PendingIntent like this:
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
If you have "extras" in the PendingIntent that need to be replaced when you reset the alarm, you can create the PendingIntent like this:
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
I have a PendingIntent which is used in an AlarmManager The PendingIntents have unique ids which I keep track of. Because of this, I should be able to cancel the PendingIntent by passing the unique id when I try to cancel it. I am trying to cancel it in a different Fragment from the one I created it in, if that makes any difference.
But I have not been able to cancel it even after looking at many past questions.
I have looked at eg.: https://stackoverflow.com/a/4350149/2442638 , https://stackoverflow.com/a/12257277/2442638 , https://stackoverflow.com/a/11682008/2442638
This is how I create the PendingIntent and set it with the AlarmManager:
// remCounter is unique for each PendingIntent
Intent remIntent = new Intent();
remIntent.setAction("com.example.test.remBroadcast");
Bundle extras = new Bundle();
extras.putString("content_title", (String) s_content_title);
extras.putString("content_text", (String) s_content_text);
extras.putBoolean("bOngoing", boolean_ongoing);
extras.putInt("remCounter", remCounter); // This is how I keep track of the unique id. I have proved this to work.
remIntent.putExtras(extras);
PendingIntent remPendingIntent = PendingIntent.getBroadcast(
getActivity(), remCounter, remIntent,
PendingIntent.FLAG_ONE_SHOT);
getActivity(); // (Do I need this?)
AlarmManager remAlarmManager = (AlarmManager) getActivity()
.getSystemService(Context.ALARM_SERVICE);
remAlarmManager.set(AlarmManager.RTC_WAKEUP,
setForCal,
remPendingIntent);
This is how I'm trying to cancel a PendingIntent:
// cancelCounter is the value of the unique id of the PendingIntent I want to cancel
Intent intent = new Intent();
PendingIntent pi = PendingIntent.getBroadcast(getActivity(),
cancelCounter, intent, PendingIntent.FLAG_UPDATE_CURRENT);
pi.cancel();
AlarmManager am = (AlarmManager) getActivity()
.getSystemService(Context.ALARM_SERVICE);
am.cancel(pi);
I have an App Widget.
In the App Widget I try to set 2 Pendingintests on the same Viev:
//FIRST PENDINGINTENT
Intent i1 = new Intent(getApplicationContext(), AppWidget.class);
i1.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
i1.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pi = PendingIntent.getBroadcast(
getApplicationContext(), 0, i1,
PendingIntent.FLAG_UPDATE_CURRENT);
//SECONDPENDINGINTENT
Intent i11 = new Intent(getApplicationContext(), WakeUp.class);
PendingIntent pi1 = PendingIntent.getActivity(
getApplicationContext(), 0, i11,0);
//I SET THE PENDINGINTENT ON THE VIEW
updateViews.setOnClickPendingIntent(R.id.background, pi1);
updateViews.setOnClickPendingIntent(R.id.background, pi);
As you can see I set 2 Pendingintents (pi and pi1) on the SAME view R.id.background.
The Pendingintent pi works as it shuould.
The Pendingintent pi1 has NO EFFECT.
Please any help very much appreciated
This is not possible. Any View in the RemoteViews can have only one PendingIntent for setOnClickPendingIntent(). If you call setOnClickPendingIntent() twice, the last one in wins.
Hence, please call it just once, and have WakeUp call sendBroadcast() to accomplish your second operation.
Also, please replace getApplicationContext() with this, as you do not need the application context in any of this code.
I'm creating a widget with two buttons.
One of them updates the content of the widget and the second one must launch an activity.
I have two PendingIntent for each action, but I can't make them both work. If one works the other one doesn't.
I've revised the code and can't understand what's wrong.
Any help will be very appreciated.
This is the code.
RemoteViews controls = new RemoteViews(context.getPackageName(), R.layout.miwidget);
Intent intent = new Intent("actony.com.ACTUALIZAR_WIDGET");
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
Intent intentSettings = new Intent();
intentSettings.setClass(context,WidgetConfig.class);
PendingIntent pendingIntentUpdate = PendingIntent.getBroadcast(context, widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
controls.setOnClickPendingIntent(R.id.BtnActualizar, pendingIntentUpdate);
PendingIntent pendingIntentSettings = PendingIntent.getActivity(context, 0, intentSettings, 0);
controls.setOnClickPendingIntent(R.id.botonSettings, pendingIntentSettings);
Try adding the getActivity PendingIntent.FLAG_UPDATE_CURRENT aswell...
PendingIntent pendingIntentSettings =
PendingIntent.getActivity(context, 0, intentSettings, PendingIntent.FLAG_UPDATE_CURRENT);
and if multiple widget's are possible add the widgetId there too.
Make sure both of the activities/broadcasts are listed in the manifest file.
Moreover, try creating the Intent with this constructor:
Intent intent = new Intent(context,ACTUALIZAR_WIDGET.class);
Intent intentSettings = new Intent(context,WidgetConfig.class);
add imports if needed.
Hope some of that will make you widget work.
Check this link to know which button has been clicked when there is two or more button in a widget..
https://stackoverflow.com/a/10733049/1331593
It should work... IF it does not work please let me know what is the problem...
You can try this code:
Intent read = new Intent(ctx, NotificationClick.class);
read.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
read.putExtra(Intent.EXTRA_SUBJECT, "READ");
PendingIntent readInt = PendingIntent.getActivity(ctx, 1, read, PendingIntent.FLAG_IMMUTABLE);
Intent reply = new Intent(ctx, NotificationClick.class);
reply.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
reply.putExtra(Intent.EXTRA_SUBJECT, "REPLY");
PendingIntent replyInt = PendingIntent.getActivity(ctx, 2, reply, PendingIntent.FLAG_IMMUTABLE);
NotificationManagerCompat nMgr = NotificationManagerCompat.from(ctx);
Notification newMessageNotification = new NotificationCompat.Builder(ctx, "MESSAGE_CHANNEL")
.setSmallIcon(R.drawable.user_account)
.setContentTitle(contact)
.setContentText(text)
.addAction(R.drawable.drafts, "Read", readInt)
.addAction(R.drawable.drafts, "Reply", replyInt)
.setGroup(MESSAGE_GROUP)
.build();
nMgr.notify(100, newMessageNotification);