Android Oreo Notification Crashes System UI
Asked Answered
R

3

15

I've managed to get notifications working in older API's, but not Oreo. Creating the notification causes my app to still work fine (no messages in logcat), however SystemUI crashes and reboots in an endless cycle while the Activity is running. The is the error in logcat for the systemui process:

java.lang.IllegalArgumentException: width and height must be > 0

My code:

private void showPlayingNotification() {
        NotificationCompat.Builder builder = mNotificationUtils.getAndroidChannelNotification(this, "Play", mMediaSessionCompat);
        if( builder == null ) {
            Log.i("Play Notification","No notification found!");
            return;
        }

        mNotificationUtils.getManager().notify(101,builder.build()); 
}

I initialised mNotificationUtils in the onCreate of the MediaPlayerService I created.

public class NotificationUtils extends ContextWrapper {

    private NotificationManager mManager;
    public static final String AUDIO_CHANNEL_ID = "com.liftyourheads.dailyreadings.dailyReadingsAudio";
    public static final String AUDIO_CHANNEL_NAME = "Daily Readings Audio Stream";

    public NotificationUtils(Context base) {
        super(base);
        createChannels();
    }

    public void createChannels() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // create android channel
            NotificationChannel dailyReadingsAudioChannel = new NotificationChannel(AUDIO_CHANNEL_ID,
                    AUDIO_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            getManager().createNotificationChannel(dailyReadingsAudioChannel);

        }
    }

    public NotificationManager getManager() {
        if (mManager == null) {
            mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return mManager;
    }

    public NotificationCompat.Builder getAndroidChannelNotification(Context context, String action, MediaSessionCompat mediaSession) {

        if (action.equals("Play")) {
            return MediaStyleHelper.from(context, mediaSession)
                    .addAction(new NotificationCompat.Action(android.R.drawable.ic_media_pause, "Pause", MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)))
                    .setStyle(
                            new android.support.v4.media.app.NotificationCompat.MediaStyle()
                                    .setShowActionsInCompactView(0)
                                    .setMediaSession(mediaSession.getSessionToken()))
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentText("Content Text")
                    .setContentTitle("Content Title")
                    .setChannelId(AUDIO_CHANNEL_ID);

        } else if (action.equals("Pause")) {

            return MediaStyleHelper.from(context, mediaSession)
                    .addAction(new NotificationCompat.Action(android.R.drawable.ic_media_play, "Play", MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)))
                    .setStyle(
                            new android.support.v4.media.app.NotificationCompat.MediaStyle()
                                    .setShowActionsInCompactView(0)
                                    .setMediaSession(mediaSession.getSessionToken()))
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentText("Content Text")
                    .setContentTitle("Content Title")
                    .setChannelId(AUDIO_CHANNEL_ID);

        }

        return null;

    } }
Reimer answered 18/11, 2017 at 16:13 Comment(0)
E
29

Switch from mipmap to drawable for the icon. See this issue for more.

Exarate answered 18/11, 2017 at 16:23 Comment(7)
Thankyou! This was the reason.Reimer
i change mipmap to drawable but facing same issueLunnete
mipmap worked on most Samsung devices but crashed on my HTC U11. drawable fixed this issue. @Andrea Scalabrini answer gives some more info about the issue.Maui
If we change it from mipmap to drawable then how do we put a constant resource id for notification icon drawable. Recent release in gradle ignores public.xml file. #25318159Mebane
@shivamgupta: "how do we put a constant resource id for notification icon drawable" -- use R.drawable.whatever_your_drawable_is. See the documentation.Exarate
@CommonsWare, Sorry, please let me explain the issue. If I put notification icon under drawable, then the generated resource-id can change from old build to new build, since any developer can introduce new drawables. Due to changed drawable resource id, sometimes RemoteServiceException occurs when we send app updates. Thats why I need some way to have a constant resource id for notification icon which doesn't change from one build to another.Mebane
When your app is updated, your process is terminated. Any notifications using the old resource ID already exist; any new notifications would use the new resource ID. So, I am not completely clear on how your scenario is occurring. Since what you want ("a constant resource id for notification icon which doesn't change from one build to another") does not exist, you will need to solve your problem some other way.Exarate
D
8

The issue is related to new adaptive icons in Android O.

To solve it, just replace all adaptive icons with classic icons. No matter if mipmap or drawable

Some references: Link 1 Link 2

Dominique answered 23/11, 2017 at 16:57 Comment(0)
A
5

My app was crashing when I tried to create a notification. For my case, I was using an Android Studio sample project, "Basic Activity" that contained the AndroidManifest.xml below. The mipmap/ic_launcher and mipmap/ic_launcher_round are used as app icon.

<application
    android:name=".DriveMeApp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

The project contains ic_launcher.xml and ic_launcher_round.xml adaptive icons above. I had to delete both of the files to resolve my crashing issue. After the adaptive icon files are deleted, the png files should be used as app icon.

Avoirdupois answered 7/2, 2018 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.