【问题标题】:java.lang.IllegalArgumentException: Invalid notification (no valid small icon) [duplicate]java.lang.IllegalArgumentException:无效通知(没有有效的小图标)[重复]
【发布时间】:2020-09-07 22:27:39
【问题描述】:

你好有人可以告诉我如何在不收到此异常的情况下发出通知:java.lang.IllegalArgumentException:无效通知(没有有效的小图标):通知(频道=null pri=0 contentView=null vibrate=null sound=null默认值=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());
}

【问题讨论】:

标签: java android firebase android-studio exception


【解决方案1】:

您正在构建两个通知。另一种基于没有小图标或任何其他内容的空构建器:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

...

NotificationManager mannager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mannager.notify(0,builder.build());

您可以删除此代码。

【讨论】:

  • 效果很好,谢谢!我一直在努力解决这个问题。
猜你喜欢
  • 1970-01-01
  • 2013-06-29
  • 2011-07-23
  • 2019-10-04
  • 1970-01-01
  • 2017-02-25
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多