首页 文章

Android:从通知栏中删除通知

提问于
浏览
143

我创建了一个应用程序,并通过事件我设法在android通知栏中添加通知 . 现在我需要示例如何从事件通知栏中删除该通知?

11 回答

  • 1

    这很简单 . 您必须在NotificationManager上调用 cancelcancelAll . cancel方法的参数是应该取消的通知的ID .

    查看API:http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int)

  • 5

    请试试这个,

    public void removeNotification(Context context, int notificationId) {      
        NotificationManager nMgr = (NotificationManager) context.getApplicationContext()
                .getSystemService(Context.NOTIFICATION_SERVICE);
        nMgr.cancel(notificationId);
    }
    
  • 197

    您也可以在通知管理器上调用 cancelAll ,这样您甚至不必担心通知ID .

    NotificationManager notifManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notifManager.cancelAll();
    

    编辑:我被downvoted所以也许我应该指定这只会从您的应用程序中删除通知 .

  • 6

    this will help:

    NotificationManager mNotificationManager = (NotificationManager)
                getSystemService(NOTIFICATION_SERVICE);
    mNotificationManager.cannelAll();
    

    and if you create a notification by calling

    startForeground();
    

    inside a Service.you may have to call

    stopForeground(false);
    

    first,then cancel the notification.

  • 58

    NotificationManager.cancel(id) 是正确答案 . 您可以通过删除整个通知渠道删除 Android Oreo 及更高版本的通知 . 这应删除已删除 Channels 中的所有邮件 .

    以下是Android documentation中的示例:

    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // The id of the channel.
    String id = "my_channel_01";
    mNotificationManager.deleteNotificationChannel(id);
    
  • 10

    您可以尝试这个快速代码

    public static void cancelNotification(Context ctx, int notifyId) {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
        nMgr.cancel(notifyId);
    }
    
  • 155

    在Android API> = 23上,你可以做这样的事情来删除一组通知 .

    for (StatusBarNotification statusBarNotification : mNotificationManager.getActiveNotifications()) {
            if (KEY_MESSAGE_GROUP.equals(statusBarNotification.getGroupKey())) {
                mNotificationManager.cancel(statusBarNotification.getId());
            }
        }
    
  • 1

    只需拨打ID:

    public void delNoti(int id) {((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancel(id);}
    
  • 1

    只需像下面的代码一样设置setAutoCancel(True):

    Intent resultIntent = new Intent(GameLevelsActivity.this, NotificationReceiverActivityAdv.class);
    
    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    GameLevelsActivity.this,
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
                    );
    
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            getApplicationContext()).setSmallIcon(R.drawable.icon)
            .setContentTitle(adv_title)
            .setContentText(adv_desc)
            .setContentIntent(resultPendingIntent)
             //HERE IS WHAT YOY NEED:
            .setAutoCancel(true);
    
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(547, mBuilder.build());`
    
  • 1

    如果要生成 Notification from a Service ,它是在 foreground 中启动的

    startForeground(NOTIFICATION_ID, notificationBuilder.build());
    

    然后发出

    notificationManager.cancel(NOTIFICATION_ID);
    

    取消通知并且通知仍然显示在状态栏中 . 在这种特殊情况下,您将通过两种方式解决这些问题:

    1> Using stopForeground( false ) inside service:

    stopForeground( false );
    notificationManager.cancel(NOTIFICATION_ID);
    

    2> Destroy that service class with calling activity:

    Intent i = new Intent(context, Service.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    if(ServiceCallingActivity.activity != null) {
        ServiceCallingActivity.activity.finish();
    }
    context.stopService(i);
    

    第二种方式更喜欢在 music player notification 更多因为不仅通知删除但删除播放器... ...

  • 0

    使用NotificationManager取消通知 . 您只需提供通知ID即可

    mNotificationManager.cancel(YOUR_NOTIFICATION_ID);

    还请查看此链接See Developer Link

相关问题