我的Android应用程序有一个小问题。它通过FCM接收通知,并将其显示为推送通知。到目前为止一切正常,但奇怪的问题是,图标有时是白色的,有时是彩色的。
当应用程序在屏幕上打开时,我在这一刻收到了推送通知,五颜六色的推送通知显示在屏幕顶部。
当应用程序关闭时,我会收到一个白色图标的推送通知。
我附上了一个截图:Screenshot
下面是创建推送通知的代码片段:
Notification.Builder notificationBuilder = new Notification.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_alert)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setAutoCancel(true)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setPriority(Notification.PRIORITY_HIGH)
.setColor(Color.parseColor("#83c3ed"))
.setLights(Color.RED, 1000, 500)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
inboxStyle.setBigContentTitle("WetterApp");
inboxStyle.addLine(notification.getTitle());
inboxStyle.addLine(notification.getBody());
notificationBuilder.setStyle(inboxStyle);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
我的移动设备是Android 6.0.1,我的SDK版本是23。
谢谢你的帮助。
发布于 2016-06-14 18:21:50
好吧,我想问题出在别的地方。我创建通知的代码只有在应用程序在屏幕上打开时才会调用。当我收到通知,应用程序关闭时,Android系统会自动处理通知。
我必须在通知中设置颜色,通知将发送到FCM服务器:
$data = [
'notification' => [
'title' => 'Warnung: Wohnzimmer',
'text' => 'Innen: 20,3°C Außen: 24,5°C, Tendenz: -0,2°C',
'color' => '#83c3ed',
'sound' => 'default'
],
'to' => '/topics/testNotification'
];
现在,在应用程序内和应用程序关闭时,我都会看到一个浅蓝色背景图标。
发布于 2016-06-13 19:21:16
按照谷歌的guide来创建你的图标,它可能也出现在状态栏中。
然后,试试这个:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setTicker(context.getResources().getString(R.string.app_name));
builder.setSmallIcon(R.mipmap.ic_your_status_bar_logo);
builder.setAutoCancel(true);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setContentIntent(pendingIntent);
builder.setContentTitle(
context.getResources().getString(R.string.app_name));
builder.setContentText(message);
builder.setDefaults(Notification.DEFAULT_SOUND);
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
builder.setColor(ContextCompat.getColor(context, R.color.color_primary));
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NOTIFICATION_ID, builder.build());
发布于 2016-06-13 18:27:25
我认为一个更好的解决方案是在应用程序中添加一个剪影图标,如果设备运行的是Android棒棒糖,就可以使用它。
例如:
Notification notification = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle("My notification")
.setContentText("Look, white in Lollipop, else color!")
.setSmallIcon(getNotificationIcon())
.build();
return notification;
并且,在getNotificationIcon方法中:
private int getNotificationIcon() {
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? R.drawable.icon_silhouette : R.drawable.ic_launcher;
}
https://stackoverflow.com/questions/37796695
复制