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

15
我使用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. 这是我的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>

推送通知代码:

@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中,只有onMessage正常工作。当它在后台运行时,我希望能实现相同的效果。

6个回答

18

如果找不到 "string.xml" 文件,可以在android>app>src>main>res>values目录下找到。它与styles.xml不同。如果还没有这个文件,您可以创建一个:

  1. 右键单击 "values" 文件夹,
  2. 点击 New/Values Resource File
  3. 复制并粘贴以下文本:

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


13

Maksim在这里提供了一个相当不错的答案,包括官方文档链接。你需要在Manifest中添加以下元数据标签:

<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标签。

            <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"/>

但是先生,我在元数据中指定了一些内容: android:name="io.flutter.app.android.SplashScreenUntilFirstFrame" android:value="true" />``` 如果我将其添加到元数据中,它会抛出一个错误,对吧? - Harsha Vardhan
  • 出了什么问题: 任务 ':app:processDebugResources' 执行失败。
    Android 资源链接失败 输出:/Users/xxx/Workspace/xxx/build/app/intermediates/merged_manifests/debug/processDebugManifest/merged/AndroidManifest.xml:53: 错误:找不到资源字符串/default_notification_channel_id (即 com.iotrl.cargill_fc:string/default_notification_channel_id)。 错误:处理清单失败。
这是我得到的结果,我在 Flutter 项目中找不到 String.xml。
- Harsha Vardhan
@HarshaVardhan 你应该在某个地方定义了 default_notification_channel_id 字符串的 ID.. :O - zeroDivider

9

1- 首先,在 AndroidManifest.xml 文件中添加以下元代码,位于路径 <flutter 项目路径>/android/app/src/main/AndroidManifest.xml</activity> 标签后面。

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

注意: 如果你把这个meta标签设置在中,代码将不会生效。
2- 修改文件(如果不存在则新建)在此路径下:/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>

这将解决问题:在AndroidManifest中缺少默认通知通道元数据。将使用默认值。

但是之后,您需要在Android中创建此频道。为此,请转到文件<flutter项目路径>//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()
    .........
}

8

需要添加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",
        },
    },
}

2

如果你的Flutter版本大于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/ 这个网站提供了有关如何在Flutter中使用Firebase云消息传递的详细信息。Firebase云消息传递是一种跨平台的解决方案,可让您向移动和Web应用程序发送通知和消息。此外,该网站还提供了有关如何配置和测试您的应用程序以及如何处理各种情况(例如静默通知)的信息。

1
将“click_action”: “FLUTTER_NOTIFICATION_CLICK”添加到我的通知数据中,这对我解决了问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接