Android Notification.Builder: show a notification without icon - android

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ic_notification_icon;
android.app.Notification.Builder nbuilder = new Notification.Builder(this);
nbuilder.setContentTitle(getString(R.string.notifcation_title,mProfile.mName));
nbuilder.setContentText(msg);
nbuilder.setOnlyAlertOnce(true);
nbuilder.setOngoing(true);
nbuilder.setSmallIcon(icon,level.level);
How can I hide or completely delete the smallIcon? I tried to not use nbuilder.setSmallIcon, but the result is that the notification is not shown at all!

On Jelly Bean and later you can use nbuilder.setPriority(Notification.PRIORITY_MIN); the exact interpretation of this priority level is left up to the system UI, but in AOSP this causes the notification's icon to be hidden.
This is intended for "ambient" or "background" information that doesn't need to get the user's attention, but if the user happens to be poking around the notification panel, she might be interested. See the Notifications section of the Android Design site for more about how to best use notification priority.

Update: don't use it on Android 7, it's crashing
Using reflection, here's something that's working in Lollipop (emulators and Moto G device) and Marshmallow (emulator)
Notification notification = NotificationCompat.Builder.blabla().build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int smallIconViewId = context.getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());
if (smallIconViewId != 0) {
if (notification.contentView != null)
notification.contentView.setViewVisibility(smallIconViewId, View.INVISIBLE);
if (notification.headsUpContentView != null)
notification.headsUpContentView.setViewVisibility(smallIconViewId, View.INVISIBLE);
if (notification.bigContentView != null)
notification.bigContentView.setViewVisibility(smallIconViewId, View.INVISIBLE);
}
}
I couldn't find a better way.
This can stop working the moment they change the id of the view or alter any of the RemoteViews fields inside Notification, so use at your own risk.

you can't do that... without setSmallIcon(icon,level.level); Notification is not showing..

Try in your Notification builder adding this line
.setPriority(Notification.PRIORITY_MIN)
So your Notification will be like this :
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.stat_notify_chat)
.setContentTitle("my app")
.setContentText("my app ")
.setPriority(Notification.PRIORITY_MIN)
.setContentIntent(pendingIntent).build();
So in this Case you can see your icon just in Notification bar and it be hide in status bar . hope this help .

You can set your own layout without icon:
RemoteViews notificationView = new RemoteViews(
context.getPackageName(),
R.layout.notify
);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContent(notificationView)

I faced this problem before and solved it like that:
private int getNotificationIcon() {
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.LOLLIPOP);
// Lollipop or Marshmellow >>>>>>>>>>>>>>>>>>>>> KitKat or less
return useWhiteIcon ? R.drawable.logo_new : R.drawable.logo;
}
and just call this function in setSmallIcon()
nbuilder.setSmallIcon(getNotificationIcon());

Related

NotificationManager Using Clear All Notification [duplicate]

