Android 应用未运行时通知白色圆圈 运行

Android notification white circle when app is not running

我正在为我的应用程序的推送通知使用 Firebase 通知。 一切正常,但当应用程序不是 运行 时,通知图标显示白色圆圈。 我的目标是 SDK 版本 23,我还使用 Roman Nurik 的通知图标生成器来生成白色透明图标。

当应用程序在前台并且 运行 时,通知图标可以正确显示。 img

但是当应用程序处于后台或被终止时,图标会被替换为通用的白色圆圈。 img

这是我的通知生成器方法:

private void sendNotification(String messageTitle, String messageBody) {

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_notification_icon)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
            .setAutoCancel(true)
            .setPriority(Notification.PRIORITY_DEFAULT)
            .setDefaults(Notification.DEFAULT_ALL)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notification.build());

}

我已经找到了类似问题的答案:

Unfortunately this is a limitation of Firebase Notifications in SDK 9.0.0. When the app is in the background the launcher icon is use from the manifest (with the requisite Android tinting) for messages sent from the console.

If the app is in the foreground (or a data message is sent) you should be able to use your own logic to customise, and you should be able to customise the icon if sending the message from the HTTP/XMPP APIs. Right now with messages sent from the console you'll get this behavior.

似乎避免此 Firebase 通知错误的最佳方法是将 build.gradle 中的 targetSdkVersion 更改为 19。然后通知图标将被着色。一段时间后 Firebase 将解决此问题。

希望对您有所帮助

试试这个代码:

 Intent intent = new Intent(this, TabActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(getNotificationIcon())
            .setContentTitle("IDS DMS Support")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);


    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

我认为如果设备是 运行 Android Lollipop,您需要在应用程序中添加一个剪影图标并使用它。

使用下面的代码获取通知的小图标。

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

您可以看到完整的解决方案

我做了同样的事情并且工作正常。