Prevent notification drawer from collapsing after button click - android

I have a media player application with a control notification, play, next, previous.
When I click on one of these buttons, the complete notification drawer collapses.
How do I prevent the collapsing?
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Intent intent = new Intent(context, NotificationPlayerControlReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
builder.setPriority(Notification.PRIORITY_MAX);
builder.setAutoCancel(false);
builder.setContentTitle(title);
builder.setContentText(interpret);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.addAction(R.drawable.av_previous, "", pIntent);
builder.addAction(R.drawable.av_play, "", pIntent);
builder.addAction(R.drawable.av_next, "", pIntent);
builder.setOngoing(true);
return builder.build();

This is the way things are supposed to work when the user clicks on a button that fires a PendingIntent created with PendingIntent.getActivity(); presumably if a button in a notification navigates to a new activity, you want the notification panel to get out of the way so you can see it.
If you want to implement transport controls in your notification, use PendingIntent.getBroadcast or .getService().

Related

How to send the notification bar back up programatically?

I researched and the only answers I could find either told:
How to disable the notification bar from being pulled down.
How to cancel a notification using:
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(0);
Both of these aren't what I require as the notification gets cancelled fine. I've set two actions in my notification: 'Dismiss' and 'Open Activity'. As the name implies, upon clicking both, the above code executes and clears the notification but doesn't cause the notification bar from going back up. This is needed, specially when the second Notification Action causes an Activity launch.
I tried on Android Lollipop and Nougat and the notification bar didn't go back up in either. So if someone could kindly tell me if it is even possible and how.
Thanks.
The code for building the notification:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder
.setSmallIcon(icon).setTicker(message).setWhen(when)
.setAutoCancel(true).setContentTitle("Kindly record your Voice")
.setColor(Color.RED);
notificationIntent = new Intent(this,ReminderReceiver.class);
notificationIntent.setAction("Record");
PendingIntent pendIntent1 = PendingIntent.getBroadcast(getApplicationContext(), 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(R.drawable.common_google_signin_btn_icon_dark, "Record", pendIntent1);
notificationIntent2 = new Intent(this, ReminderReceiver.class);
notificationIntent2.setAction("Dismissed");
PendingIntent pendIntent2 = PendingIntent.getBroadcast(getApplicationContext(), 1, notificationIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(R.drawable.starticon, "Dismiss", pendIntent2);
notificationIntent3 = new Intent(this, CancelReceiver.class);
PendingIntent pendIntent3 = PendingIntent.getBroadcast(getApplicationContext(), 1, notificationIntent3, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setDeleteIntent(pendIntent3);
notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1, notification);
Might seem weird, but the notification bar doesn't go up when there are other notifications present. If yours is the only notification, the bar slides up automatically upon clicking.

Handling clicks on notification button and the notification part itself in android

I've a notification which says "Now Playing : Radio " and it has a stop button below this. When the user clicks on stop button, the audio stops and now what I'm trying to achieve is when the user clicks on the notification part above the stop button I want to open a fragment. I'm showing the notification like this.
Intent notIntent = new Intent();
notIntent.setAction(ACTION_STOP);
PendingIntent pendInt = PendingIntent.getService(this, (int) System.currentTimeMillis(),
notIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification n = builder.setContentIntent(pendInt)
.setSmallIcon(R.drawable.music_nepal_not)
.setColor(ContextCompat.getColor(BackgroundAudioService.this, R.color.colorPrimary))
.setTicker("Playing Live Radio")
.setContentTitle("Now Playing : Live Radio ")
.addAction(R.layout.toggle_notification, "", pendInt)
.build();
notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
n.flags |= Notification.FLAG_NO_CLEAR;
notificationManager.notify(0, n);
You can do something like:
n.addContentIntent(pendingIntent);
This would get triggered when the user clicks on the notification body. pendingIntent would be a PendingIntent to your Activity hosting the fragment since, those bastards cannot exist without activities. Similarly, you can add that pendingIntent to your addAction().

notification disappear when home button clicked

MY application displays notification when it is running. But when I click the home button (the application is still running in the background) the notification disappears.
What should I do to keep it as is until the user clicks on it.
Thanks
private void showNotification() {
// The PendingIntent to launch our activity if the user selects this
// notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
serviceIntent, 0);
Notification notification = new Notification.Builder(this)
.setTicker("Accelormeter app").setContentTitle("Acc")
.setContentText("Notification content.")
.setSmallIcon(R.drawable.noti_icon).setContentIntent(contentIntent)
.getNotification();
// Send the notification.
mNM.notify(0, notification);
}
Don't use getNotification(); instead use build(); in the Notification.Builder

How to implement onclick() on buttons in custom notification

I am trying to create a custom notification with the help of remote view with a button on it. I want to write onclick listener for this button but the problem is that the notification container is catching the touch event and button is not being clicked. My question is Is there any way to disable this notification and let the button to be clicked. My code is something like below:
Notification notification = new Notification(R.drawable.applogo,
"Apps activated", System.currentTimeMillis());
Intent notificationIntent = new Intent(getBaseContext(), AppsActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(),
0, notificationIntent , 0);
notification.contentIntent = pendingIntent;
//Remoteview and intent for my button
RemoteViews remoteView = new RemoteViews(getBaseContext().getPackageName(),
R.layout.customnotification);
Intent mIntent = getPackageManager().getLaunchIntentForPackage(c.getString(1));//starting some application through package
PendingIntent pintent = PendingIntent.getActivity(this, 0,
mIntent, 0);
remoteView.setOnClickPendingIntent(R.id.button1,
pintent);
notification.contentView = remoteView;
notificationManager.notify(CUSTOM_NOTIFICATION_ID, notification);
I am working on api 7.
Thanks
Unfortunately, clickable buttons within notification layout available only with Custom notifications for API >= 11.
Also, for API >= 16 you can use Notification Actions to provide buttons within notification.

Displaying a notification in android

I am using the following code to show a notification in my android application, but using this, with the notification an activity also shows up (i.e. a black blank screen) I dont want this screen, but just a simple notification and when the user clicks the notification then I want to launch the activity. What should be done to this code?
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification(R.drawable.mcube_logo, "Message Sent", Calendar.getInstance().getTimeInMillis());
Intent intent = new Intent(RecieveAlarm.this, otherActivity.class);
PendingIntent pendingIntent =PendingIntent.getActivity(RecieveAlarm.this, 1, null, PendingIntent.FLAG_CANCEL_CURRENT);
notify.setLatestEventInfo(RecieveAlarm.this, "Title", "Details", pendingIntent);
notificationManager.notify(0, notify);
I suggest you to use:
Notification not = new Notification(idIcon, text, System.currentTimeMillis());
PendingIntent pInt = PendingIntent.getActivity(ctx, 0, new Intent(ctx, the_class_to_call), 0);
not.setLatestEventInfo(ctx, app_name, text, pInt);
and then not.notify....
use AlertDialog, then launch your desired activity on positive button click

Categories

Resources