Is it possible to clear a notification programatically?
I tried it with the NotificationManager but its not working.
Is there any other way I can do it?
Use the following code to cancel a Notification:
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
In this code there is alway the same id used for notifications. If you have different notifications that need to be canceled you have to save the ids that you used to create the Notification.
From: http://developer.android.com/guide/topics/ui/notifiers/notifications.html
To clear the status bar notification when the user selects it from the Notifications window, add the "FLAG_AUTO_CANCEL" flag to your Notification object. You can also clear it manually with cancel(int), passing it the notification ID, or clear all your Notifications with cancelAll().
But Donal is right, you can only clear notifications that you created.
Since no one has posted a code answer to this:
notification.flags = Notification.FLAG_AUTO_CANCEL;
.. and if you already have flags, you can OR FLAG_AUTO_CANCEL like this:
notification.flags = Notification.FLAG_INSISTENT | Notification.FLAG_AUTO_CANCEL;
Please try methods provided in NotificationManagerCompat.
To remove all notifications,
NotificationManagerCompat.from(context).cancelAll();
To remove a particular notification,
NotificationManagerCompat.from(context).cancel(notificationId);
Starting with API level 18 (Jellybean MR2) you can cancel Notifications other than your own via NotificationListenerService.
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class MyNotificationListenerService extends NotificationListenerService {...}
...
private void clearNotificationExample(StatusBarNotification sbn) {
myNotificationListenerService.cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
}
If you are generating Notification from a Service that is started in the foreground using
startForeground(NOTIFICATION_ID, notificationBuilder.build());
Then issuing
notificationManager.cancel(NOTIFICATION_ID);
does not end up canceling the Notification, and the notification still appears in the status bar. In this particular case, you will need to issue
stopForeground( true );
from within the service to put it back into background mode and to simultaneously cancel the notifications. Alternately, you can push it into the background without having it cancel the notification and then cancel the notification.
stopForeground( false );
notificationManager.cancel(NOTIFICATION_ID);
Notification mNotification = new Notification.Builder(this)
.setContentTitle("A message from: " + fromUser)
.setContentText(msg)
.setAutoCancel(true)
.setSmallIcon(R.drawable.app_icon)
.setContentIntent(pIntent)
.build();
.setAutoCancel(true)
when you click on notification, open corresponding activity and remove notification from notification bar
I believe the most RECENT and UPDATED for AndroidX and backward compatibility. The best way of doing (Kotlin and Java) this should be done as:
NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID)
Or to cancel all notifications is:
NotificationManagerCompat.from(context).cancelAll()
Made for AndroidX or Support Libraries.
If you're using NotificationCompat.Builder (a part of android.support.v4) then simply call its object's method setAutoCancel
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true);
Some guys were reporting that setAutoCancel() did not work for them, so you may try this way as well
builder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
Note that the method getNotification() has been deprecated!!!
// Get a notification builder that's compatible with platform versions
// >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
builder.setSound(soundUri);
builder.setAutoCancel(true);
this works if you are using a notification builder...
Actually as answered before starting with API Level 18 you can cancel Notifications posted by other apps differet than your own using NotificationListenerService but that approach will no longer work on Lollipop, here is the way to remove notifications covering also Lillipop API.
if (Build.VERSION.SDK_INT < 21) {
cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
}
else {
cancelNotification(sbn.getKey());
}
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager Nmang = (NotificationManager) getApplicationContext()
.getSystemService(ns);
Nmang .cancel(getIntent().getExtras().getInt("notificationID"));
All notifications (even other app notifications) can be removed via listening to 'NotificationListenerService' as mentioned in NotificationListenerService Implementation
In the service you have to call cancelAllNotifications().
The service has to be enabled for your application via:
‘Apps & notifications’ -> ‘Special app access’ -> ‘Notifications access’.
this code worked for me:
public class ExampleReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
int notificationId = 1;
notificationManager.cancel(notificationId);
}
}
A function written in Kotlin:
/**
* Delete the notification
*/
fun delete(context: Context, notificationId: Int) =
with(NotificationManagerCompat.from(context)) {
cancel(notificationId)
}
Or shorter:
fun delete(context: Context, notificationId: Int) = NotificationManagerCompat.from(context).cancel(notificationId)
If you use OneSignal, you must use one of this:
Specific notification:
OneSignal.removeNotification(mutableNotification.androidNotificationId)
All notifications:
OneSignal.clearOneSignalNotifications()
In OneSignal's java doc says:
For removeNotification
Cancels a single OneSignal notification based on its Android notification integer ID. Use
* instead of Android's {#link NotificationManager#cancel(int)}, otherwise the notification will be restored
* when your app is restarted.
For clearOneSignalNotifications
If you just use
* {#link NotificationManager#cancelAll()}, OneSignal notifications will be restored when
* your app is restarted.
To clear notifications on Oreo and greater versions
//Create Notification
Notification.Builder builder = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(getString(R.string.app_name))
.setAutoCancel(true);
Notification notification = builder.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
createNotificationChannel(builder, notificationManager);
mNotificationManager=notificationManager;
startForeground(1, notification);
//Remove notification
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationManager.deleteNotificationChannel(NOTIFICATION_CHANNEL_ID);
}

issues in clearing notifications in Android [duplicate]

