如何在android中以编程方式从通知栏中删除通知?

时间:2013-10-09 09:43:21

标签: android notifications android-notifications android-pendingintent

任何人都知道如何以编程方式从应用程序中删除使用Pending intent调用的通知。

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

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

5 个答案:

答案 0 :(得分:166)

也许试试这个:

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

或者,您也可以在给定的上下文中取消所有通知:

notificationManager.cancelAll();

请参阅此文档的链接:NotificationManager

答案 1 :(得分:2)

我认为最最近已更新的操作方式(科特林和Java)应该通过以下方式完成:

NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID)

或者取消所有通知是:

NotificationManagerCompat.from(context).cancelAll()

适用于AndroidX或支持库。

答案 2 :(得分:1)

在下列情况之一发生之前​​,通知仍然可见:

用户单独或使用"全部清除"来解除通知。 (如果通知可以清除)。 用户单击通知,并在创建通知时调用setAutoCancel()。 您可以为特定通知ID调用cancel()。此方法还会删除正在进行的通知。 您调用cancelAll(),它会删除您之前发出的所有通知。 如果在使用setTimeoutAfter()创建通知时设置超时,系统会在指定的持续时间过后取消通知。如果需要,您可以在指定的超时持续时间过去之前取消通知

public void cancelNotification() {

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

答案 3 :(得分:1)

可以通过听NotificationListenerService Implementation

中提到的“ NotificationListenerService”来删除所有通知(甚至是其他应用程序通知)

在服务中,您必须致电cancelAllNotifications()

必须通过“应用和通知”->“特殊应用访问”->“通知访问”为您的应用启用该服务。

添加到清单:

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

然后输入代码;

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"));
}

之后,使用广播接收器触发全部清除。

按照上述代码触发广播使用;

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

答案 4 :(得分:1)

对于那些使用(通过子类)Service 类并使用它运行的人:

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

你可以像这样关闭它:

context.stopService(new Intent(context, YourNotificationService.class))
相关问题