您好,有人可以建议我如何在不接收此异常的情况下进行通知:java.lang.IllegalArgumentException:无效的通知(没有有效的小图标):Notification(channel = null pri = 0 contentView = null vibrate = null sound = null defaults = 0x0标志= 0x0颜色= 0x00000000 vis = PRIVATE)
代码:
public void notifyThis(String text){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Intent notificationIntent = new Intent(this, RegisterActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT >= 26) {
//When sdk version is larger than26
String id = "channel_1";
String description = "143";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(id, description, importance);
channel.enableLights(true);
channel.enableVibration(true);//
manager.createNotificationChannel(channel);
Notification notification = new Notification.Builder(MapsActivity.this, id)
.setCategory(Notification.CATEGORY_MESSAGE)
.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.screenmec))
.setContentTitle("MECTS.SA")
.setContentText(text)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.build();
manager.notify(1, notification); }
else {
//When sdk version is less than26
Notification notification = new NotificationCompat.Builder(MapsActivity.this)
.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.screenmec))
.setContentTitle("MECTS.SA")
.setContentText(text)
.setContentIntent(contentIntent)
.build();
manager.notify(1,notification); }
NotificationManager mannager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mannager.notify(0,builder.build());
}
您正在建立两个通知。另一个基于没有小图标或任何其他内容的空生成器:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
...
NotificationManager mannager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mannager.notify(0,builder.build());
您可以删除此代码。