Is it possible to clear a notification programatically?
I tried it with the NotificationManager but its not working.
Is there any other way I can do it?
Use the following code to cancel a Notification:
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
In this code there is alway the same id used for notifications. If you have different notifications that need to be canceled you have to save the ids that you used to create the Notification.
From: http://developer.android.com/guide/topics/ui/notifiers/notifications.html
To clear the status bar notification when the user selects it from the Notifications window, add the "FLAG_AUTO_CANCEL" flag to your Notification object. You can also clear it manually with cancel(int), passing it the notification ID, or clear all your Notifications with cancelAll().
But Donal is right, you can only clear notifications that you created.
Since no one has posted a code answer to this:
notification.flags = Notification.FLAG_AUTO_CANCEL;
.. and if you already have flags, you can OR FLAG_AUTO_CANCEL like this:
notification.flags = Notification.FLAG_INSISTENT | Notification.FLAG_AUTO_CANCEL;
Please try methods provided in NotificationManagerCompat.
To remove all notifications,
NotificationManagerCompat.from(context).cancelAll();
To remove a particular notification,
NotificationManagerCompat.from(context).cancel(notificationId);
Starting with API level 18 (Jellybean MR2) you can cancel Notifications other than your own via NotificationListenerService.
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class MyNotificationListenerService extends NotificationListenerService {...}
...
private void clearNotificationExample(StatusBarNotification sbn) {
myNotificationListenerService.cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
}
If you are generating Notification from a Service that is started in the foreground using
startForeground(NOTIFICATION_ID, notificationBuilder.build());
Then issuing
notificationManager.cancel(NOTIFICATION_ID);
does not end up canceling the Notification, and the notification still appears in the status bar. In this particular case, you will need to issue
stopForeground( true );
from within the service to put it back into background mode and to simultaneously cancel the notifications. Alternately, you can push it into the background without having it cancel the notification and then cancel the notification.
stopForeground( false );
notificationManager.cancel(NOTIFICATION_ID);
Notification mNotification = new Notification.Builder(this)
.setContentTitle("A message from: " + fromUser)
.setContentText(msg)
.setAutoCancel(true)
.setSmallIcon(R.drawable.app_icon)
.setContentIntent(pIntent)
.build();
.setAutoCancel(true)
when you click on notification, open corresponding activity and remove notification from notification bar
I believe the most RECENT and UPDATED for AndroidX and backward compatibility. The best way of doing (Kotlin and Java) this should be done as:
NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID)
Or to cancel all notifications is:
NotificationManagerCompat.from(context).cancelAll()
Made for AndroidX or Support Libraries.
If you're using NotificationCompat.Builder (a part of android.support.v4) then simply call its object's method setAutoCancel
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true);
Some guys were reporting that setAutoCancel() did not work for them, so you may try this way as well
builder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
Note that the method getNotification() has been deprecated!!!
// Get a notification builder that's compatible with platform versions
// >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
builder.setSound(soundUri);
builder.setAutoCancel(true);
this works if you are using a notification builder...
Actually as answered before starting with API Level 18 you can cancel Notifications posted by other apps differet than your own using NotificationListenerService but that approach will no longer work on Lollipop, here is the way to remove notifications covering also Lillipop API.
if (Build.VERSION.SDK_INT < 21) {
cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
}
else {
cancelNotification(sbn.getKey());
}
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager Nmang = (NotificationManager) getApplicationContext()
.getSystemService(ns);
Nmang .cancel(getIntent().getExtras().getInt("notificationID"));
All notifications (even other app notifications) can be removed via listening to 'NotificationListenerService' as mentioned in NotificationListenerService Implementation
In the service you have to call cancelAllNotifications().
The service has to be enabled for your application via:
‘Apps & notifications’ -> ‘Special app access’ -> ‘Notifications access’.
this code worked for me:
public class ExampleReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
int notificationId = 1;
notificationManager.cancel(notificationId);
}
}
A function written in Kotlin:
/**
* Delete the notification
*/
fun delete(context: Context, notificationId: Int) =
with(NotificationManagerCompat.from(context)) {
cancel(notificationId)
}
Or shorter:
fun delete(context: Context, notificationId: Int) = NotificationManagerCompat.from(context).cancel(notificationId)
If you use OneSignal, you must use one of this:
Specific notification:
OneSignal.removeNotification(mutableNotification.androidNotificationId)
All notifications:
OneSignal.clearOneSignalNotifications()
In OneSignal's java doc says:
For removeNotification
Cancels a single OneSignal notification based on its Android notification integer ID. Use
* instead of Android's {#link NotificationManager#cancel(int)}, otherwise the notification will be restored
* when your app is restarted.
For clearOneSignalNotifications
If you just use
* {#link NotificationManager#cancelAll()}, OneSignal notifications will be restored when
* your app is restarted.
To clear notifications on Oreo and greater versions
//Create Notification
Notification.Builder builder = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(getString(R.string.app_name))
.setAutoCancel(true);
Notification notification = builder.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
createNotificationChannel(builder, notificationManager);
mNotificationManager=notificationManager;
startForeground(1, notification);
//Remove notification
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationManager.deleteNotificationChannel(NOTIFICATION_CHANNEL_ID);
}

