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

wkyowqbh  于 2022-12-21  发布在  Android
关注(0)|答案(5)|浏览(160)

如何以编程方式从应用程序中移除通知(使用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);

但问题是通知已经激发,但未从通知栏中删除。

bq8i3lrv

bq8i3lrv1#

或许试试这个:

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

或者,您也可以执行此操作以取消给定上下文中的所有通知:

notificationManager.cancelAll();

请参阅文档的以下链接:通知管理器

llew8vvj

llew8vvj2#

我认为最更新的方法(Kotlin和Java)应该是:

NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID)

或取消所有通知:

NotificationManagerCompat.from(context).cancelAll()

适用于AndroidX或支持库。

wsewodh2

wsewodh23#

通知保持可见,直到发生以下情况之一:
用户单独或通过使用“全部清除”来关闭通知(如果可以清除通知)。用户单击通知,您调用setAutoCancel()。您调用取消()。此方法还删除正在进行的通知。调用cancelAll(),它将删除您以前发出的所有通知。如果在创建通知时使用setTimeoutAfter设置超时(),系统将在指定的持续时间过后取消通知。如果需要,您可以在指定的超时持续时间过后取消通知

public void cancelNotification() {

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

qlvxas9a4#

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

toe950275#

对于使用(通过子类)Service类并使用以下命令运行它的用户:

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

你可以这样关闭它:

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

相关问题