Android代码删除通知,Android:从通知中删除通知b

Android:从通知中删除通知b

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

11个解决方案

197 votes

你可以尝试这个快速代码

public static void cancelNotification(Context ctx, int notifyId) {

String ns = Context.NOTIFICATION_SERVICE;

NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);

nMgr.cancel(notifyId);

}

Ankit Patial answered 2019-05-23T02:26:35Z

155 votes

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

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

RoflcoptrException answered 2019-05-23T02:26:10Z

58 votes

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

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

notifManager.cancelAll();

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

Stephane Mathis answered 2019-05-23T02:27:06Z

10 votes

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

farhad.kargaran answered 2019-05-23T02:27:30Z

6 votes

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

startForeground(NOTIFICATION_ID, notificationBuilder.build());

然后发行

notificationManager.cancel(NOTIFICATION_ID);

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

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

stopForeground( false );

notificationManager.cancel(NOTIFICATION_ID);

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

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

Dhruv Raval answered 2019-05-23T02:28:30Z

5 votes

这将有助于:

NotificationManager mNotificationManager = (NotificationManager)

getSystemService(NOTIFICATION_SERVICE);

mNotificationManager.cannelAll();

如果您通过调用创建通知

startForeground();

在Service.you可能需要打电话

stopForeground(false);

首先,然后取消通知。

pilotmario answered 2019-05-23T02:29:15Z

1 votes

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

mNotificationManager.cancel(YOUR_NOTIFICATION_ID);

还请查看此链接见Developer Link

Muhammad Usman Ghani answered 2019-05-23T02:29:54Z

1 votes

在Android API> = 23上,您可以执行以下操作来删除一组通知。

for (StatusBarNotification statusBarNotification : mNotificationManager.getActiveNotifications()) {

if (KEY_MESSAGE_GROUP.equals(statusBarNotification.getGroupKey())) {

mNotificationManager.cancel(statusBarNotification.getId());

}

}

Szabolcs Becze answered 2019-05-23T02:30:18Z

1 votes

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

以下是Android文档中的示例:

NotificationManager mNotificationManager =

(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// The id of the channel.

String id = "my_channel_01";

mNotificationManager.deleteNotificationChannel(id);

gil.fernandes answered 2019-05-23T02:30:49Z

1 votes

请试试这个,

public void removeNotification(Context context, int notificationId) {

NotificationManager nMgr = (NotificationManager) context.getApplicationContext()

.getSystemService(Context.NOTIFICATION_SERVICE);

nMgr.cancel(notificationId);

}

Akansha patel answered 2019-05-23T02:31:09Z

0 votes

只需拨打ID:

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

phnghue answered 2019-05-23T02:31:35Z

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值