【问题标题】:W/FirebaseMessaging: Missing Default Notification Channel metadata in AndroidManifest. Default value will be usedW/FirebaseMessaging:AndroidManifest 中缺少默认通知通道元数据。将使用默认值
【发布时间】:2018-05-07 07:48:13
【问题描述】:

我们的应用现在有 targetSdkVersion 26 (Android 8) 并且应用使用 FCM 推送通知。当应用程序处于前台状态时,推送通知运行良好。当应用程序未处于前台状态时,单击通知时会出现此问题。我刚刚在 ma​​nifest 中添加了 元数据,但仍然出现同样的错误。

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


【解决方案1】:

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

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

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

更新: 添加了缺少的报价

【讨论】:

  • default_notification_channel_id 已设置?还是应该设置在哪里?
  • @shinriyo default_notification_channel_id 在 res/values/strings.xml 上设置。所以你可以编辑和设置你的channel_id
【解决方案2】:

我和你有同样的问题。 您的元名称不正确。

代替

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

你应该使用

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

唯一不同的是“_id”

【讨论】:

    【解决方案3】:

    新人的答案和提示

    元数据标签:

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      • 请在您的答案中添加一些解释,以便其他人可以从中学习
      猜你喜欢
      • 2020-10-24
      • 2019-10-19
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多