gpt4 book ai didi

android - 通知 setAutoCancel(true) 不起作用

转载 作者:IT老高 更新时间:2023-10-28 22:15:49 27 4
gpt4 key购买 nike

我正在尝试显示当用户点击它时删除的通知。我正在使用 NotificationCompat 类来构建通知,并在构建器上调用 setAutoCancel(true)。这是一段代码:

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

通知已正确添加,但当我点击它时没有任何反应!我做错了什么?

最佳答案

使用 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()”替换为其他内容。

我上传了 demo到github。

关于android - 通知 setAutoCancel(true) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15033316/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com