Android API 31 级别导致应用程序中 Flutter 本地通知出现错误

问题描述 投票:0回答:1

根据Google的政策,我将targetSdkVersion和compileSdkVersion更新为31。但我注意到Flutter本地通知包现在对传入消息抛出错误,如下所示:

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.

我浏览了与此错误相关的所有堆栈溢出答案并进行了以下更改:

  1. 在 build.gradle 文件中添加了以下 Java 和 Kotlin 依赖项:build.gradle file
  2. 在活动标签下添加了此接收器: Receiver tag
  3. 在活动标签下添加导出标志: Exported flag
  4. 将 gradle-wrapper.properties 中的 gradle 版本更新为最新版本: Latest gradle version 我没有在代码中使用任何 PendingIntent,并在 API 31 级别的模拟器上模拟应用程序。有人可以通过消息通知帮我解决这个问题吗?
flutter android-pendingintent flutter-local-notification android-api-31
1个回答
0
投票

看起来

flutter_local_notifications
存储库中存在一个提供解决方案的问题。 在这里查看问题

如果您不想更新软件包的版本,您可以分叉,然后重置为包含当前版本代码的提交,并根据您正在使用的版本实施您自己的修复。

  1. 可以去FlutterLocalNotificationsPlugin.java文件
  2. 找到一个错误函数“scheduleNotification”
  3. PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationDetails.id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)
    固定为
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationDetails.id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT  | PendingIntent.FLAG_IMMUTABLE)
© www.soinside.com 2019 - 2024. All rights reserved.