NotificationCompat.Builder中是否需要setContentIntent(PendingIntent)?

时间:2013-11-17 15:08:18

标签: android android-notifications

致电:

public static void triggerTestNotification(Context ctx, String tag, int id) {
    Notification not = new NotificationCompat.Builder(ctx)
        .setContentTitle("Title").setContentText("Text")
        .setAutoCancel(true) // cancel on click
        .setSmallIcon(R.drawable.ic_launcher).build();
    NotificationManager notificationManager = (NotificationManager) ctx
        .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(tag, id, not);
}

在我的主要活动的onCreate()中产生:

11-17 15:58:46.198: E/AndroidRuntime(1507): FATAL EXCEPTION: main
11-17 15:58:46.198: E/AndroidRuntime(1507): java.lang.RuntimeException: Unable to start activity ComponentInfo{gr.uoa.di.monitoring.android/gr.uoa.di.monitoring.android.activities.MainActivity}: java.lang.IllegalArgumentException: contentIntent required: pkg=gr.uoa.di.monitoring.android id=0 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x10)
//...
11-17 15:58:46.198: E/AndroidRuntime(1507): Caused by: java.lang.IllegalArgumentException: contentIntent required: pkg=gr.uoa.di.monitoring.android id=0 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x10)
11-17 15:58:46.198: E/AndroidRuntime(1507):     at android.os.Parcel.readException(Parcel.java:1326)
11-17 15:58:46.198: E/AndroidRuntime(1507):     at android.os.Parcel.readException(Parcel.java:1276)
11-17 15:58:46.198: E/AndroidRuntime(1507):     at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274)
11-17 15:58:46.198: E/AndroidRuntime(1507):     at android.app.NotificationManager.notify(NotificationManager.java:133)
11-17 15:58:46.198: E/AndroidRuntime(1507):     at gr.uoa.di.monitoring.android.C.triggerTestNotification(C.java:200)
11-17 15:58:46.198: E/AndroidRuntime(1507):     at gr.uoa.di.monitoring.android.activities.MainActivity.onCreate(MainActivity.java:44)
11-17 15:58:46.198: E/AndroidRuntime(1507):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-17 15:58:46.198: E/AndroidRuntime(1507):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
11-17 15:58:46.198: E/AndroidRuntime(1507):     ... 11 more

请注意 contentIntent所需的

然而,文档could not be more clear

  

必填通知内容

     

Notification对象必须包含以下内容:

     
      
  • 由setSmallIcon()

  • 设置的小图标   
  • 标题,由setContentTitle()

  • 设置   
  • 详细文本,由setContentText()

  • 设置   
     

可选的通知内容和设置

     

所有其他通知设置和内容都是可选的。要了解有关它们的更多信息,请参阅NotificationCompat.Builder的参考文档。

此观点反映在various SO answers中,结果为SO questions(及another一个。)

解决方法:

final Intent emptyIntent = new Intent();
PendingIntent pi = PendingIntent.getActivity(ctx, NOT_USED,
    emptyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//...
.setContentIntent(pi).build;

但这真的需要吗?这种情况是否是另一个 Android docs bug ?它是否依赖于API?

注意我的目标SDK是17并在2.3.7手机上运行

1 个答案:

答案 0 :(得分:20)

如果您使用像waybackmachine这样的缓存服务并且查找了通知指南的previous versions,您会看到指南确实告诉您contentIntent是必需的。

这也反映在Android源代码中。 NotificationManagerService在显示通知之前处理通知的检查。

Gingerbread中,作为enqueueNotificationInternal()方法的一部分,它有以下检查:

if (notification.icon != 0) {
    if (notification.contentView == null) {
          throw new IllegalArgumentException("contentView required: pkg=" + pkg
                    + " id=" + id + " notification=" + notification);
    }
    if (notification.contentIntent == null) {
        throw new IllegalArgumentException("contentIntent required: pkg=" + pkg
                + " id=" + id + " notification=" + notification);
    }
}

在以后的Android版本中,例如Ice Cream Sandwich,该检查已经消失:

if (notification.icon != 0) {
    if (notification.contentView == null) {
       throw new IllegalArgumentException("contentView required: pkg=" + pkg
              + " id=" + id + " notification=" + notification);
    }
}

因此,姜饼及其下方需要<{1}}