I have a system notification in my application which will notify the user on receiving a certain event when the application is in background. When I open the app by clicking the notification, app opens fine and dismisses the notification. However, when I open the app using the app icon(either from notification bar or home screen) notification is not dismissed.
Is there a way to dismiss notification (if it is visible in notification bar) on starting the app?
Please find my notification code below:
final int notificationId = 9999;
final NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notification title")
.setContentText("Notification text")
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MyActivity.class), 0));
final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notificationId, mBuilder.build());
Just do this:
mNotificationManager.cancel(notificationId);
or cancel all your app's notifications:
mNotificationManager.cancelAll();
Related
My application is running as a service so, I need to notify the user as application is running. I am trying to add a notification on the notification bar. Since it is a service application I need to always show the notification.
How can I prevent the user to remove the notification even when the application is quit.
I able to prevent the user to remove the application on swipe by using,
mBuilder.setOngoing(true);
How to show the notification even the application quit.
I have used the below code ,
private void addNotificaiononNotificationBar(){
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, MainActivity.class);
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setOngoing(true);
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
If you want to restrict removing notification you can create Foreground Service so notification will exist when service is running.
I wrote the below method for showing FCM notification on my android 5.1 device. When I run the code inside a FirebaseMessagingService it is just giving single line notification , where if I run the same code inside my Activity its giving expandable notifications.
I basically need my long FCM text notifications to get expanded on notifications rather than showing partial notification text.
Any leads?
private void showNotif(String messageBody){
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
// Constructs the Builder object.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission
.setAutoCancel(true)
.setContentIntent(pendingIntent)
/*
* Sets the big view "big text" style and supplies the
* text (the user's reminder message) that will be displayed
* in the detail area of the expanded notification.
* These calls are ignored by the support library for
* pre-4.1 devices.
*/
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(messageBody));
// android.support.v4.app.NotificationManagerCompat mNotifManager = (NotificationManagerCompat) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
}
This should work .
Big Text notifcations are shown in normal mode when there are other notifications on top . Try to expand it while testing.
You need to creare notification using this method https://fcm.googleapis.com/fcm/send and not using firebase console and all would be ok
I'm using this code to create a Heads Up notification.
private static void showNotificationNew(final Context context,final String title,final String message,final Intent intent, final int notificationId, final boolean isHeaderNotification) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context.getApplicationContext())
.setSmallIcon(R.drawable.prime_builder_icon)
.setPriority(Notification.PRIORITY_DEFAULT)
.setCategory(Notification.CATEGORY_MESSAGE)
.setContentTitle(title)
.setContentText(message)
.setWhen(0)
.setTicker(context.getString(R.string.app_name));
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
notificationBuilder.setContentText(message);
if(isHeaderNotification) {
notificationBuilder.setFullScreenIntent(fullScreenPendingIntent, false);
}
notificationBuilder.setContentIntent(fullScreenPendingIntent);
notificationBuilder.setAutoCancel(true);
Notification notification = notificationBuilder.build();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(notificationId, notification);
}
The thing is, notification should appear occupying a good part of top screen to call user attention, but after a couple seconds it should dismiss and a normal notification should appear.
But this code doesn't do that. The notification stay occupying all the top screen until user dismiss it.
I'm thinking in create another normal notification with same ID after a couple of seconds using Handler, but I want to know if there is a better way to do this.
Follow an example of WhatsApp, simulating the behavior I want.
The issue is caused because you use setFullScreenIntent:
An intent to launch instead of posting the notification to the status
bar. Only for use with extremely high-priority notifications demanding
the user's immediate attention, such as an incoming phone call or
alarm clock that the user has explicitly set to a particular time. If
this facility is used for something else, please give the user an
option to turn it off and use a normal notification, as this can be
extremely disruptive.
Also as explained in this answer you should use setVibrate to make Heads-up work.
This is an example of working Heads-up notification:
private static void showNotificationNew(final Context context, final String title, final String message, final Intent intent, final int notificationId) {
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context.getApplicationContext())
.setSmallIcon(R.drawable.small_icon)
.setPriority(Notification.PRIORITY_HIGH)
.setContentTitle(title)
.setContentText(message)
.setVibrate(new long[0])
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(notificationId, notificationBuilder.build());
}
On lollipop, If the user has a pin set then some notifications aren't able to be swiped away. they act like a persistent notification when attempting to be dismissed.
Some apps notifications are able to be swiped away on the lockscreen without unlocking.
I have only tested this with hide sensitive notification content, does a flag set change this ability?
How do I achieve this?
This flag makes your notification stick:
Notification.FLAG_ONGOING_EVENT;
If you leave it out you can remove it. If you put it in, it sticks.
FYI: How I make my notifications:
//These are parameters for setting up the tag in the tray
private static final String NOTIFICATION_ID_TAG="notificationID";
private static final int NOTIFICATION_ID=123456;
public void createNotification() {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, MainActivity_Host.class);
intent.putExtra(NOTIFICATION_ID_TAG, NOTIFICATION_ID);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Build notification
Notification noti = new Notification.Builder(this)
.setContentTitle("Service Running")
.setContentText("The service is running").setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//THIS FLAG MAKES THE NOTIFICATION STICK = YOU CAN'T SWIPE IT AWAY... IF YOU LEAVE IT OUT YOU CAN REMOVE THE NOTIFICATION
noti.flags |= Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(NOTIFICATION_ID, noti);
}
This is how you can remove the notification from within the code:
//Erase the notification that we set up when the service started
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
As you see, you can get the notification from the NotificationManager by the NOTIFICATION_ID you used to make it. This is just a number i made up.
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.