18

I have a service running that updates a notification in the notification bar when it recieves a message saying it has to be changed.

However I get the following error sometimes when the notification is to be updated

java.lang.IllegalArgumentException: contentIntent required

Here is my code:

Variable setup


int icon = R.drawable.notification;
CharSequence tickerText = "Test";
long when = System.currentTimeMillis();
PendingIntent contentIntent;

Notification notification = new Notification(icon, tickerText, when);

NotificationManager mNotificationManager;

NotificationManager Creation


    String ns = Context.NOTIFICATION_SERVICE;
    mNotificationManager = (NotificationManager) getSystemService(ns);

Notification Creation


    Intent notificationIntent = new Intent(this, TestsApp.class);
    contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.flags |= Notification.FLAG_NO_CLEAR;
    notification.icon = R.drawable.notification3;
    notification.setLatestEventInfo(this, "Registering", "Test", contentIntent);
    mNotificationManager.notify(1, notification);

Update of Notification


    notification.icon = R.drawable.notification2;
    notification.setLatestEventInfo(getApplicationContext(), "Registered", "Test", contentIntent);
    mNotificationManager.notify(1, notification);   

So something is happening my contentIntent somewhere along the line, would that be correct?

It is declared at the top of my Service class as a member variable and is not used anywhere else in the code apart from shown above, so where could it be getting reset to null?

Donal Rafferty
  • 19,707
  • 39
  • 114
  • 191

4 Answers4

15

you need to set the contentIntent for your notification.

in your case:

notification.contentIntent = notificationIntent;

otherwise you will get the message, that the contentIntent of the notification is null, because it's not set.

the docu is here: http://developer.android.com/reference/android/app/Notification.html#contentIntent

i have a little example here: http://united-coders.com/nico-heid/show-progressbar-in-notification-area-like-google-does-when-downloading-from-android

nheid
  • 1,088
  • 2
  • 11
  • 17
  • 5
    Not all versions of Android require this. I had a case that was working fine on everything except the Kindle Fire. – Ed Burnette Jun 18 '12 at 20:54
  • 4
    Do you know what I do if I don't want any intent to fire on clicking the notification? I just want it to be there whilst the system is doing something and it will go on its own. – Richard Green Oct 24 '12 at 02:23
  • for my case, showing notificationBar from Service, that would be mBuilder.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0)); – toantran Nov 15 '13 at 06:43
  • 2
    Interestingly the docs specify that 3 things are required, but the contentIntent is not one of them. http://developer.android.com/guide/topics/ui/notifiers/notifications.html – Richard Fung Jun 18 '14 at 23:21
3

I think this is because the Android OS Version

The version 2.3 or lower,must set contentIntent,if not,you will get this Exception.

In my project,I write like this:

 if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) { 
     Intent intent = new Intent();
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0); 
     mNotification.contentIntent = contentIntent; 
 }

Perhaps this could help you!

Community
  • 1
  • 1
DomonLee
  • 193
  • 1
  • 12
2

In your case

contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,0);

if you want to use Intents with the same action but different extras:

  1. Change requestCode from default "0" in

    getActivity(Context context, int requestCode, Intent intent, int flags)
    

to something unique like

(int) System.currentTimeMillis();
notification.contentIntent = notificationIntent;

Both steps are mandatory because:

  • Option 2 will not work without option 1.
  • Option 1 will throw IllegalArgumentException without 2.
Hanzala
  • 1,965
  • 1
  • 16
  • 43
tomurka
  • 851
  • 1
  • 7
  • 16
0

In my case, I had an example code to do, with a single Notification to create, and I also got "contentIntent required" error - google brought me to this thread :D

the source of this problem were quotations that I copied from an example code and pasted it in eclipse project. When I deleted " " and typed them back and problem was solved. Maybe this helps someone.

These were quotations source of error: nb.setContentTitle("My first notification!"); nb.setContentText("Hello");

Ranko1977
  • 91
  • 1
  • 4