W/FirebaseMessaging:AndroidManifest 中缺少默认通知通道元数据。将使用默认值

问题描述 投票:0回答:5

我们的应用程序现在有 targetSdkVersion 26 (Android 8) 并且该应用程序使用 FCM 推送通知。当应用程序处于前台状态时,推送通知运行良好。当应用程序不处于前台状态时单击通知时会出现此问题。我刚刚在 manifest 中添加了 metadata 但仍然遇到相同的错误。

AndroidManifest.xml

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

MyFirebaseMessagingService.java

/**
 * Create and show a simple notification containing the received FCM message.
 */
private void sendNotification(NotificationModel notificationModel) 
{
    Intent intent;
    if (notificationModel.getAppLink() != null) 
    {
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(notificationModel.getAppLink()));
    } else 
    {
        intent = new Intent(this, NoticeActivity.class);
    }

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);

    if (notificationModel.getAppLink() != null) 
    {
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
    } else 
    {
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
    }

    notificationBuilder.setContentTitle(notificationModel.getTitle())
            .setContentText(notificationModel.getMessage())
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
    {
        int importance = NotificationManager.IMPORTANCE_LOW;
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        assert notificationManager != null;
        notificationBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
        notificationManager.createNotificationChannel(notificationChannel);
    }

    assert notificationManager != null;
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
push-notification firebase-cloud-messaging android-notifications android-studio-3.0 android-8.0-oreo
5个回答
35
投票

我知道这已经晚了,但我希望这对您或至少对其他人有帮助。 之前已经回答过这里

基本上,您的元数据名称错误。
而不是

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

应该是

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

更新: 添加了缺失的报价


6
投票

更新的答案和提示

元数据标签:

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

正在AndroidManifest.xml上的application标签内部,例如:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.domain.appname">

  <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@mipmap/ic_launcher" />

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

有一个名为“app/src/main/res/values/strings.xml”的文件,您需要在资源标签内添加此行:

<string name="default_notification_channel_id">com.domain.appname.urgent</string>

值“com.domain.appname.urgent”需要与您默认创建的频道相同。

strings.xml 看起来像:

<?xml version='1.0' encoding='utf-8'?>
<resources>
    <string name="app_name">MyApp</string>
    <string name="title_activity_main">MyApp</string>
    <string name="package_name">com.domain.appname</string>
    <string name="custom_url_scheme">com.domain.appname</string>
    <string name="default_notification_channel_id">com.domain.appname.urgent</string>
</resources>


3
投票

啊!,您可能忘记在清单上添加频道 id,因此请检查您的清单并为频道 id 添加元数据

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

2
投票

我也遇到了和你一样的问题。 您的元名称不正确。

而不是

android:name="com.google.firebase.messaging.default_notification_channel"

你应该使用

android:name="com.google.firebase.messaging.default_notification_channel_id"

唯一不同的是“_id”


0
投票

我在 firebase_messaging 中遇到同样的问题:^14.7.14

AndroidManifest.xml 添加以下内容对我有用。

<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="high_importance_channel" />
© www.soinside.com 2019 - 2024. All rights reserved.