首页 文章

Android推送通知:图标未显示在通知中,而是显示白色方块

提问于
浏览
127

我的应用会生成通知,但我没有显示为该通知设置的图标 . 相反,我得到一个白色方块 .

我已经尝试调整图标的大小(尺寸720x720,66x66,44x44,22x22) . 奇怪的是,当使用较小尺寸时,白色方块较小 .

我已经google了这个问题,以及生成通知的正确方法,从我读过的代码应该是正确的 . 可悲的是,事情并不像他们应该的那样 .

我的手机是带有Android 5.1.1的Nexus 5 . 问题还出现在模拟器,带有Android 5.0.1的三星Galaxy s4和带有Android 5.0.1的摩托罗拉Moto G上(我借用了这两款,现在还没有)

接下来是通知代码和两个屏幕截图 . 如果您需要更多信息,请随时提出要求 .

谢谢你们 .

@SuppressLint("NewApi") private void sendNotification(String msg, String title, String link, Bundle bundle) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
    resultIntent.putExtras(bundle);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            resultIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    Notification notification;
    Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notificationsound);
    notification = new Notification.Builder(this)
                .setSmallIcon(R.drawable.lg_logo)
                .setContentTitle(title)
                .setStyle(new Notification.BigTextStyle().bigText(msg))
                .setAutoCancel(true)
                .setContentText(msg)
                .setContentIntent(contentIntent)
                .setSound(sound)
                .build();
    notificationManager.notify(0, notification);
}

without opening the notification

notifications opened

