通知setAutoCancel(真)不工作 [英] Notification setAutoCancel(true) doesn't work
问题描述
我要表明上有被删除时,用户水龙头的通知。
我使用 NotificationCompat
类建立我的通知,我称之为 setAutoCancel(真)
我的制造商
。这就是那条code的:
I'm trying to show a notification that has to be deleted when user tap on it.
I'm using NotificationCompat
class to build my notification and I call setAutoCancel(true)
on my builder
. This is the piece of code:
NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("title")
.setAutoCancel(true)
.setContentText("content");
NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
通知正确添加,但是当我在敲打什么也没有发生!我哪里错了?
The notification is correctly added but when I tap on it nothing happens! Where I'm wrong?
推荐答案
使用setContentIntent应该解决您的问题:
Using setContentIntent should solve your problem:
.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());
通常情况下,你可能希望将用户引导到相关的内容,因此可能会取代新意图()'别的东西。
Often you might want to direct the user to the relevant content and so might replace 'new Intent()' with something else.
我上传了演示到GitHub上。
I uploaded a demo to github.
这篇关于通知setAutoCancel(真)不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!