Android12(SDK31)通知栏通知报错问题

之前Android11时候加了个通知栏常驻通知,今天升级Android12突然就凉了,项目框框报错

java.lang.IllegalArgumentException: com.ibptadb: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
2022-07-13 10:48:01.939 31160-31160/com.xxx I/info: Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.

报错写的清楚明了,就是SDK31版本的问题,然后网上一搜索是在Android12中待处理的PendingIntent必须声明可变性。

要声明给定PendingIntent对象是可变的或不可变的 ,请分别使用 PendingIntent.FLAG_MUTABLE 或 PendingIntent.FLAG_IMMUTABLE标志。

代码就是:

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this, Main.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
Notification notify = new Notification.Builder(this).setSmallIcon(R.drawable.ic_launcher)
        // 设置通知常驻,不会通过左右滑动消除,只能通过cancel消除
        .setOngoing(true)
        // 设置通知将要启动程序的Intent
        .setContentIntent(pi)

        // 设置打开该通知,该通知不会自动消失
        .setAutoCancel(false).build();
// 发送通知
nm.notify(NOTIFICATION_ID, notify);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值