Android api升级到31 导致的兼容性问题

1.问题描述:

android api升级到31的时候会引发很多兼容性问题,今天主要记录其中一个问题,

Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. 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.

2.解决方法

这个是我们的app发布之后在firebase平台检测到的一个异常,我在网上查阅了诸多资料,大部分的解决方案是要把

"androidx.work:work-runtime-ktx:$work_version"  版本升级到 2.7.0 ,但是升级到这个版本之后并不能完全解决我的问题,因为我在看firebase报错信息的时候发现这个异常底层是由com.google.firebase.firebase-messaging抛出来的,报错信息如下:

所以我推测是因为firebase库没有升级到相应版本导致的不兼容,原来的firebase库相关版本如下:

api 'com.google.firebase:firebase-core:18.0.0'
api 'com.google.firebase:firebase-messaging:21.0.0'
api 'com.google.android.gms:play-services-analytics:17.0.0'
implementation 'com.google.firebase:firebase-analytics:18.0.0'
implementation 'com.google.firebase:firebase-analytics-ktx:18.0.0'
implementation 'com.google.firebase:firebase-crashlytics:17.4.1'
implementation 'com.google.android.gms:play-services-location:17.1.0'

因此我把这些相关库都升级到最新版本,要想知道这些库的最新版本号可以访问这个网站,https://mvnrepository.com/

通过搜索就可以知道每个库最新的版本。那么升级后的版本如下:

api 'com.google.firebase:firebase-core:20.1.2'
api 'com.google.firebase:firebase-messaging:23.0.2'
api 'com.google.android.gms:play-services-analytics:18.0.1'
implementation 'com.google.firebase:firebase-analytics:20.1.2'
implementation 'com.google.firebase:firebase-analytics-ktx:20.1.2'
implementation 'com.google.firebase:firebase-crashlytics:18.2.9'
implementation 'com.google.android.gms:play-services-location:19.0.1'

现在我已经相关的库都升级了,接下来就是验证。

3.验证是否成功

因为这个问题是由firebase-messageing库导致的,而且崩溃时机都是app在后台运行的时候,所以我猜测应该是firebase在给app发通知消息的时候导致崩溃,所以我在firebase平台测试发送一下消息到我的app,看是否可以复现这个问题。https://firebase.google.com/docs/cloud-messaging/android/first-message?hl=zh&authuser=0

在这个官网上查看了一下向后台应用发送测试消息的方法,

FirebaseMessaging.getInstance().getToken()
    .addOnCompleteListener(new OnCompleteListener<String>() {
        @Override
        public void onComplete(@NonNull Task<String> task) {
          if (!task.isSuccessful()) {
            Log.w(TAG, "Fetching FCM registration token failed", task.getException());
            return;
          }

          // Get new FCM registration token
          String token = task.getResult();

          // Log and toast
          String msg = getString(R.string.msg_token_fmt, token);
          Log.d(TAG, msg);
          Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    });

然后通过这个方法获取token,结果一直提示获取token失败,排查得知是因为没有安装google service导致firebase无法启动,因为我手机使用的是小米手机,无法安装google service,所以我开启一个android12的模拟器,通过科学上网,安装google service,登录google play,终于获取到firebase token,发送测试消息如图:

发送成功之后,在模拟器上果然发现app停止运行了,看本地异常日志 正式之前监测到的异常,然后使用第二部的升级方案,再次运行app,发送测试消息,发现可以通过通知消息正常唤起后台app,问题至此修复完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值