Bad Notification: Couldn't expand RemoteViews

2019-04-04 07:57发布

问题:

I use OneSignal SDK to show notifications. I do it in OneSignalPushService.java.

OneSignalPushService.java:

public class OneSignalPushService extends NotificationExtenderService {

    @Override
    protected boolean onNotificationProcessing(OSNotificationReceivedResult notification) {

        if (!TinyDbWrap.getInstance().isPushEnabled()) {
            KLog.d(this.getClass().getSimpleName(), "Notification will not displayed");
            return true;
        }

        OverrideSettings overrideSettings = new OverrideSettings();
        overrideSettings.extender = new NotificationCompat.Extender() {
            @Override
            public NotificationCompat.Builder extend(NotificationCompat.Builder notificationBuilder) {
                notificationBuilder.setDefaults(0);
                notificationBuilder.setContentTitle(getApplicationContext().getResources().getString(R.string.app_name));

                boolean is_in_silent_mode = false; /*or true by user's settings in app*/
                /*TinyDbWrap.getInstance()... - it stores user's settings*/
                KLog.d(OneSignalPushService.class.getSimpleName(), "Notification isSoundPushEnabled: " + TinyDbWrap.getInstance().isSoundPushEnabled());
                if (!is_in_silent_mode && TinyDbWrap.getInstance().isSoundPushEnabled()) {
                    notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
                } else {
                    notificationBuilder.setSound(null);
                }

                KLog.d(OneSignalPushService.class.getSimpleName(), "Notification isVibrationPushEnabled: " + TinyDbWrap.getInstance().isVibrationPushEnabled());
                if (!is_in_silent_mode && TinyDbWrap.getInstance().isVibrationPushEnabled()) {
                    notificationBuilder.setVibrate(new long[]{0, 100, 200, 300, 400});
                } else {
                    notificationBuilder.setVibrate(new long[]{0});
                }

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    notificationBuilder.setColor(ContextCompat.getColor(getApplicationContext(), R.color.bg_first_item_white_scheme));
                }
                notificationBuilder.setLights(ContextCompat.getColor(getApplicationContext(), R.color.time_white_sheme), 500, 500);
                return notificationBuilder;
            }
        };

        OSNotificationDisplayedResult result = displayNotification(overrideSettings);
        if (result != null) {
            KLog.d(OneSignalPushService.class.getSimpleName(), "Notification displayed with id: " + result.androidNotificationId);
        }

        return true;
    }

}

This works well on all my devices but:

I'm receiving a big number of this issue on Crashlytics only on devices with Android Nougat:

Fatal Exception: android.app.RemoteServiceException: Bad notification posted from package my.package: Couldn't expand RemoteViews for: StatusBarNotification(pkg=my.package user=UserHandle{0} id=-1542711428 tag=null key=0|my.package|-1542711428|null|10184: Notification(pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x19 color=0xff56a0d3 vis=PUBLIC semFlags=0x0 semPriority=0)) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1813) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6776) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)

Unfortunately, I can't reproduce this issue on my devices with Android Nougat to understand how I can eliminate it.

I tried to change graphics resources such as icons of notification, to clean project in order to follow this advice.

I noticed that number of devices with this issue increase for a week when I release a new version of the app later these numbers decrease to zero.

This issue also reported to Google and developers of OneSignal SDK.

I'm looking for any workarounds, any ideas or suggestions which can help eliminate this issue.

回答1:

I think this crash happens because your notification include the integer reference to the icon in the PendingIntent bundle, and that integer was later being referenced while being posted to the NotificationManager.

In between getting the integer reference and the pending intent going off, the app was updated and all of the drawable references changed. The integer that used to reference the correct drawable now referenced either the incorrect drawable or none at all (none at all - causing this crash)

This is evidenced by

I noticed that number of devices with this issue increase for a week when I release a new version of the app later these numbers decrease to zero.

As a solution you can rebuild all notifications after the application has been updated.



回答2:

Developers of OneSignal suggest to do next a work around:

Add following under the <application> tag in a AndroidManifest.xml

<manifest ...>
    <application ...>
        ...
        <receiver android:name="com.onesignal.UpgradeReceiver" tools:node="remove" />
    </application>
</manifest>

These crashes disappeared when I did this temporary solution.