I have a notification using a remoteView. On the notification I have 3 buttons, each of them with their own pendingIntent. For some reason only the last pendingIntent that I add with setOnClickPendingIntent works.
Here is some of the
Intent intentPause = new Intent(context, BluetoothConnectionService.class);
intentPause.putExtra("pause", device.device.getAddress());
Intent intentIncrease = new Intent(context, BluetoothConnectionService.class);
intentIncrease.putExtra("inc", device.device.getAddress());
PendingIntent pendingIntentPause = PendingIntent.getService(context, 0, intentPause, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntentIncrease = PendingIntent.getService(context, 0, intentIncrease, PendingIntent.FLAG_CANCEL_CURRENT);
RemoteViews expandedView = new RemoteViews(context.getPackageName(), R.layout.notification_layout);
expandedView.setOnClickPendingIntent(R.id.noti_pause_heat_button, pendingIntentPause);
expandedView.setOnClickPendingIntent(R.id.noti_inc_heat_button, pendingIntentIncrease);
Notification notification = new Notification.Builder(context)
.setContentTitle(this.device.getName())
.setSmallIcon(R.drawable.inu_small)
.setContentText("" + this.notificationId)
.setContent(expandedView)
.build();
use a different requestCode (second parameter of getService) for each PendingIndent. E.g.
PendingIntent pendingIntentPause = PendingIntent.getService(context, 0, intentPause, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntentIncrease = PendingIntent.getService(context, 1, intentIncrease, PendingIntent.FLAG_UPDATE_CURRENT);
Related
I am creating a custom notification in service (MusicService) and adding buttons as follow.
//Notification
Intent notIntent = new Intent(this, MainActivity.class);
notIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendInt = PendingIntent.getActivity(this, 0,
notIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//Play
Intent playIntent = new Intent(this, MusicService.class);
playIntent.setAction(ACTION_PLAY);
PendingIntent pPlayIntent = PendingIntent.getService(this, 1,
playIntent, 0);
//Pause
Intent pauseIntent = new Intent(this, MusicService.class);
pauseIntent.setAction(ACTION_PAUSE);
PendingIntent pPauseIntent = PendingIntent.getService(this, 1,
playIntent, 0);
//Previous
Intent previousSongIntent = new Intent(this, MusicService.class);
previousSongIntent.setAction(ACTION_PREVIOUS);
PendingIntent pPreviousSongIntent = PendingIntent.getService(this, 1,
previousSongIntent, 0);
//Next
Intent nextSongIntent = new Intent(this, MusicService.class);
nextSongIntent.setAction(ACTION_NEXT);
PendingIntent pNextSongIntent = PendingIntent.getService(this, 1,
nextSongIntent,0);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setOngoing(true)
.setContentText(songTitle)
.setContentIntent(pendInt)
.setPriority(Notification.PRIORITY_MAX)
.setWhen(0)
.addAction(R.drawable.ic_play_button, "PLAY", pPlayIntent)
.addAction(R.drawable.ic_pause_button, "PAUSE", pPauseIntent)
.addAction(R.drawable.ic_next_button, "NEXT", pNextSongIntent)
.addAction(R.drawable.ic_previous_button, "PREV", pPreviousSongIntent);
Notification notification = builder.build();
startForeground(NOTIFY_ID, notification);
Buttons icons are not displayed in notification.
Using two fingers and swiping down on notification display text (PLAY PAUSE NEXT).
Am I missing something?
Use MediaStyle with setShowActionsInCompactView
builder.setStyle(androidx.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2))
Full example here https://code.videolan.org/videolan/vlc-android/-/blob/master/application/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.kt#L99
I have this function to create a notification:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)
.setSound(alarmSound)
.setContentText(subtitle);
if(haveIntent) {
Intent intent = new Intent(context, PSBeaconMainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("idToPass", beaconSettingID);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
mBuilder.setContentIntent(pIntent);
}
int mNotificationId = 001;
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
I send for now a hardcoded value for the beaconSettingID such as this:
https://scontent-amt2-1.xx.fbcdn.net/v/t35.0-12/28822214_1907084479363661_711965391_o.png?oh=b4c247836bcbd572d27f53738d2bdf6c&oe=5AA0C75E
This is my getIntent.getBundle:
if (bundle != null && bundle.containsKey("idToPass")) {
beaconUUID = bundle.getString("idToPass");
}
But I get back this:
https://scontent-amt2-1.xx.fbcdn.net/v/t35.0-12/28768405_1907084916030284_1955200830_o.png?oh=bcccf27352c90b248e82e08d902b8c15&oe=5AA1BA02
I don't understand? beaconID was the name of the "extra" key before. Since then I deleted the app, and changed it to idToPass. But I still get the old key, and more than that, a wrong value for it. Why is this happening?
Deleting an app doesn't mean that you deleted the previous notification.
Try to change
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
to:
PendingIntent pIntent = PendingIntent.getActivity(context, SOME_UNIQUE_ID, intent, 0);
In order to create new
Or to:
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
To update previous notification
Hope it helps
I am currently working on a reminders app in which the user gets a notification with the name of the reminder and is then redirected to an activity which contains the text of the reminder in detail.
I am however, only able to redirect to the same activity each time.
I am using this code :
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
notification.setLatestEventInfo(this, title, text, contentIntent);
So this redirects to the MainActivity on clicking the notification. I would like to redirect to a separate screen and then based on a key value display a text on that activity.
How do I achieve this ?
Thanks
Just change the PendingIntent using another Activity and/or append extra information on the Intent you are using to create the PendingIntent:
Intent launchIntent = new Intent(this, AnotherActivity.class)
launchIntent.putExtra("myKey", "myValue");
//....
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, launchIntent , 0);
notification.setLatestEventInfo(this, title, text, contentIntent);
And than, in you Activity's onCreate():
//...
getIntent().getStringExtra("myKey")
//do your stuff..
Just pass the value in the intent . eg.
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
contentIntent.putExtra("phone", value);
contentIntent.putExtra("name", value);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, contentIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
Question has already been anwered and accepted, but here's another method:
int mId = 1;
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, TwoActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(this)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title")
.setContentText("Content")
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setNumber(1);
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(mId, builder.build());
I'm trying to make notification actions for Android Wear,
And I set an PendingIntent (.getService) for every action,
But only the first Action actually work.
My code (variables names are examples):
intent1 = new Intent(this, intent1.class);
Intent intent2 = new Intent(this, intent2.class);
Intent intent3 = new Intent(this, intent3.class);
pendingIntent = PendingIntent.getService(this, 0, intent1, 0);
pendingIntent2 = PendingIntent.getService(this, 0, intent2, 0);
pendingIntent3 = PendingIntent.getService(this, 0, intent3, 0);
Only clicking on the "pendingIntent" action, it workes.
When clicking on "pendingIntent2" or "pendingIntent3" action, they don't work.
Can you help? Thanks.
I am trying to open dialog on click of notification but unable to do this:
Here's my code:
Intent in = new Intent(context, SnoozeEvent.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent Sender = PendingIntent.getBroadcast(context, 0, intent, 0);
manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notification = new Notification(R.drawable.icon, "Wake up alarm", System.currentTimeMillis());
notification.setLatestEventInfo(context, "Hanuman Chalisa", "Wake Up...", Sender);
manager.notify(1, notification);
The error is that you point the PendingIntent to an intent that doesn't exist (it points to an intent called "intent" - you created an intent called "in").
Replace the following line:
PendingIntent Sender = PendingIntent.getBroadcast(context, 0, intent, 0);
With this (so it points to the intent you created):
PendingIntent Sender = PendingIntent.getBroadcast(context, 0, in, 0);
If you do that, then everything should work fine.