Android - java.lang.IllegalArgumentException: contentIntent required error caused by notification?

后端 未结 4 2248
北荒
北荒 2021-02-19 00:52

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 so

相关标签:
4条回答
  • 2021-02-19 01:03

    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!

    0 讨论(0)
  • 2021-02-19 01:13

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

    0 讨论(0)
  • 2021-02-19 01:14

    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

    0 讨论(0)
  • 2021-02-19 01:19

    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.
    0 讨论(0)
提交回复
热议问题