Android NotificationManager给我“无效小图标”错误

时间:2015-11-11 00:54:28

标签: android google-cloud-messaging notificationmanager

我在通知管理器方面遇到了一个奇怪的错误。

@Override
public void onMessageReceived(String from, Bundle data)
{
     Log.i(TAG, "IP : " + (String) data.get("ip"));
     NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
     Intent acceptNextIntent = new Intent(MainActivity.BROADCAST_KEY_ACCEPT);
//        acceptNextIntent.putExtra("ip", (String) data.get("blah")); //add stuff here
     PendingIntent acceptNextPendingIntent = PendingIntent.getBroadcast(this, 0, acceptNextIntent, 0);

     Intent declineNextIntent = new Intent(MainActivity.BROADCAST_KEY_DECLINE);
     PendingIntent declineNextPendingIntent = PendingIntent.getBroadcast(this, 0, declineNextIntent, 0);

     NotificationCompat.Action acceptAction = new NotificationCompat.Action
                .Builder(R.drawable.common_signin_btn_icon_disabled_focus_light, "Grant Request", acceptNextPendingIntent).build();

     NotificationCompat.Action declineAction = new NotificationCompat.Action
                .Builder(R.drawable.common_signin_btn_icon_focus_dark, "Decline Request", declineNextPendingIntent).build();

     NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
                .setContentTitle("New Password Request From " + (String) data.get("ip"))
                .addAction(acceptAction)
                .addAction(declineAction);

     notificationManager.notify(1, notification.build()); //ERROR HERE

错误讯息:

9.474 9327-9371/com.inh.amnesia_application I/MyGcmListenerService: IP : 128.239.213.39
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime: Process: com.inh.amnesia_application, PID: 9327
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime: java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(pri=0 contentView=com.inh.amnesia_application/0x1090085 vibrate=null sound=null defaults=0x0 flags=0x0 color=0x00000000 actions=2 vis=PRIVATE)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at android.app.NotificationManager.notify(NotificationManager.java:222)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at android.app.NotificationManager.notify(NotificationManager.java:194)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at com.inh.amnesia_application.MyGcmListenerService.onMessageReceived(MyGcmListenerService.java:65)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at com.google.android.gms.gcm.GcmListenerService.zzt(Unknown Source)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at com.google.android.gms.gcm.GcmListenerService.zzk(Unknown Source)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at com.google.android.gms.gcm.GcmListenerService.zza(Unknown Source)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at com.google.android.gms.gcm.GcmListenerService$1.run(Unknown Source)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818)

这是否意味着我尝试访问的图标不存在?我不确定如何解释此错误并搜索此错误消息不会产生任何结果。

4 个答案:

答案 0 :(得分:29)

您实际上没有为推送通知设置图标。将.setSmallIcon(R.drawable.your_icon)添加到您的通知中。

 NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
            .setContentTitle("New Password Request From " + (String) data.get("ip"))
            .setSmallIcon(R.drawable.your_icon)
            .addAction(acceptAction)
            .addAction(declineAction);

答案 1 :(得分:7)

您尚未拨打setSmallIcon() on the NotificationCompat.Builder。这会在Notification处于活动状态时提供状态栏中的图标。

答案 2 :(得分:6)

根据Android NotificationManager Source Code

if (mContext.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
    if (notification.getSmallIcon() == null) {
        throw new IllegalArgumentException("Invalid notification (no valid small icon): "
                + notification);
    }
}

只有在设置目标API>时才会出现此错误LOLLIPOP_MR1(22)和通知没有小图标。

答案 3 :(得分:0)

这突然开始在我的应用程序中发生。经过一番研究,我发现清单文件中的这种更改实际上开始引起了它。

来自:

  <uses-sdk android:minSdkVersion="21" />

收件人:

  <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />

我在某处看到有人指出,如果设置了目标sdk版本,它只会检查图标。当然,在源代码中是这样的:

if (mContext.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
    if (notification.getSmallIcon() == null) {
        throw new IllegalArgumentException("Invalid notification (no valid small icon): "
                + notification);
    }
}

幸运的是,解决方案很简单,只需在清单中定义图标即可:

  <application android:label="MyApp" android:icon="@mipmap/icon" >