【问题标题】:android 7.0 Notification icon appearing white squareandroid 7.0 通知图标出现白色方块
【发布时间】:2017-08-22 20:33:46
【问题描述】:

我正在使用下面的 sn-p 在我的 android 应用中生成通知。

private void sendNotification(String contentText, String message) {

    Intent resultIntent = new Intent(this, MainActivity.class);
    resultIntent.putExtra("clear","clear");
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
     Intent.FLAG_ACTIVITY_CLEAR_TASK);

   PendingIntent piResult = PendingIntent.getActivity(this, 0, resultIntent,0);

    NotificationCompat.Builder builder=new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.icon)
            .setColor(ContextCompat.getColor(getApplicationContext(),R.color.red))
            .setContentTitle("title")
            .setContentText(message)
            .setAutoCancel(true)
            .setLargeIcon(BitmapFactory.decodeResource(getResources() 
            ,R.drawable.notification))
            .setContentIntent(piResult);

    NotificationCompat.InboxStyle notification = new NotificationCompat.InboxStyle(builder);

    int i;
    for(i=0; i<messageList.size();i++){
        notification.addLine(messageList.get(i));
    }
    notification.setBigContentTitle("title");
    notification.setSummaryText(contentText);

    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID,notification.build());
}

它适用于 android 5 和 6,但对于 android nougat 它不起作用

【问题讨论】:

    标签: android android-notifications android-7.0-nougat


    【解决方案1】:

    从 android 版本的棒棒糖开始,他们对通知进行了更改。当您指定小图标时,它应该具有this link 中提到的特定大小。

    重要的是图片应该是透明并且只包含白色颜色。

    您可以查看this question获取答案

    【讨论】:

    • 图片只是透明的
    • @SureshKumar 您的图像可能具有透明背景,但图标是正方形,因此您会得到一个正方形的白色图像。除了透明部分之外,您的整个图像将被转换为白色。
    • @AlbertoMéndez 谢谢,我会检查一下
    【解决方案2】:

    据此博客here

    上面写着

    您会注意到新通知中没有图标;相反,在通知栏的受限空间中为标签本身提供了更多空间。但是,通知操作图标仍然是必需的,并且会继续在旧版 Android 和 Android Wear 等设备上使用。

    如果您一直在使用 NotificationCompat.Builder 和可用的标准样式构建通知,则默认情况下您将获得新的外观,无需更改代码。

    【讨论】:

    • 是的,但奇怪的是通知没有弹出图标:/
    • 其实是他们重新设计了通知,可以直接回复等等。
    【解决方案3】:

    遵循 Android API 中的docs

    状态栏图标仅由透明背景上的白色像素组成,并在适当的情况下使用 alpha 混合来平滑边缘和内部纹理。

    您的整个图像,但透明部分将被转换为白色(原本是白色或有颜色)。

    一种解决方案是创建一个带有颜色的剪影图标,这样您就可以在所有 Android API 中使用相同的图像。一个例子可能是这个图标:

    在较低版本的 Android 中,您会看到黑色的脸,在最新版本中,您会看到相同的脸,但颜色为白色。那是因为图像的透明部分(似乎SO删除了透明度,您可以从here获取原件)。

    【讨论】: