Reputation: 622
In my app i have notification with pause/play action, however, when user clicks on the action, notification bar is closing. How can i prevent notification bar from closing upon action click?
Method which creates notifcation:
public void createNotification() {
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
Intent intent2 = new Intent(this, IntentActivity.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
pIntent2 = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent2, 0);
builder = new NotificationCompat.Builder(context);
Notification noti = builder
.setAutoCancel(true)
.setContentTitle("Title")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setPriority(Notification.PRIORITY_MAX)
.setStyle(new NotificationCompat.BigTextStyle().bigText("Czas trwania: " + time + "\nKalorie: 4kcal"))
.addAction(icon, actionString, pIntent2).build();
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
}
Upvotes: 2
Views: 2283
Reputation: 4477
I solved a similar problem.
For every notification action used PendingIntent. It seems that if you create PendingIntent from PendingIntent.getActivity(...) - it closes notification. If you create from PendingIntent.getService(...) or from PendingIntent.getBroadcast(...) - notification remains.
So if you want to start activity from notification and don't hide notification: send intent to Service or send broadcast, then start activity from there.
But from there starts another problem: launched activity displays under notification and I don't know how to bring it to top.
Upvotes: 1
Reputation: 11
I'm kinda late to the party but it might help others facing this topic.
As Anrimian pointed out, building PendingIntent via getActivity(Context, int, Intent, int)
closes the notification panel upon action click and there is not much you can do about it as far as i know, it's just how it works.
But building the PendingIntent
with getBroadcast(Context, int, Intent, int)
or getService(Context, int, Intent, int)
does what you are looking for.
So let's say you build a PendingIntent
as follows:
val playIntent = Intent("ACTION_PLAY_INTENT")
val requestCode = 1
val playPendingIntent = PendingIntent.getBroadcast(context, requestCode, playIntent, PendingIntent.FLAG_UPDATE_CURRENT)
Then you need to add the PendingIntent
to NotificationCompat.Builder
with:
.addAction(icon, actionString, playPendingIntent)
In your activity you can declare a receiver like this:
private val playBroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
// parse your actions here...
}
}
Finally you can register by:
registerReceiver(playBroadcastReceiver, IntentFilter("ACTION_PLAY_INTENT"))
The result is that you perform actions without closing the notification panel. I've tested this approach also with RemoteViews and works just fine.
Upvotes: 0
Reputation: 2069
While building Notification by NotificationBuilder
you can use builder.setAutoCancel(false);
.
Upvotes: 0
Reputation: 144
change your code from
setAutoCancel(true)
to
setAutoCancel(false)
Upvotes: 3