Flutter 中 AndroidManifest 中缺少默认的通知通道元数据



我正在使用firebase_messaging: ^5.0.1包来实现推送通知,在IOS中一切正常,而当我的移动应用程序运行后台时,我收到了通知,但它没有导航到相应的屏幕,它只是打开默认屏幕。如何实现导航到该特定屏幕。

PS:我实现了click_action功能,这就是它在iOS中运行良好的原因,但是Android它显示以下消息

W/FirebaseMessaging( 8260): Missing Default Notification Channel metadata in AndroidManifest. Default value will be used.

这是我的Android清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.check">
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="Cargill FC"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:allowBackup="false"
            android:fullBackupContent="false"
            android:windowSoftInputMode="adjustResize">
            <!-- This keeps the window background of the activity showing
                 until Flutter renders its first frame. It can be removed if
                 there is no splash screen (such as the default splash screen
                 defined in @style/LaunchTheme). -->
            <meta-data
                android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
                android:value="true" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

推送通知代码:

@override
  void initState() {
    super.initState();
    tabController = new TabController(length: 2, vsync: this);
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        onFirebaseMessage(message);
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
    );
    _firebaseMessaging.requestNotificationPermissions(const IosNotificationSettings(sound: true, badge: true, alert: true));
    _firebaseMessaging.onIosSettingsRegistered.listen((IosNotificationSettings settings) {
      print("Settings registered: $settings");
    });
    _firebaseMessaging.getToken().then(registerFirebaseTokenForUser);
  }

在这里,消息是唯一在Android中完美运行的东西。我想在后台运行时实现相同的效果。

对于那些无法找到">string.xml"的人,您可以在以下位置找到它:android>app>src>main>res>values。它与样式不同.xml。如果您还没有,可以创建一个:

  1. 右键单击">"文件夹,
  2. 单击">新建/值"资源文件
  3. 复制并粘贴以下文本:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="default_notification_channel_id" translatable="false">fcm_default_channel</string>
</resources>

Maksim在这里有一个非常可靠的答案,包括官方文档的链接。您需要在清单中添加以下元数据标记:

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

string.xml中,您可以通过以下方式声明default_notification_channel_id: <string name=“default_notification_channel_id”>Channel ID</string>

然后,您必须在发送推送通知时提供具有该特定 ID 的属性。

编辑您的AndroidManifest.xml中可以有多个元数据标签:

            <meta-data
                android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
                android:value="true" />
            <meta-data
                android:name="com.google.firebase.messaging.default_notification_channel_id"
                android:value="@string/default_notification_channel_id"/>

需要发送添加FLUTTER_NOTIFICATION_CLICK,才能执行 onResume 和 onLunch。

{ 
    "notification": {...},
    "click_action": "FLUTTER_NOTIFICATION_CLICK"
} 

对于我的 golang 服务器,这意味着添加AndroidConfig

message := &messaging.Message{
    Topic: topic,
    Notification: &messaging.Notification{/* */}
    Data: data,
    APNS: &messaging.APNSConfig{/* */}
    Android: &messaging.AndroidConfig{
        Notification: &messaging.AndroidNotification{
            ClickAction: "FLUTTER_NOTIFICATION_CLICK",
        },
    },
}

1-首先,在位于路径<flutter project path>/android/app/src/main/AndroidManifest.xmlAndroidManifest.xml标签之后</activity>添加此元代码

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

注意:如果您在<activity>中设置此元,则代码将不起作用。

2-修改此路径中的文件(如果不存在,则创建新文件(<flutter project path>/android/app/src/main/res/values/string.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="default_notification_channel_id" translatable="false">fcm_default_channel</string>
</resources>

这将解决问题Missing Default Notification Channel metadata in AndroidManifest. Default value will be used.

但是在那之后,您需要在Android中创建此通道,为此请转到文件<flutter project path>//android/app/src/main/kotlin/com/examble/project_name/Application.kt并添加此功能:

private fun createChannel(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Create the NotificationChannel
        val name = getString(R.string.default_notification_channel_id)
        val channel = NotificationChannel(name, "default", NotificationManager.IMPORTANCE_HIGH)
        val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }
}

然后从onCreate()函数调用它:

override fun onCreate() {
    super.onCreate()
    createChannel()
    .........
}

如果您的颤振版本大于 1.12则无需创建任何文件,例如 Application.javaApplication.kt只需将以下元值添加到您的文件中AndroidManifest

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

参考: https://firebase.flutter.dev/docs/messaging/overview/

在我的通知数据中添加"click_action":"FLUTTER_NOTIFICATION_CLICK"为我解决了这个问题

相关内容

  • 没有找到相关文章

最新更新