Is setContentIntent (PendingIntent) required in NotificationCompat.Builder?

Call:

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

in onCreate() my main action gives:

 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 

Pay attention to contentIntent .

However, docs could not be more comprehensible :

Required Notification Content

The notification object should contain the following:

  • Small icon setSmallIcon ()

  • Header given by setContentTitle ()

  • Detailed text given by setContentText ()

Advanced settings and notification settings

All other settings and contents of notifications are optional. To learn more about them, see the Help documentation for NotificationCompat.Builder.

This opinion is reflected in various SO answers and leads to SO questions (and one more ).

workaround:

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

But is it really necessary? Is there any Android docs error in this situation? Is it API dependent?

NB my target SDK is 17 and runs this on phone 2.3.7

+8
android android-notifications
Nov 17 '13 at 15:08
source share
1 answer

If you use a caching service like waybackmachine and you are looking for previous versions of the notification guide, you will see that the guide tells you that contentIntent necessary.

This is also reflected in the Android device. NotificationManagerService processes the verification of notifications before displaying them.

In Gingerbread , as part of the enqueueNotificationInternal() method, it has this check:

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

In later versions of Android, such as Ice Cream Sandwich , this check failed:

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

Thus, a contentIntent is required on Gingerbread and below.

+19
Nov 17 '13 at 16:06
source share



All Articles