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

Har*_*han 9 android flutter firebase-cloud-messaging

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

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

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

这是我的 AndroidManifest:

<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>
Run Code Online (Sandbox Code Playgroud)

推送通知代码:

@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);
  }
Run Code Online (Sandbox Code Playgroud)

这里 onMessage 是唯一能在 Android 中完美运行的东西。我想在后台运行时实现相同的目标。

小智 13

对于那些找不到“ string.xml ”的人,您可以在以下位置找到它:android>app>src>main>res>values。它与style.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>
Run Code Online (Sandbox Code Playgroud)


Ana*_*afi 10

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

<meta-data
   android:name="com.google.firebase.messaging.default_notification_channel_id"
   android:value="@string/default_notification_channel_id" />
Run Code Online (Sandbox Code Playgroud)

注意:如果您在代码中设置此元,则该代码<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>
Run Code Online (Sandbox Code Playgroud)

这将解决问题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)
    }
}
Run Code Online (Sandbox Code Playgroud)

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

override fun onCreate() {
    super.onCreate()
    createChannel()
    .........
}
Run Code Online (Sandbox Code Playgroud)


Jua*_*cki 8

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

{ 
    "notification": {...},
    "click_action": "FLUTTER_NOTIFICATION_CLICK"
} 
Run Code Online (Sandbox Code Playgroud)

对于我的 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",
        },
    },
}
Run Code Online (Sandbox Code Playgroud)


fla*_*rup 7

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

<meta-data
    android:name="com.google.firebase.messaging.default_notification_channel_id"
    android:value="@string/default_notification_channel_id"/>
Run Code Online (Sandbox Code Playgroud)

而在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"/>
Run Code Online (Sandbox Code Playgroud)