Android通知意图自我清除

29

我阅读了许多关于如何创建通知消息的示例。 我的目标是当小部件触发通知时,希望用户在单击通知意图后可以清除它自己。因为我没有要返回的活动,所以此处的通知只是纯粹的提醒,不需要做其他事情。 那么,有没有一种只能清除/取消自身的意图代码呢? 下面的代码是由按钮启动的活动(按钮代码未包含)。通知将由后台服务触发。

CharSequence title = "Hello";
CharSequence message = "Hello, Android!";
final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final Notification notification = new Notification(R.drawable.icon,"A New Message!",System.currentTimeMillis());

notification.defaults=Notification.FLAG_ONLY_ALERT_ONCE+Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, AndroidNotifications.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);

notification.setLatestEventInfo(AndroidNotifications.this, title,message, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);

感谢

6个回答

74

查看FLAG_AUTO_CANCEL

该位应位按位或进标志字段中,如果通知被用户点击后应该被取消的话则需要设置。

编辑:

notification.flags |= Notification.FLAG_AUTO_CANCEL;

我的代码已经使用了FLAG_AUTO_CANCEL,但实际上通知在点击后并没有消失。 - John
17
你的代码使用了错误的(放置在defaults字段中),你需要按位或到flags字段中,像这样:notification.flags |= Notification.FLAG_AUTO_CANCEL; - Pentium10
2
感谢通知.flags |= Notification.FLAG_AUTO_CANCEL; 已经生效。 - John
这里似乎标志位不起作用。进一步设置此标志会导致持续播放通知声音。 - Murtuza Kabul

19

不要使用notification.defaults设置通知的标志,而是使用notification.flags。

例如:

notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;

10

如果您正在使用android.support.v4的一部分NotificationCompat.Builder,那么只需调用其对象的方法setAutoCancel

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true);

一些人报告说setAutoCancel() 对他们不起作用,因此您也可以尝试这种方法

builder.build().flags |= Notification.FLAG_AUTO_CANCEL;

似乎使用操作(通过addAction())不会自动取消通知。 - Someone Somewhere

1
使用setContentIntent应该可以解决您的问题:
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));

例如:

NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("title")
        .setAutoCancel(true)
        .setContentText("content")
        .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());

通常你可能希望将用户直接引导到相关内容,因此可能会用其他方式替换“new Intent()”。

1
 /** 
      Post a notification to be shown in the status bar. 
      Obs.: You must save this values somewhere or even pass it as an extra through Intent to use it later
 */
 notificationManager.notify(NOTIFICATION_ID, notification);

 /** 
      Cancel a previously shown notification given the notification id you've saved before
 */
 notificationmanager.cancel(NOTIFICATION_ID);

notify() 后立即取消通知会导致通知根本不显示。 - Aryo

1
我唯一能想到的方法是让你的NotificationIntent指向一个后台Service。当这个服务被启动时,它将使用NotificationManager.cancel(int id)清除给定的Notification。然后,Service将停止自己。这不太美观,也不容易实现,但我找不到其他方法来做到这一点。

如果您想通过代码取消通知,请在取消调用中使用“NOTIFICATION_ID”。 - Pentium10
是的,我会从一个按钮中调用它,但当用户选择通知时,我希望从代码中调用它。 - John
可行但有点混乱的解决方案(需要编写附加服务)。不过,在服务中我可以调用 Intent,并将通知 ID 作为参数传递。 (注:该翻译仅供参考,如需准确翻译建议寻求专业人士帮助。) - Danubian Sailor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接