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

时间:2017-11-23 10:59:41

标签: push-notification firebase-cloud-messaging android-notifications android-studio-3.0 android-8.0-oreo

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

的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());
}

3 个答案:

答案 0 :(得分:0)

我知道这已经晚了但我希望这对你或至少其他人有所帮助。 它已在here之前得到解答。

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

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

<强>更新 添加了缺失的引用

答案 1 :(得分:0)

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

代替

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

您应该使用

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

唯一的区别是“ _ id”

答案 2 :(得分:0)

新人的答案和提示

元数据标签:

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