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

时间:2019-06-03 13:41:34

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

推送通知代码:

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

这里onMessage是在Android中完美运行的唯一内容。我想在后台运行时达到相同的效果。

5 个答案:

答案 0 :(得分:8)

对于那些找不到“ string.xml ”的用户,可以在以下位置找到它: android> app> src> main> res>值。它与 styles.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>

答案 1 :(得分:1)

在通知的数据中添加“ click_action”:“ FLUTTER_NOTIFICATION_CLICK”对我来说解决了

答案 2 :(得分:1)

添加要发送的.mat-form-field ::ng-deep .mat-optgroup { // styles } is required,以便执行onResume和onLunch。

FLUTTER_NOTIFICATION_CLICK

对于我的golang服务器,这意味着添加{ "notification": {...}, "click_action": "FLUTTER_NOTIFICATION_CLICK" }

AndroidConfig

答案 3 :(得分:0)

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

NetworkImage > FileImage > AssetImage > MemoryImage

<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id"/> 中,您可以通过以下方式声明default_notification_channel_id: string.xml

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

编辑 您的<string name=“default_notification_channel_id”>Channel ID</string>中可能有多个元数据标签:

AndroidManifest.xml

答案 4 :(得分:0)

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

注意:如果您在<activity>内设置此meta,则该代码将无效。

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()
    .........
}