【问题标题】:How to remove notification from notification bar programmatically in android?如何在android中以编程方式从通知栏中删除通知?
【发布时间】:2013-10-16 14:25:03
【问题描述】:

任何人都知道我们如何以编程方式从应用程序中删除通知,该通知是使用待定意图调用的。

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

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

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

提前谢谢...

【问题讨论】:

  • 您好,我不知道是否可以通过编程方式删除通知,但您可以确定用新通知覆盖旧通知。如果它满足您的要求,那么我可以在这里为您发布代码。请回复
  • 不,实际上我有两个不同的通知,我已经取消了通知。

标签: android notifications android-notifications android-pendingintent


【解决方案1】:

也许试试这个:

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

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

notificationManager.cancelAll();

请参阅此文档链接:NotificationManager

【讨论】:

  • 我看不到if (Context.NOTIFICATION_SERVICE != null) 的意义,因为那是一个字符串常量,永远不会为空。
  • 你说得对,这个测试没用,我不知道我为什么这样做(删除它):/
  • @Matthieu 它工作了 4 年... :) 现在没有检查。如果它在任何情况下都不起作用,请告诉我。
  • @JayeshSojitra 我只是好奇cancel(NOTIFICATION_ID)cancelAll() 中的哪一个成功了。如果你的记忆可以追溯到那么远,这没什么大不了的:)
  • @Matthieu 在我的情况下两者都有效。如果要取消特定通知,请使用 cancel(NOTIFICATION_ID),如果要取消所有通知,请使用 cancelAll()。
【解决方案2】:

我认为最最近更新的做法(Kotlin 和 Java)应该这样做:

NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID)

或者取消所有通知是:

NotificationManagerCompat.from(context).cancelAll()

专为 AndroidX 或支持库设计。

【讨论】:

    【解决方案3】:

    在发生以下任一情况之前,通知一直可见:

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

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

    【讨论】:

      【解决方案4】:

      所有通知(甚至是其他应用程序通知)都可以通过收听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"));
      

      【讨论】:

      • 我不知道你为什么在已经接受stackoverflow.com/a/19268653/1404774时发布了这个答案。
      • 您是否有任何特定情况下上述问题不起作用?
      • 以上不适用于所有通知(所有“应用”通知)。这只会删除当前应用创建的通知。
      • 如何触发?发送哪个广播?能否请您也发布如何触发它?
      • 按照上面的代码,它会是; getContext().sendBroadcast(new Intent("com.test.app");
      【解决方案5】:

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

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

      你可以这样关闭它:

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

      【讨论】:

        猜你喜欢
        • 2017-10-11
        • 2011-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        • 2015-04-24
        • 1970-01-01
        相关资源
        最近更新 更多