gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:22:53 26 4
gpt4 key购买 nike

调用:

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);
}

在我的主要 Activity 的 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 :

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.

这个意见反射(reflect)在various所以 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 手机上运行

最佳答案

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

这也反射(reflect)在 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 及以下版本上需要

关于android - NotificationCompat.Builder 是否需要 setContentIntent(PendingIntent)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20032249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com