Blu*_*ver 155 notifications icons android
我的应用会生成通知,但我没有显示为该通知设置的图标.相反,我得到一个白色方块.
我已经尝试调整图标的大小(尺寸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);
}
Run Code Online (Sandbox Code Playgroud)
Gar*_*hur 167
原因:对于5.0 Lollipop"通知图标必须完全是白色".
如果我们通过将目标SDK设置为20来解决白色图标问题,我们的应用程序将不会针对Android Lollipop,这意味着我们无法使用Lollipop特有的功能.
目标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)
针对以下Lollipop OS版本的Notification Builder的实现将是:
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);
}
Run Code Online (Sandbox Code Playgroud)
注意:setColor仅在Lollipop中可用,它只影响图标的背景.
它将完全解决您的问题!
Ruc*_*nia 50
如果您使用的是Google云消息传递,则只需更改图标即可解决此问题.例如,这不起作用:
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();
Run Code Online (Sandbox Code Playgroud)
即使 ic_notification是透明的和白色的.它必须也在Manifest元数据中定义,如下所示:
<meta-data android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_notification" />
Run Code Online (Sandbox Code Playgroud)
元数据在application
标签下,供参考.
N J*_*N J 29
其中说"通知图标必须完全是白色的".
ole*_*234 29
(Android Studio 3.5)如果您使用的是最新版本的 Android Studio,则可以生成通知图像。右键单击您的 res 文件夹 > New > Image Asset。然后,您将看到配置图像资产,如下图所示。将图标类型更改为通知图标。您的图像必须是白色和透明的。此配置图像资产将强制执行该规则。
重要提示:如果您希望图标用于云/推送通知,您必须在应用程序标签下添加元数据以使用新创建的通知图标。
<application>
...
<meta-data android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_notification" />
Run Code Online (Sandbox Code Playgroud)
vic*_*ale 21
在Android Manifest中声明此代码:
<meta-data android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_name" />
Run Code Online (Sandbox Code Playgroud)
我希望这对你有用.
小智 10
我们可以这样做:
创建通知构建器的新对象,并setSmallIcon()
使用通知构建器对象进行调用,如下面的代码所示.
创建一个方法,我们将检查我们正在安装应用程序的操作系统版本.如果它低于Lolipop即API 21,那么它将采用带有背景颜色的普通应用程序图标,否则它将采用没有任何背景的透明应用程序图标.因此,使用os版本> = 21的设备将使用setColor()
Notification builder类的方法设置图标的背景颜色.
示例代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
<meta-data android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_notification" />
Run Code Online (Sandbox Code Playgroud)
在应用程序块的manifest.xml文件中添加此行
我面临同样的问题,我尝试了很多回答,但没有得到任何解决方案,最后我找到了解决我的问题的方法.
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());
Run Code Online (Sandbox Code Playgroud)
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/app_icon" />
Run Code Online (Sandbox Code Playgroud)
如果您想提供棒棒糖支持通知图标,请制作两个类型的通知图标:
现在,根据操作系统版本在运行时将相应的图标设置为通知构建器:
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);
}
Run Code Online (Sandbox Code Playgroud)
最后我得到了解决这个问题的方法.
仅当应用程序未运行时才会出现此问题.(既不在背景中也不在前景中).当应用程序在前台或后台运行时,通知图标会正确显示.(不是白色方块)
因此,我们要设置的是后端API中的通知图标与前端的配置相同.
在前端,我们使用了React Native,对于推送通知,我们使用了react-native-fcm npm包.
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
});
});
Run Code Online (Sandbox Code Playgroud)
我们使用fcm-push npm包使用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"
}
}
Run Code Online (Sandbox Code Playgroud)
什么它基本上搜索我们的Android系统本地存储的notification_icon图像.
我通过在清单中添加以下代码解决了这个问题,
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_name" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/black" />
Run Code Online (Sandbox Code Playgroud)
ic_stat_name
在 Android Studio上创建的地方右键单击 res >> New >>Image Assets >> IconType(Notification)
还有一个我必须在服务器 php 端使用通知负载做的步骤
$message = [
"message" => [
"notification" => [
"body" => $title ,
"title" => $message
],
"token" => $token,
"android" => [
"notification" => [
"sound" => "default",
"icon" => "ic_stat_name"
]
],
"data" => [
"title" => $title,
"message" => $message
]
]
];
Run Code Online (Sandbox Code Playgroud)
注意部分
"android" => [
"notification" => [
"sound" => "default",
"icon" => "ic_stat_name"
]
]
Run Code Online (Sandbox Code Playgroud)
图标名称的位置"icon" => "ic_stat_name"
应与清单上的设置相同。
通知是灰度的,如下所述。尽管其他人已经写过,但它们不是黑白的。您可能已经看到带有多种阴影的图标,例如网络强度条。
在API 21(Lollipop 5.0)之前,颜色图标可以使用。您可以强制应用程序以API 20为目标,但这限制了应用程序可用的功能,因此不建议这样做。您可以测试正在运行的API级别,并适当地设置颜色图标或灰度图标,但这可能不值得。在大多数情况下,最好使用灰度图标。
图像具有四个通道,RGBA(红色/绿色/蓝色/ alpha)。对于通知图标,Android会忽略R,G和B通道。唯一重要的渠道是Alpha,也称为不透明度。使用编辑器设计图标,该编辑器可让您控制绘图颜色的Alpha值。
Alpha值如何生成灰度图像:
用以下方法进行更改setColor
:
致电NotificationCompat.Builder.setColor(int argb)
。从文档中Notification.color
:
呈现此通知时,标准样式模板要应用的强调颜色(类似于Color中的常数的ARGB整数)。当前的模板设计通过将图标图像(以白色模版)覆盖在此颜色的字段上方来构造彩色标题图像。Alpha组件将被忽略。
我对setColor的测试表明,不忽略Alpha组件。较高的Alpha值会使像素变白。较低的Alpha值会将像素变为通知区域中的背景颜色(在我的设备上为黑色),或在下拉通知中变为指定的颜色。
小智 5
解决此问题的要求:
图像格式:32 位 PNG(带 Alpha)
图像应该是透明的
透明度颜色索引: 白色 (FFFFFF)
来源:http ://gr1350.blogspot.com/2017/01/problem-with-setsmallicon.html
归档时间: |
|
查看次数: |
167673 次 |
最近记录: |