16 回答

  • 15

    解决此问题的要求:

    • 图像格式:32位PNG(带alpha)

    • 图像应该是透明的

    • 透明度颜色索引:白色(FFFFFF)

    资料来源:http://gr1350.blogspot.com/2017/01/problem-with-setsmallicon.html

  • 4

    我找到了一个链接,我们可以生成自己的白色图标,

    尝试此链接以生成启动器图标的白色图标 .

    打开此Link并上传您的ic_launcher或通知图标

  • 3

    您可以为不同版本使用不同的图标 . 只需在您的图标上设置逻辑,如下所示:

    int icon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.colored_: R.drawable.white_tint_icon_for_lolipop_or_upper;
    
  • 7

    Cause: For 5.0 Lollipop "Notification icons must be entirely white".

    如果我们通过将目标SDK设置为20来解决白色图标问题,我们的应用程序将不会针对Android Lollipop,这意味着我们无法使用Lollipop特有的功能 .

    Solution for target Sdk 21

    如果您想支持棒棒糖素材图标,请为Lollipop及以上版本制作透明图标 . 请参考以下内容:https://design.google.com/icons/

    请查看http://developer.android.com/design/style/iconography.html,我们会看到白色样式是Android Lollipop中显示通知的方式 .

    在Lollipop中,Google还建议我们使用将在白色通知图标后面显示的颜色 . 参考链接:https://developer.android.com/about/versions/android-5.0-changes.html

    我们想要添加颜色的地方https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setColor(int)

    Implementation of Notification Builder for below and above Lollipop OS version would be:

    Notification notification = new NotificationCompat.Builder(this);
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        notification.setSmallIcon(R.drawable.icon_transperent);
        notification.setColor(getResources().getColor(R.color.notification_color));
    } else { 
        notification.setSmallIcon(R.drawable.icon);
    }
    

    Note: setColor is only available in Lollipop and it only affect to background of the icon.

    It will solve your problem completely!!

  • 6

    如果您使用的是Google Cloud 消息传递,则只需更改图标即可解决问题 will not be solved . 例如,这不起作用:

    Notification notification  = new Notification.Builder(this)
                    .setContentTitle(title)
                    .setContentText(text)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setContentIntent(pIntent)
                    .setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE)
                    .setAutoCancel(true)
                    .build();
    

    Even if ic_notification是透明和白色的 . 它必须也在Manifest元数据中定义,如下所示:

    <meta-data android:name="com.google.firebase.messaging.default_notification_icon"
    
                android:resource="@drawable/ic_notification" />
    

    元数据位于 application 标记下,供参考 .

  • 0

    我真的建议遵循Google的设计指南:

    其中说 "Notification icons must be entirely white."

  • 20

    在Android Manifest中声明此代码:

    <meta-data android:name="com.google.firebase.messaging.default_notification_icon" 
    
    android:resource="@drawable/ic_stat_name" />
    

    我希望这对你有用 .

  • 3

    我们可以这样做:

    创建通知构建器的新对象,并使用通知构建器对象调用 setSmallIcon() ,如下面的代码所示 .

    创建一个方法,我们将检查我们正在安装应用程序的操作系统版本 . 如果它低于Lolipop即API 21,那么它将采用具有背景颜色的普通应用程序图标,否则它将采用没有任何背景的透明应用程序图标 . 因此,使用os版本> = 21的设备将使用Notification builder类的方法 setColor() 设置图标的背景颜色 .

    Sample Code:

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    
    notificationBuilder.setSmallIcon(getNotificationIcon(notificationBuilder));
    
    private int getNotificationIcon(NotificationCompat.Builder notificationBuilder) {
    
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                 int color = 0x008000;
                 notificationBuilder.setColor(color);
                 return R.drawable.app_icon_lolipop_above;
    
        } 
        return R.drawable.app_icon_lolipop_below;
    }
    
  • 34

    最后我得到了解决这个问题的方法 .

    This issue occurs only when the app is not at all running. (neither in the background nor in the foreground) . 当 app runs on either foreground or background the notification icon is displayed properly . (不是白色方块)

    So what we've to set is the same configuration for notification icon in Backend APIs as that of Frontend.

    在前端,我们使用了 React Native ,对于推送通知,我们使用了react-native-fcm npm package .

    FCM.on("notification", notif => {
       FCM.presentLocalNotification({
           body: notif.fcm.body,
           title: notif.fcm.title,
           big_text: notif.fcm.body,
           priority: "high",
           large_icon: "notification_icon", // notification icon
           icon: "notification_icon",
           show_in_foreground: true,
           color: '#8bc34b',
           vibrate: 300,
           lights: true,
           status: notif.status
       });
    });
    

    我们使用fcm-push npm package使用 Node.js 作为推送通知的后端,并按如下方式设置有效负载结构 .

    {
      to: '/topics/user', // required
      data: {
        id:212,
        message: 'test message',
        title: 'test title'
      },
      notification: {
        title: 'test title',
        body: 'test message',
        icon : 'notification_icon', // same name as mentioned in the front end
        color : '#8bc34b',
        click_action : "BROADCAST"
      }
    }
    

    什么它基本上搜索我们的Android系统本地存储的notification_icon图像 .

  • 10
    <meta-data android:name="com.google.firebase.messaging.default_notification_icon"
    
            android:resource="@drawable/ic_notification" />
    

    在应用程序块的manifest.xml文件中添加此行

  • 7

    如果您想提供棒棒糖支持通知图标,请制作两个类型的通知图标:

    • 正常通知图标:用于以下棒棒糖版本 .
      带有透明背景的
    • 通知图标:用于棒棒糖及以上版本 .

    现在,根据操作系统版本在运行时将相应的图标设置为通知构建器:

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mBuilder.setSmallIcon(R.drawable.ic_push_notification_transperent);
    } else {
        mBuilder.setSmallIcon(R.drawable.ic_push_notification);
    }
    
  • 1

    试试这个

    我面临同样的问题,我尝试了很多回答,但没有得到任何解决方案,最后我找到了解决我的问题的方法 .

    - 制作带有透明背景的通知图标 . 应用程序的宽度和高度必须类似于以下尺寸,并将所有这些粘贴到您的项目中 - > app-> src-> main-> res

    • MDPI 24 * 24

    • HDPI 36 * 36

    • XHDPI 48 * 48

    • XXHDPI 72 * 72


    在上面粘贴了下面的onMessageReceived方法中的这一行


    Intent intent = new Intent(this, News.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 (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
                {
                    notificationBuilder.setSmallIcon(R.drawable.notify)
                                          //            .setContentTitle(title)
                                //                        .setContentText(message)
                            .setAutoCancel(true)
                            .setSound(defaultSoundUri)
                            .setContentIntent(pendingIntent);
                } else
                    {
                        notificationBuilder.setSmallIcon(R.drawable.notify)
                           //                                .setContentTitle(title)
                            //                        .setContentText(message)
                                .setAutoCancel(true)
                                .setSound(defaultSoundUri)
                                .setContentIntent(pendingIntent);
                }
                NotificationManager notificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.notify(0, notificationBuilder.build());
    

    不要忘记在清单文件中添加此代码

    <meta-data 
    android:name="com.google.firebase.messaging.default_notification_icon" 
    android:resource="@drawable/app_icon" />
    
  • 1

    通知是 greyscale ,如下所述 . 尽管别人写过,但它们并非黑白分明 . 您可能已经看到了具有多种阴影的图标,例如网络强度条 .

    在API 21(Lollipop 5.0)之前,颜色图标有效 . 您可以强制您的应用程序进行定位API 20,但限制了应用程序可用的功能,因此不建议这样做 . 您可以测试正在运行的API级别并适当地设置颜色图标或灰度图标,但这可能不值得 . 在大多数情况下,最好使用灰度图标 .

    图像有四个通道,RGBA(红色/绿色/蓝色/ alpha) . 对于通知图标,Android会忽略R,G和B通道 . 唯一重要的通道是Alpha,也称为不透明度 . 使用编辑器设计图标,使您可以控制绘图颜色的Alpha值 .

    How Alpha values generate a greyscale image:

    • Alpha = 0(透明) - 这些像素是透明的,显示背景颜色 .

    • Alpha = 255(不透明) - 这些像素为白色 .

    • Alpha = 1 ... 254 - 这些像素正是您所期望的,提供透明和白色之间的阴影 .

    Changing it up with setColor:

    • 致电 NotificationCompat.Builder.setColor(int argb) . 从 Notification.color 的文档:

    呈现此通知时,标准样式模板应用的强调颜色(ARGB整数,如颜色中的常量) . 当前模板设计通过在该颜色的字段顶上覆盖图标图像(以白色印刷)来构建彩色 Headers 图像 . Alpha组件被忽略 .

    我使用setColor进行的测试显示Alpha组件被 not 忽略 . 较高的Alpha值会使像素变为白色 . 较低的Alpha值会将一个像素转换为通知区域中的背景颜色(我的设备上为黑色),或者为下拉通知中的指定颜色 .

  • 5

    对于SDK> = 23,请添加setLargeIcon

    notification = new Notification.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(context.getResources(), R.drawable.lg_logo))
                .setContentTitle(title)
                .setStyle(new Notification.BigTextStyle().bigText(msg))
                .setAutoCancel(true)
                .setContentText(msg)
                .setContentIntent(contentIntent)
                .setSound(sound)
                .build();
    
  • 147

    When you want keep colorful icon - Workaround
    将颜色略有不同的像素添加到图标中 .
    在我的情况下,有一个带有阴影和光线的黑色图标 . 当添加深蓝色像素时,它可以工作 .

  • 0

    我在android 8.0上有类似的问题 . 尝试使用WHITE图标资源 . 当我尝试使用彩色图像作为图标时,我有白色方块,当我将其替换为白色图标时,它开始工作 .

相关问题