Android:从通知栏中删除通知

Nez*_*zir 156 notifications android

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

Ank*_*ial 213

你可以尝试这个快速代码

public static void cancelNotification(Context ctx, int notifyId) {
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
    nMgr.cancel(notifyId);
}
Run Code Online (Sandbox Code Playgroud)

  • 这应该是正确的答案,因为它简单,正确并直接达到目的. (18认同)

Rof*_*ion 169

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

请参阅API:http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int)

  • private static final int MY_NOTIFICATION_ID = 1234; String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager; mNotificationManager =(NotificationManager)getSystemService(ns); mNotificationManager.notify(MY_NOTIFICATION_ID,通知); 示例代码不完整.这取决于您创建通知的方式.只需确保使用相同的ID取消您在创建通知时使用的通知.取消:mNotificationManager.cancel(MY_NOTIFICATION_ID); (3认同)

Ste*_*his 60

您也可以呼叫cancelAll通知管理器,这样您甚至不必担心通知ID.

NotificationManager notifManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.cancelAll();
Run Code Online (Sandbox Code Playgroud)

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


far*_*ran 11

只需像下面的代码一样设置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());`
Run Code Online (Sandbox Code Playgroud)

  • 仅当您直接点击通知时,此功能才有效.点击操作(例如:https://pl.vc/1gvrli)不会删除通知. (3认同)

小智 8

这将有助于:

NotificationManager mNotificationManager = (NotificationManager)
            getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();
Run Code Online (Sandbox Code Playgroud)

这应该删除应用程序发出的所有通知

如果您通过调用创建通知

startForeground();
Run Code Online (Sandbox Code Playgroud)

在Service.you可能需要打电话

stopForeground(false);
Run Code Online (Sandbox Code Playgroud)

首先,然后取消通知.


Dhr*_*val 7

如果要从使用前台启动的服务生成通知

startForeground(NOTIFICATION_ID, notificationBuilder.build());
Run Code Online (Sandbox Code Playgroud)

然后发行

notificationManager.cancel(NOTIFICATION_ID);
Run Code Online (Sandbox Code Playgroud)

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

1>在服务中使用stopForeground(false):

stopForeground( false );
notificationManager.cancel(NOTIFICATION_ID);
Run Code Online (Sandbox Code Playgroud)

2>使用调用活动销毁该服务类:

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);
Run Code Online (Sandbox Code Playgroud)

第二种方式更喜欢音乐播放器通知更多因为不仅通知删除但删除播放器... ...