【问题标题】:Android 5.1 push notification icon is blankAndroid 5.1 推送通知图标为空白
【发布时间】:2015-06-26 01:23:28
【问题描述】:

当使用 Parse 推送通知时,我们的应用程序总是显示应用程序的启动器图标。 在最新的 Android 5.1 版本中,该图标显示为空白(一个白色方块)。

我尝试在元数据中设置图标:

<meta-data android:name="com.parse.push.notification_icon" android:resource="@drawable/noti_icon"/>

基于问题here

但似乎没有任何效果。 有什么想法吗?

【问题讨论】:

  • 图标中的任何实心像素都会在 Lollipop 上涂上白色。使用带有“孔”的图标来表达对比。
  • 如果你的图标是白色的,它会被删除

标签: android parse-platform push-notification


【解决方案1】:

试试这个代码。

Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(largeIcon)
                .setContentText(data)
                .setContentTitle("Notification from Parse")
                .setContentIntent(pendingIntent);

【讨论】:

    【解决方案2】:

    虽然@Pelanes 有正确的答案(并且应该被接受),但这就是我所做的。请注意,Parse docs for getSmallIconId 声明如下:

    Retrieves the small icon to be used in a Notification. The default implementation uses the icon specified by com.parse.push.notification_icon meta-data in your AndroidManifest.xml with a fallback to the launcher icon for this package. To conform to Android style guides, it is highly recommended that developers specify an explicit push icon.

    因此,完全没有必要重写 getSmallIconId() 和 getLargeIcon() 方法。

    我为解决这个问题所做的只是复制了我的图标,在图标上打了透明的“洞”,并在我的清单中设置了 com.parse.push.notification_icon 元数据以指向这个新图标。

    对于 Android 5.0,您的通知图标必须是白色和透明的,正如其他人所提到的。所以创建单独的图标是必要的。只需要清单中的一行和一个新的可绘制文件。

    【讨论】:

    • 你确定我们不需要定义不同的操作系统版本应该如何处理通知图标吗?我执行了您的建议,似乎使用com.parse.push.notification_icon 设置的通知图标现在在 Android 4.4 和 5.1 中都使用了。
    • 我用 5.1 和 4.4.2 测试过。 5.1 使用新图标,4.4.2 使用普通应用程序图标......不知道为什么会发生这种情况,但不是你。也许为了安全起见,您需要区分。我将编辑我的答案。
    【解决方案3】:

    您必须在 Android Lollipop 5.0 或更高版本下使用透明的白色图标。您可以扩展 ParsePushBroadcastReceiver 类并覆盖这两个方法,以使您的通知图标与这些 Android API 兼容。

        @Override
    protected int getSmallIconId(Context context, Intent intent) {
        return R.drawable.your_notifiation_icon;
    }
    
    @Override
    protected Bitmap getLargeIcon(Context context, Intent intent) {
        return BitmapFactory.decodeResource(context.getResources(), R.drawable.your_notifiation_icon);
    }
    

    记得自定义您的代码以支持 Lollipop 和以前的 API。

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            return BitmapFactory.decodeResource(context.getResources(), R.drawable.your_notifiation_icon_lollipop);
        }
        else{
            return BitmapFactory.decodeResource(context.getResources(), R.drawable.your_notifiation_icon);
        }
    

    【讨论】:

    • 我实现了 getSmallIconId 并按照您的说明工作
    • 非常感谢您的回答@Pelanes。所以要扩展它,基本上你必须创建一个像 MyPushBroadcastReceiver 这样的类来扩展 ParsePushBroadcastReceiver 并覆盖 getSmallIconId(...) 和 getLargeIcon(...)。不要忘记在清单中指定您的接收器。在棒棒糖上,将使用小图标和大图标。在模拟器 2.3.7(我测试过)上只有很小的。
    【解决方案4】:

    它与 Parse 或 push nitification 无关,而与 Android 5.0 如何处理通知图标有关。 有关详细信息,请参阅此相关问题: Notification bar icon turns white in Android 5 Lollipop

    【讨论】:

    • 确实如此,但它只适用于通知栏图标 - 通知上的图标保持彩色。
    • 将 SDK 定位到 20 以上时,它不仅会在栏上显示白色图标,还会在通知上显示白色图标