我正在android
中实现通知。我想更改通知图标,但是问题是setSmallIcon
无法正常工作。我的自定义图标显示在lollipop
设备中,但不显示lollipop
设备上方的自定义图标。
我尝试了以下代码:
send_notification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.book2);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("New Book Added")
.setContentText("Android with java")
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(icon)
.bigLargeIcon(null));
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setSmallIcon(R.drawable.ic_notification);
} else {
builder.setSmallIcon(R.drawable.notification);
}
NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(1,builder.build());
}
}
大图也没有显示。请帮忙。
编辑:
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.test);
String channelId = getString(R.string.default_notification_channel_id);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);
if (remoteMessage.getData().size() > 0) {
String title = remoteMessage.getData().get("title");
String text = remoteMessage.getData().get("body");
builder.setContentTitle(title)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(text)
.setLargeIcon(icon)
.setPriority(Notification.PRIORITY_MAX)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigLargeIcon(icon));
} else if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle();
String text = remoteMessage.getNotification().getBody();
builder.setContentTitle(title)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(text)
.setLargeIcon(icon)
.setPriority(Notification.PRIORITY_MAX)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigLargeIcon(icon));
}
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(channelId, "Testing channel", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(notificationChannel);
}
manager.notify(1, builder.build());
答案 0 :(得分:1)
尝试卸载并重新安装100%新鲜的App。 Android会保持高速缓存,因此事情可以更快地处理,而通知就是其中之一。
一段时间前,卸载和重新安装可解决我的问题。
还请记住,在Android Oreo及更高版本中,为了使通知起作用;通知频道必须创建,并且您要创建的通知也应分配给其中一个。
我尝试过的方法(对我有用):(科特林的样本,但应该足够相似);
fun onClick(view: View) {
var icon_resource = R.drawable.ic_mtrl_chip_close_circle
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
icon_resource = R.drawable.ic_mtrl_chip_checked_circle
}
val icon = BitmapFactory.decodeResource(resources,R.mipmap.ic_launcher_round)
val notification = NotificationCompat.Builder(this)
.setSmallIcon(icon_resource)
.setContentTitle("New Book Added")
.setContentText("Android with java")
.setLargeIcon(icon)
.setStyle(NotificationCompat.BigPictureStyle()
.bigPicture(icon)
.bigLargeIcon(null)).build()
val manager= getSystemService(NOTIFICATION_SERVICE) as NotificationManager
manager.notify(1,notification)
}
我认为您的图像有问题(例如我的图像,尺寸之类的东西)。在花了很多时间试图弄清楚为什么它没有在通知中显示启动器图标之后,我决定尝试使用我放置的一张图像...
// Parametherise this drawable with an if statement of your own
var icon_resource = R.drawable.ic_mtrl_chip_close_circle
val manager= NotificationManagerCompat.from(this)
// This is required because the testing device is an Android O
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
manager.createNotificationChannel(
NotificationChannel("CUSTOM_NOTIFICATIONS", "StackOverflow", NotificationManager.IMPORTANCE_HIGH)
)
}
val icon = BitmapFactory.decodeResource(resources,R.mipmap.dafuq)
val notification = NotificationCompat.Builder(this, "CUSTOM_NOTIFICATIONS")
.setSmallIcon(icon_resource)
.setContentTitle("New Book Added")
.setContentText("Android with java")
.setLargeIcon(icon)
.setStyle(NotificationCompat.BigPictureStyle()
.bigPicture(icon)
.bigLargeIcon(null)).build()
manager.notify(1,notification)
当我使用错误的图像时,我在Logcat中得到了这个讨厌的日志。检查相同或相似!
2019-04-10 20:11:02.120 3434-3434/com.example.deletemeapp D/skia: --- Failed to create image decoder with message 'unimplemented'
答案 1 :(得分:0)
您要执行的操作如下创建通知图标:
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("New Book Added")
.setContentText("Android with java");
现在添加:
NotificationCompat.BigPictureStyle s = new NotificationCompat.BigPictureStyle().bigPicture(bitmap_image);
s.setSummaryText("Summary text appears on expanding the notification");
//Set the style of the notification to this big picture style
builder.setStyle(s);
对于版本控制,请添加以下内容:
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
// Marshmallow+
}else{
//below Marshmallow
}
希望这会有所帮助:)