flutter_local_notifications 在 Android 6 以上设备中发布模式下不显示前台通知

发布于 2025-01-12 08:31:01 字数 2487 浏览 3 评论 0原文

我正在使用 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 级别是否有任何额外的步骤。插件的自述文件中有一条标题为“发布构建配置” 的注释,要求进行 ProGuard 文件自定义,我也遵循了这些步骤,但对我的情况没有任何帮助。正在寻找有关此问题的一些帮助。

依赖版本:

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

Flutter SDK 版本:Flutter 2.10.2

I'm using Firebase Cloud Messaging(FCM) to send push notifications to my Flutter app. I need to show notifications in Foreground in Android, so tried the plugin -flutter_local_notifications as mentioned in the FlutterFire documentation, but it doesn't seem to work on all devices. I found it working only on one device running on Android 6.

I added this line in AndroidManifest.xml -

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

How I'm triggering local notifications on receiving FCM message -

 // 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",
        );
      }
    });

I have also tried creating custom notification channel like this

  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);

I'm not sure if I'm missing something? Are there any additional steps for higher Android API levels. There was a note on the plugin's Readme titled Release build configuration calling for ProGuard file customisation, I followed those steps too, but nothing helped in my case. Looking for some help on this issue.

Dependency versions:

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

Flutter SDK Version : Flutter 2.10.2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你没皮卡萌 2025-01-19 08:31:01

android\app\目录中添加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  ----------

您可以尝试在 android\app\build.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
        }

Add the proguard-rules.pro in android\app\ directory. As flutter_location_notification suggests which is

##---------------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  ----------

You can try adding the following line in android\app\build.gradle > buildTypes > release

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

The buildTypes will look like following:

buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文