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
    评论
极光通知是一种Android推送通知服务,它可以帮助应用程序在后台向用户发送通知消息。使用极光通知,应用程序可以轻松地向用户发送推送通知,提醒他们有新的消息、新的活动或新的优惠等。 具体使用方法如下: 1. 在项目中添加极光推送 SDK。 2. 在 AndroidManifest.xml 文件中注册 JPush SDK。 ``` <receiver android:name="cn.jpush.android.service.PushReceiver" android:enabled="true" > <intent-filter> <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" /> <action android:name="cn.jpush.android.intent.REGISTRATION" /> <action android:name="cn.jpush.android.intent.UNREGISTRATION" /> <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" /> <category android:name="com.example.jpushdemo" /> </intent-filter> </receiver> ``` 3. 初始化 JPush SDK。 ``` JPushInterface.init(this); ``` 4. 设置通知样式。 ``` // 设置通知样式 - 定义通知Layout private void setStyleCustom() { CustomPushNotificationBuilder builder = new CustomPushNotificationBuilder(MainActivity.this, R.layout.customer_notitfication_layout, R.id.icon, R.id.title, R.id.text); builder.layoutIconDrawable = R.drawable.jpush_notification_icon; builder.developerArg0 = "developerArg2"; JPushInterface.setPushNotificationBuilder(2, builder); } ``` 5. 发送通知。 ``` JPushInterface.sendNotification(MainActivity.this, R.layout.customer_notitfication_layout, R.id.icon, R.id.title, R.id.text, pushPayload); ``` 以上是使用极光通知的简单步骤,具体可参考极光官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值