Android11版本中,当通知被取消或点击时,Notification.DeleteIntent收到了回调。当被点击时,不应该调用它。
创始人
2024-08-20 10:00:12
0

Android 11中的通知行为发生了变化。当通知被点击时,DeleteIntent不应该被触发。如果你想在通知被点击时执行某些操作,则可以使用PendingIntent。下面是示例代码:

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
        .setContentTitle(title)
        .setContentText(body)
        .setSmallIcon(R.drawable.notification_icon);

// Create the intent that should be sent when the notification is clicked
Intent resultIntent = new Intent(context, MyActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntentWithParentStack(resultIntent);
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

// Set the intent that should be sent when the notification is dismissed
builder.setDeleteIntent(resultPendingIntent);

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(notificationId, builder.build());

在上述代码中,我们使用TaskStackBuilder来创建一个包含我们想要启动的Activity的Intent,并将其添加到堆栈中。我们还使用PendingIntent.FLAG_UPDATE_CURRENT标志来确保PendingIntent中的数据是最新的。最后,我们使用setDeleteIntent()方法将PendingIntent设置为在通知被消除时发送。这样,当用户点击通知时,应用程序会启动相应的Activity,而不是调用DeleteIntent。

相关内容

热门资讯

第三分钟下载!微乐小程序免费黑... 第三分钟下载!微乐小程序免费黑科技,微乐游戏小程序脚本(开挂)真是是真的挂(2026)1、下载好微乐...
透视肯定!微乐小程序黑科技,微... 透视肯定!微乐小程序黑科技,微乐家乡自建房辅助app(透视)确实真的是有挂(哔哩哔哩)1、打开软件启...
3分钟插件!微乐小程序黑科技,... 3分钟插件!微乐小程序黑科技,微乐斗地主有挂吗(开挂)竟然存在有挂(2026)1、微乐小程序黑科技辅...
透视安卓版!微乐小程序黑科技,... 透视安卓版!微乐小程序黑科技,微乐江西小程序辅助(透视)确实真的有挂(哔哩哔哩)1、点击下载安装,微...
五分钟下载!微乐小程序黑科技,... 五分钟下载!微乐小程序黑科技,微乐广西小程序插件(开挂)确实真的是有挂(2026)1、点击下载安装,...
黑科技教程!微乐小程序免费黑科... 黑科技教程!微乐小程序免费黑科技,微信小程序微乐辅助器免费下载(透视)其实真的有挂(哔哩哔哩)1、超...
第1分钟辅助!微乐小程序黑科技... 第1分钟辅助!微乐小程序黑科技,微乐自建房辅助软件(开挂)竟然是真的挂(2026)1、第1分钟辅助!...
透视插件!微乐小程序黑科技,微... 透视插件!微乐小程序黑科技,微乐贵阳足鸡麻将开挂(透视)竟然是真的挂(哔哩哔哩)1、实时微乐贵阳足鸡...
3分钟插件!微乐小程序黑科技,... 3分钟插件!微乐小程序黑科技,微信小程序微乐辅助免费版(开挂)原来真的是有挂(2026)1、金币登录...
透视黑科技!微乐小程序免费黑科... 透视黑科技!微乐小程序免费黑科技,手机微乐小程序破解器如何使用(透视)其实存在有挂(哔哩哔哩);在进...