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

sigwle7e  于 2023-04-18  发布在  Android
关注(0)|答案(6)|浏览(168)

我使用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中完美工作的东西。我想在后台运行时实现同样的效果。

esbemjvw

esbemjvw1#

对于那些无法找到“string.xml”的人,您可以在以下位置找到它:android〉app〉src〉main〉res〉values,和 styles.xml 不一样,如果你还没有,可以创建一个:
1.右键单击“values”文件夹,
1.单击 * 新建 / 值资源文件 *
1.复制并粘贴以下文本:

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

57hvy0tb2#

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的属性。

EDIT您的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"/>
mzsu5hc0

mzsu5hc03#

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

bt1cpqcv4#

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

<meta-data
   android:name="com.google.firebase.messaging.default_notification_channel_id"
   android:value="@string/default_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()
    .........
}
jaql4c8m

jaql4c8m5#

如果你的flutter版本大于1.12,你不需要创建任何像Application.javaApplication.kt这样的文件,只需将下面的 meta值添加到你的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/

pkmbmrz7

pkmbmrz76#

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

相关问题