【问题标题】:Is setContentIntent(PendingIntent) required in NotificationCompat.Builder?NotificationCompat.Builder 中是否需要 setContentIntent(PendingIntent)?
【发布时间】:2013-11-17 15:08:18
【问题描述】:

来电:

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

必需的通知内容

通知对象必须包含以下内容:

  • 一个小图标,由 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 文档错误 吗?它依赖于 API 吗?

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

【问题讨论】:

  • 拥有Notification 而没有contentIntent 是用户体验的味道,恕我直言。如果您的应用非常重要,足以用 Notification 来骚扰用户,请让用户可以做一些事情来回应 Notification
  • @CommonsWare:谢谢(我用它来提醒用户打开应用程序的电池电量不足警告 - 不知道) - 但不是 IllegalArgumentException 太多的惩罚气味?无论如何,我知道这种行为 - 我只是问因为我相信如果确实如此,那么必须在谷歌提出一个问题。这是完全不可接受的——除非我错过了什么。
  • “我将它用于电池电量不足的警告,提醒用户打开应用程序”——那么您应该使用PendingIntent 来打开应用程序(不管是什么意思)。

标签: android android-notifications


【解决方案1】:

如果您使用像 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);
    }
}

因此,contentIntent 在 Gingerbread 及以下版本中是必需的

【讨论】:

  • 先生,这就是我所说的答案 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
  • 2011-11-02
  • 2014-03-07
  • 1970-01-01
相关资源
最近更新 更多