微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

致电:
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,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

required notification contents

A Notification object must contain the following:

  • A small icon,set by setSmallIcon()

  • A title,set by setContentTitle()

  • Detail text,set by setContentText()

Optional notification contents and settings

All other notification settings and contents are optional. To learn more about them,see the reference documentation for 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手机上运行

解决方法

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

因此,Gingerbread及以下需要contentIntent.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