FATAL EXCEPTION when useing setSmallIcon(Icon i) On Android 6.0.1

0

I am trying to make a dynamic small icon on Notification bar. And it worked well on Android 8.0.0. But there is problem on Android 6.0.1.

Here is the code:

Intent notificationIntent = new Intent(getApplicationContext(), ResumeMainActivity.class);
            intent.putExtra("type", "exit");
            PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
                    0, notificationIntent, 0);
            Bundle a = new Bundle();
            a.putString("type", "exit");

            Intent snoozeIntent = new Intent(getApplicationContext(), Foreground.class);
            snoozeIntent.putExtra(EXTRA_NOTIFICATION_ID, 0);
            snoozeIntent.setAction("Stop");
            PendingIntent snoozePendingIntent =
                    PendingIntent.getService(getApplicationContext(), 0, snoozeIntent, 0);


            Notification.Builder notification = null;
            NotificationCompat.Builder notificationBelow = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                notification = new Notification.Builder(getApplicationContext())
                        .setContentTitle("Title")
                        .setContentText("Tap to open app.")
                        .setSmallIcon(Icon.createWithBitmap(drawText(totalDataSent, totalDataCollected)))

                        .setContentIntent(pendingIntent)
                        .setExtras(a);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    notification.setChannelId(CHANNEL_ID);
                }
                notification.addAction(new Notification.Action.Builder(Icon.createWithResource(getApplicationContext(), R.drawable.ic_launcher_foreground), "Stop", snoozePendingIntent).build());
            } else {
                notificationBelow = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
                        .setContentTitle("Title")
                        .setContentText("Tap to open app.")
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setContentIntent(pendingIntent)
                        .setExtras(a)
                        .setChannelId(CHANNEL_ID)
                        .addAction(R.drawable.ic_launcher_foreground, "Stop", snoozePendingIntent);
            }
            NotificationManager manager = (NotificationManager)
                    context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                manager.notify(1, notification.build());
            } else {
                manager.notify(1, notificationBelow.build());
            }

And I get Couldn't update icon: StatusBarIcon(icon=Icon(typ=RESOURCE pkg=com.example.app id=0x00000000) visible user=0 )

Here is the error:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.app, PID: 20774
android.app.RemoteServiceException: Bad notification posted from package com.example.app: Couldn't update icon: StatusBarIcon(icon=Icon(typ=RESOURCE pkg=com.example.app id=0x00000000) visible user=0 )
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2019)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7224)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
android
android-notifications
android-notification-bar
asked on Stack Overflow Mar 6, 2019 by Erik Sahakyan • edited Mar 7, 2019 by c-an

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0