NotificationCompat android - how to show only Large icon without small

When I add notification:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.plus)
.setContentTitle(title)
.setAutoCancel(true)
.setContentText(text)
.setSound(RingtoneManager .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setLargeIcon(bm);
I see large icon and small in it:
How can I set only large Icon, without small.
If use only setLargeIcon, I don't see notification at all, just sound alert.
Small icon is mandatory.
If you don't set a large one you'll get your small icon bigger in the middle of the circle with the color of your choice (setColor).
If i were you i'd put that blank E on a transparent background for smallicon, and set a red color for the circle.
get the small icon id and then try to hide it
int smallIconId = ctx.getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());
if (smallIconId != 0) {
if (notification.contentView!=null)
notification.contentView.setViewVisibility(smallIconId, View.INVISIBLE);
}
try looking at this post it will help too
i test the code on api 18,23 (samsung j1,galaxy S6) work fine
Based on the previous answer you can also hide the expended view too :
int smallIconId = AnghamiApp.getContext().getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());
if (smallIconId != 0) {
notification.contentView.setViewVisibility(smallIconId, View.INVISIBLE);
notification.bigContentView.setViewVisibility(smallIconId, View.INVISIBLE);
}
You can create a custom notification and then show whatever you want in the large notification area.
See and example here TutorialsFace: Build all types of Notifications in Android
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Test")
.setContentText("Hii There")
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.smallicon))
.setAutoCancel(true)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(123, notification);
}
}

Android notification large icon, is there a way to remove the smaller icon on the bottom right?

I have a notification that displays a largeicon.
Is there any way to remove the smaller icon from honeycomb and above devices from this view?
Obviously still keeping the small icon for the top status bar
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
// Set required fields, including the small icon, the
// notification title, and text.
.setSmallIcon(R.drawable.ic_notify_status_new)
.setContentTitle(title)
.setContentText(text)
// All fields below this line are optional.
// Use a default priority (recognized on devices running Android
// 4.1 or later)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Provide a large icon, shown with the notification in the
// notification drawer on devices running Android 3.0 or later.
.setLargeIcon(picture)
.setContentIntent(
PendingIntent.getActivity(
context,
0,
notiIntent,
PendingIntent.FLAG_UPDATE_CURRENT)
);
builder = getNotiSettings(builder);
notify(context, builder.build());
Add this code after you build the notification.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int smallIconViewId = getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());
if (smallIconViewId != 0) {
if (notif.contentIntent != null)
notif.contentView.setViewVisibility(smallIconViewId, View.INVISIBLE);
if (notif.headsUpContentView != null)
notif.headsUpContentView.setViewVisibility(smallIconViewId, View.INVISIBLE);
if (notif.bigContentView != null)
notif.bigContentView.setViewVisibility(smallIconViewId, View.INVISIBLE);
}
}
where "notif" is your built notification,
There is only one way:
Use your custom layout
Your question is probable a duplicate of NotificationCompat 4.1 SetSmallIcon and SetLargeIcon
Use only setSmallIcon for Your icon. It will be also used for largeIcon if second wasn't specified (just make sure it is big enough to fit both).
Make sure that it looks good on pre-lolipop, lolipop and above. For example here I do set application icon only for pre-lolipop devices, where lolipop and above are getting different smallIcon and same largeIcon as smallIcon.
int currentApiVersion = android.os.Build.VERSION.SDK_INT;
if (currentApiVersion >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder
.setSmallIcon(smallIcon)
.setLargeIcon(appIconDrawable)
} else {
notificationBuilder.setSmallIcon(appIconDrawable);
}

How to clear a notification in Android

