如何删除通知从通知栏编程在机器人?

任何人都知道我们如何能够从应用程序中删除通知,这被称为使用挂起意图。

我使用以下方法取消通知。

AlarmManager am=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(Display.this, TwoAlarmService.class);
PendingIntent pi = PendingIntent.getBroadcast(Display.this, AlarmNumber, intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.cancel(pi);

但问题是已经触发的通知没有从通知栏中删除。

先谢谢你。

enter image description here

105038 次浏览

Maybe try this :

NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);

OR, you can also do this to cancel all notifications in given context:

notificationManager.cancelAll();

See this link to the documentation : NotificationManager

Notifications remain visible until one of the following happens:

The user dismisses the notification either individually or by using "Clear All" (if the notification can be cleared). The user clicks the notification, and you called setAutoCancel() when you created the notification. You call cancel() for a specific notification ID. This method also deletes ongoing notifications. You call cancelAll(), which removes all of the notifications you previously issued. If you set a timeout when creating a notification using setTimeoutAfter(), the system cancels the notification after the specified duration elapses. If required, you can cancel a notification before the specified timeout duration elapse

public void cancelNotification() {


String ns = NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) getActivity().getApplicationContext().getSystemService(ns);
nMgr.cancel(NOTIFICATION_ID);
}

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’.

Add to manifest:

    <activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:label="Test App" android:name="com.test.NotificationListenerEx" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>

Then in code;

public class NotificationListenerEx extends NotificationListenerService {




public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
NotificationListenerEx.this.cancelAllNotifications();
}
};




@Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
}


@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
super.onNotificationRemoved(sbn);
}


@Override
public IBinder onBind(Intent intent) {
return super.onBind(intent);
}


@Override
public void onDestroy() {
unregisterReceiver(broadcastReceiver);
super.onDestroy();
}


@Override
public void onCreate() {
super.onCreate();
registerReceiver(broadcastReceiver, new IntentFilter("com.test.app"));
}

After that use the broadcast receiver to trigger the clear all.

As per the above code to trigger the broadcast use;

getContext().sendBroadcast(new Intent("com.test.app"));

I believe the most RECENT and UPDATED 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.

For those who used (via subclass) the Service class and run it using:

final Intent serviceIntent = new Intent(context, YourNotificationService.class);
ContextCompat.startForegroundService(context, serviceIntent);

you can close it like so:

context.stopService(new Intent(context, YourNotificationService.class));