artkoenig
artkoenig

Reputation: 7257

Exception while creating an Android Notification

In my app I construct a Notification with the following code:

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder notificationBuilder = new Notification.Builder(this)
    .setSmallIcon(R.drawable.ic_notify_nursing)
    .setOngoing(true)
    .setContentTitle(getString(R.string.feeding_in_progress));

if(android.os.Build.VERSION.SDK_INT >= 21) {
    notificationBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
}

Intent resultIntent = new Intent(this, MainActivity.class);

PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(resultPendingIntent);

if(android.os.Build.VERSION.SDK_INT >= 16) {
    notificationManager.notify(NOTIFICATION_TAG, NOTIFICATION_ID, notificationBuilder.build());
} else {
    notificationManager.notify(NOTIFICATION_ID, notificationBuilder.getNotification());
}

In the Google Play console I see many exceptions logged for this line:

notificationManager.notify(NOTIFICATION_TAG, NOTIFICATION_ID, notificationBuilder.build());

Mostly the error occure in Android 5.0 and 5.1, no problems with older versions. The stacktrace looks like:

java.lang.ArrayIndexOutOfBoundsException: src.length=0 srcPos=0 dst.length=1 dstPos=0 length=1
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3326)
    at android.app.ActivityThread.access$2200(ActivityThread.java:178)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1547)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5944)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: src.length=0 srcPos=0 dst.length=1 dstPos=0 length=1
    at java.lang.System.arraycopy(System.java:358)
    at android.util.ArrayMap.putAll(ArrayMap.java:579)
    at android.os.Bundle.putAll(Bundle.java:183)
    at android.app.Notification$Builder.build(Notification.java:3786)

Any ideas what is going on here?

Upvotes: 0

Views: 795

Answers (3)

ianhanniballake
ianhanniballake

Reputation: 199805

Per the notification guide list of required fields, you must set the content text via the setContentText() method. Note that this can be blank, but it needs to be set.

Upvotes: 1

PyrotechnicStudios
PyrotechnicStudios

Reputation: 1

It looks as though the method was deprecated as of API 11 as discussed below..

Issue with Notification Manager on android

Upvotes: 0

BladeCoder
BladeCoder

Reputation: 12929

Use NotificationCompat.Builder instead to make your code simpler and avoid surprises.

Upvotes: 0

Related Questions