FCM通知图标未被使用。

8
我正在使用 FCM 并且已经成功向我的应用发送推送通知。我希望使用自定义通知图标,但它总是显示白色图标。我正在运行 Lollipop
根据文档,它说:

icon 可选,字符串 指示通知图标。将值设置为 myicon 表示可绘制资源 myicon。

(我不确定它确切的含义)。但这是我所做的。
  1. 我从 这里 生成了图标。其中有只有白色文本和透明背景的图标。图标的示例截图如下:enter image description here

  2. 我添加到 res 文件夹中 enter image description here

  3. 我编辑了 AndroidManifest 并在 <Application 中添加了这个 android:icon="@drawable/ic_stat_set"

  4. 我尝试在设备上运行项目,新的应用程序图标 ic_stat_set 被用作应用程序启动器。

  5. 从控制台发送通知,我确实收到了通知,但不是我刚刚设置的图标。

  6. 我还尝试通过 API 发送。我确实收到了通知,但没有设置的图标。

    curl -X POST --header "Authorization: key=SERVERKEY" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"REGISTERATION-TOKEN-ID\",\"notification\":{\"body\":\"Yellow\" , \"icon\" : \"ic_stat_set\"} \"priority":\"10"}"

更新:我的可绘制图标文件在 https://drive.google.com/open?id=0B5Fi1l7EbQ_BOERUMzNuQy1OWXM

我需要在控制台的自定义数据文件中放置一些东西吗?我的图标?我缺少什么?

谢谢。


你需要为棒棒糖使用轮廓。 - SweetWisher ツ
4个回答

1

这里是res文件夹图标文件。你觉得怎么样? - cjmling
@cjmling,你需要两个图标,一个用于启动器,另一个用于通知。它们的大小不同。解决这个问题并阅读我发送给你的内容,它应该可以正常工作。对我来说,这些更改完全正常运行。 - Michael B.
谢谢,现在它能工作了。我其实读了这篇文章,但是没找到哪里错了。现在我只是从示例应用程序中复制了可绘制的图标/文件夹并测试了一下。效果很好 :)。也许图标尺寸或要求存在问题,我无法弄清楚它是什么。 - cjmling

0

你应该使用剪影图标。你需要创建你的图标(例如通知图标)的剪影图标。这样可以解决白色背景问题。我已经尝试过了,它对我有效。请尝试一下。


你还应该学习这个链接:-> https://blog.clevertap.com/fixing-notification-icon-for-android-lollipop-and-above/ - Akash Garg
我确实有与您提供的博客链接中提到的相同的透明图标。问题仍然存在。:S - cjmling
必须要创建通知图标(任何你想要的),需要创建剪影图标吗?你应该尝试一下。这对我有用过。 - Akash Garg
这是与图标相关的文件。它们是剪影吗? - cjmling

0

Lollipop 版本使用没有背景的 png 图片作为图标。

  NotificationCompat.Builder builder = new NotificationCompat.Builder(
            this).setSmallIcon(getNotificationIcon())
            .setContentTitle(title)
            .setSound(defaultSoundUri)
            .setDefaults(Notification.DEFAULT_ALL)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setContentText(message).setAutoCancel(true)
            .setContentIntent(contentIntent);
    notificationManager.notify(NOTIFICATION_ID++, builder.build());


private int getNotificationIcon() {
    boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
    return useWhiteIcon ? R.drawable.logo : R.drawable.ic_launcher;
}

我提供的图标截图清楚地表明它是一个没有背景的PNG格式,不是吗? - cjmling
他正在使用 FCM 和 Lollipop,没有数据字段。这段代码永远不会被执行。是系统会显示通知。 - Michael B.

0

默认情况下,Lolipop之后的Android API状态栏图标的色调颜色为白色,您需要为较新版本和旧版本设置图标。如果不使用hack,您可以尝试以下方法:

  int icon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.your_logo_for_Kitkat : R.mipmap.your_logo_for_Lolipop_and_uper_version;
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(icon)
                .setContentTitle(remoteMessage.getData().get("title"))
                .setContentText(remoteMessage.getData().get("shortDescription"))
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setColor(Color.RED)
                .setStyle(notiStyle)
                .setContentIntent(pendingIntent);

他正在使用 FCM 和 Lollipop,没有数据字段。这段代码永远不会被执行。是系统将显示通知。 - Michael B.
还有设置通知图标或标志的系统!! - Jamil Hasnine Tamim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接