Is it possible to clear a notification programatically?
I tried it with the NotificationManager but its not working.
Is there any other way I can do it?
Use the following code to cancel a Notification:
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
In this code there is alway the same id used for notifications. If you have different notifications that need to be canceled you have to save the ids that you used to create the Notification.
From: http://developer.android.com/guide/topics/ui/notifiers/notifications.html
To clear the status bar notification when the user selects it from the Notifications window, add the "FLAG_AUTO_CANCEL" flag to your Notification object. You can also clear it manually with cancel(int), passing it the notification ID, or clear all your Notifications with cancelAll().
But Donal is right, you can only clear notifications that you created.
Since no one has posted a code answer to this:
notification.flags = Notification.FLAG_AUTO_CANCEL;
.. and if you already have flags, you can OR FLAG_AUTO_CANCEL like this:
notification.flags = Notification.FLAG_INSISTENT | Notification.FLAG_AUTO_CANCEL;
Please try methods provided in NotificationManagerCompat.
To remove all notifications,
NotificationManagerCompat.from(context).cancelAll();
To remove a particular notification,
NotificationManagerCompat.from(context).cancel(notificationId);
Starting with API level 18 (Jellybean MR2) you can cancel Notifications other than your own via NotificationListenerService.
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class MyNotificationListenerService extends NotificationListenerService {...}
...
private void clearNotificationExample(StatusBarNotification sbn) {
myNotificationListenerService.cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
}
If you are generating Notification from a Service that is started in the foreground using
startForeground(NOTIFICATION_ID, notificationBuilder.build());
Then issuing
notificationManager.cancel(NOTIFICATION_ID);
does not end up canceling the Notification, and the notification still appears in the status bar. In this particular case, you will need to issue
stopForeground( true );
from within the service to put it back into background mode and to simultaneously cancel the notifications. Alternately, you can push it into the background without having it cancel the notification and then cancel the notification.
stopForeground( false );
notificationManager.cancel(NOTIFICATION_ID);
Notification mNotification = new Notification.Builder(this)
.setContentTitle("A message from: " + fromUser)
.setContentText(msg)
.setAutoCancel(true)
.setSmallIcon(R.drawable.app_icon)
.setContentIntent(pIntent)
.build();
.setAutoCancel(true)
when you click on notification, open corresponding activity and remove notification from notification bar
I believe the most RECENT and UPDATED for AndroidX and backward compatibility. The best way of doing (Kotlin and Java) this should be done as:
NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID)
Or to cancel all notifications is:
NotificationManagerCompat.from(context).cancelAll()
Made for AndroidX or Support Libraries.
If you're using NotificationCompat.Builder (a part of android.support.v4) then simply call its object's method setAutoCancel
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true);
Some guys were reporting that setAutoCancel() did not work for them, so you may try this way as well
builder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
Note that the method getNotification() has been deprecated!!!
// Get a notification builder that's compatible with platform versions
// >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
builder.setSound(soundUri);
builder.setAutoCancel(true);
this works if you are using a notification builder...
Actually as answered before starting with API Level 18 you can cancel Notifications posted by other apps differet than your own using NotificationListenerService but that approach will no longer work on Lollipop, here is the way to remove notifications covering also Lillipop API.
if (Build.VERSION.SDK_INT < 21) {
cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
}
else {
cancelNotification(sbn.getKey());
}
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager Nmang = (NotificationManager) getApplicationContext()
.getSystemService(ns);
Nmang .cancel(getIntent().getExtras().getInt("notificationID"));
All notifications (even other app notifications) can be removed via listening to 'NotificationListenerService' as mentioned in NotificationListenerService Implementation
In the service you have to call cancelAllNotifications().
The service has to be enabled for your application via:
‘Apps & notifications’ -> ‘Special app access’ -> ‘Notifications access’.
this code worked for me:
public class ExampleReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
int notificationId = 1;
notificationManager.cancel(notificationId);
}
}
A function written in Kotlin:
/**
* Delete the notification
*/
fun delete(context: Context, notificationId: Int) =
with(NotificationManagerCompat.from(context)) {
cancel(notificationId)
}
Or shorter:
fun delete(context: Context, notificationId: Int) = NotificationManagerCompat.from(context).cancel(notificationId)
If you use OneSignal, you must use one of this:
Specific notification:
OneSignal.removeNotification(mutableNotification.androidNotificationId)
All notifications:
OneSignal.clearOneSignalNotifications()
In OneSignal's java doc says:
For removeNotification
Cancels a single OneSignal notification based on its Android notification integer ID. Use
* instead of Android's {#link NotificationManager#cancel(int)}, otherwise the notification will be restored
* when your app is restarted.
For clearOneSignalNotifications
If you just use
* {#link NotificationManager#cancelAll()}, OneSignal notifications will be restored when
* your app is restarted.
To clear notifications on Oreo and greater versions
//Create Notification
Notification.Builder builder = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(getString(R.string.app_name))
.setAutoCancel(true);
Notification notification = builder.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
createNotificationChannel(builder, notificationManager);
mNotificationManager=notificationManager;
startForeground(1, notification);
//Remove notification
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationManager.deleteNotificationChannel(NOTIFICATION_CHANNEL_ID);
}

Categories

Resources