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
Related
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.
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().
In Android How to Restart App On Click Application "onGoing" Notification. if App is open or not.
Like As When i Click on onGoing Notification "Connected as a Media device"
You can define actions you want to happen when interacting with your Notification by adding a PendingIntent.
In the following example a PendingIntent is created to launch the (current) activity.
That intent is then added to the notification in the content section. Once this notification is shown, when you click the content section, the intent is fired and the app gets started or brought back to top.
private static final int NOTIFICATION_ID = 1;
...
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder nb = new NotificationCompat.Builder(this);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP),
0);
nb.setSmallIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha)
.setCategory(NotificationCompat.CATEGORY_STATUS)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentTitle(getText(R.string.app_name))
.setContentText("Click to launch!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent) // Here the "onClick" Intent is added
.setOngoing(false);
nm.notify(NOTIFICATION_ID, nb.build());
In this case the notification is dismissable. If you set .setOngoing(true) you need to remove it by calling .cancel(NOTIFICATION_ID) on an instance of the NotificationManager.
See also this introduction on how to Build a Notification.
In my Android app I send Notification like that:
Intent intent = new Intent(ctx, MyActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(ctx, myActivityId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
int icon = R.drawable.ic_launcher;
Notification n = new Notification.Builder(ctx)
.setContentTitle(messageTitle)
.setContentText(messageSubtitle)
.setSmallIcon(icon)
.setContentIntent(pIntent)
.setAutoCancel(true).getNotification();
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(myActivityId, n);
Now when I tap on notification I go to MyActivity activity. I usualy send couple notification at once. When I tap on first notification and then immediately tap on second notification I will have 2 views in my views stack. I mean when I tap back on my phone from second notification activity I'm back to first notification activity. I want to second notification activity replace first notifitaction activity that when I tap back I go to view which was displayed before I started taping on notifications. How can I do that?
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().