单击时删除Android删除通知(由mNotifyBuilder创建)

use*_*947 10 notifications android

我发现很多答案,但没有帮助:(我有这个代码:

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.icon;
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    int notifyID = 96;
    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context)
        .setContentTitle("MyApp")
        .setContentText(message)
        .setDefaults(Notification.DEFAULT_ALL)
        .setAutoCancel(true)
        .setSmallIcon(icon);

    Notification notification = mNotifyBuilder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    mNotificationManager.notify(notifyID, notification);
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我点击通知没有任何事情发生,它仍然存在.在文档中,我需要使用:

.setAutoCancel(true)
Run Code Online (Sandbox Code Playgroud)

有人有类似的问题,有人告诉他使用:

notification.flags |= Notification.FLAG_AUTO_CANCEL;
Run Code Online (Sandbox Code Playgroud)

我使用两者,但没有结果:(非常感谢你的答案.:)

Aba*_*ano 19

我认为,如果你没有在你的通知中使用Intent,那么唯一的方法就是解雇它来刷它.

否则,您可以使用Intent打开您的活动,这将实际清除通知:

Intent showIntent = new Intent(this, YourActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, showIntent, 0);
Run Code Online (Sandbox Code Playgroud)

并将其添加到通知中:

NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context)
    .setContentTitle("MyApp")
    .setContentText(message)
    .setDefaults(Notification.DEFAULT_ALL)
    .setAutoCancel(true)
    .setContentIntent(contentIntent)
    .setSmallIcon(icon);
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

  • 我的作品!我只做了一点修改:.setContentIntent(PendingIntent.getActivity(HandymanApplication.getContext(),0,intent,PendingIntent.FLAG_CANCEL_CURRENT)) (5认同)
  • 评论实际上对我有所帮助 (2认同)

mr.*_*fox 9

用户可以解除所有通知,或者如果您将通知设置为自动取消,则在用户选择通知后也会将其删除.

您还可以在NotificationManager上调用cancel()以获取特定通知ID.cancelAll()方法调用将删除您先前发出的所有通知.

例:

mNotificationManager.cancel(notifyId);
Run Code Online (Sandbox Code Playgroud)