【问题标题】:flutter_local_notifications not showing Foreground notifications in release mode in devices above Android 6在 Android 6 以上的设备中,flutter_local_notifications 未在发布模式下显示前台通知
【发布时间】:2022-10-20 17:38:18
【问题描述】:

我正在使用 Firebase Cloud Messaging(FCM) 向我的 Flutter 应用发送推送通知。我需要在 Android 的前台显示通知,所以尝试了 FlutterFire 文档中提到的插件 -flutter_local_notifications,但它似乎不适用于所有设备。我发现它只能在一台运行 Android 6 的设备上运行。

我在AndroidManifest.xml 中添加了这一行 -

  <meta-data
            android:name="com.google.firebase.messaging.high_importance_channel"
            android:value="high_importance_channel"/>

我如何在接收 FCM 消息时触发本地通知 -

 // local notif initialisation //
    var initializationSettingsAndroid = new AndroidInitializationSettings('@mipmap/ic_launcher');
    var initializationSettingsIOS = new IOSInitializationSettings();
    var initializationSettings = new InitializationSettings(android: initializationSettingsAndroid, iOS: initializationSettingsIOS);

    flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
    flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: onSelectNotification);

    /// Foreground
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      if (message.notification != null) {
        print('Foreground (onMessage): Title:${message.notification.title}, Subtitle:${message.notification.body}, Data:${message.data.toString()}');
        remoteMessage = message;
        var data = json.decode(message.data['metadata']);

        showNotification(
          1234,
          "${message.notification.title}",
          "${message.notification.body}",
          "$message",
        );
      }
    });

我也尝试过像这样创建自定义通知通道

  AndroidNotificationChannel channel = AndroidNotificationChannel(
    'high_importance_channel', // id
    'High Importance Notifications', // title
    description: 'This channel is used for important notifications.', // description
    importance: Importance.max,
  );
 await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(channel);

我不确定我是否遗漏了什么?更高的 Android API 级别是否有任何额外的步骤。插件的自述文件中有一个标题为Release build configuration 的注释,要求对 ProGuard 文件进行自定义,我也遵循了这些步骤,但对我的情况没有任何帮助。在这个问题上寻求一些帮助。

依赖版本:

  firebase_core: ^1.13.1 
  firebase_messaging: ^11.2.8
  flutter_local_notifications: ^9.2.0

Flutter SDK 版本:Flutter 2.10.2

【问题讨论】:

    标签: android flutter firebase-cloud-messaging flutter-local-notification foregroundnotification


    【解决方案1】:

    androidpp 目录中添加proguard-rules.pro。正如 flutter_location_notification 所暗示的那样

    ##---------------Begin: proguard configuration for Gson  ----------
    # Gson uses generic type information stored in a class file when working with fields. Proguard
    # removes such information by default, so configure it to keep all of it.
    -keepattributes Signature
    
    # For using GSON @Expose annotation
    -keepattributes *Annotation*
    
    # Gson specific classes
    -dontwarn sun.misc.**
    #-keep class com.google.gson.stream.** { *; }
    
    # Application classes that will be serialized/deserialized over Gson
    -keep class com.google.gson.examples.android.model.** { <fields>; }
    
    # Prevent proguard from stripping interface information from TypeAdapter, TypeAdapterFactory,
    # JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
    -keep class * extends com.google.gson.TypeAdapter
    -keep class * implements com.google.gson.TypeAdapterFactory
    -keep class * implements com.google.gson.JsonSerializer
    -keep class * implements com.google.gson.JsonDeserializer
    
    # Prevent R8 from leaving Data object members always null
    -keepclassmembers,allowobfuscation class * {
      @com.google.gson.annotations.SerializedName <fields>;
    }
    
    # Retain generic signatures of TypeToken and its subclasses with R8 version 3.0 and higher.
    -keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
    -keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken
    
    ##---------------End: proguard configuration for Gson  ----------
    

    您可以尝试在androidppuild.gradle > buildTypes > release 中添加以下行

    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    

    buildTypes 将如下所示:

    buildTypes {
            release {
                minifyEnabled true
                shrinkResources true
                proguardFiles getDefaultProguardFile(
                        'proguard-android-optimize.txt'),
                        'proguard-rules.pro'
                signingConfig signingConfigs.release
            }
    

    【讨论】:

    • 已经试过了,没有运气:(
    • 请在 buildType 版本中尝试使用 shrinkResources false minifyEnabled false
    • 试过了,还是不行。。。
    • 在我升级flutter和flutter_local_notifications之前,一切都很好。现在我面临同样的问题。在调试一切正常时,即使是后台消息。但是当我发布构建后台通知停止工作。我也尝试添加--no-shrink,但没有运气。
    【解决方案2】:

    各位有没有找到解决办法?我也面临同样的问题,我不知道要解决这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 2022-01-06
      • 2019-09-06
      • 2019-06-24
      • 2022-11-13
      • 1970-01-01
      相关资源
      最近更新 更多