14

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.

Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
  • when you are sending the one signal notification have u pass the Big Picture parameter? – Amjad Khan Oct 24 '17 at 12:28
  • https://stackoverflow.com/questions/25317659/how-to-fix-android-app-remoteserviceexception-bad-notification-posted-from-pac – Rajesh Oct 24 '17 at 12:30
  • @AmjadKhan no, you can see all parameters here - https://drive.google.com/file/d/0B-D8XWFJqEwxYW9OVzd6Snp3d0U/view?usp=sharing – Dima Kozhevin Oct 24 '17 at 12:35
  • @RajeshKushvaha I think your link isn't useful because my issue happens only on `Android Nougat` but not on all `Android Nougat`. Also, I checked all answers(as I think) on SO before I asked my question – Dima Kozhevin Oct 24 '17 at 12:38
  • After sending the notification have you updated the apk to play store or issues raise due to Message Title = null – Amjad Khan Oct 24 '17 at 12:59
  • @AmjadKhan The issue raises after update and after few days it tends to zero. And it is repeated every time after the update on small number of devices. – Dima Kozhevin Oct 24 '17 at 13:02
  • Yes I got the same issue it is because if you send the notification and after that if you have update the application and user who have downloaded will get this error in Nougat and upper version – Amjad Khan Oct 24 '17 at 13:04
  • @Dima kozhevin Did the problem solved or exist? – Amjad Khan Oct 26 '17 at 09:46
  • @AmjadKhan No, as you can see in that issue https://github.com/OneSignal/OneSignal-Android-SDK/issues/263 it's still alive and the problem doesn't solved, moreover the dev guys tried to solve it. I'm already beginning to think that it's easier to catch this exception, as a temporary solution of the problem, although this is not a very engineering approach. Thank you for your participation. – Dima Kozhevin Oct 26 '17 at 10:56
  • So, did you found any solution to that? – Ian Medeiros Jul 08 '18 at 01:30
  • @IanMedeiros This [work around](https://stackoverflow.com/a/50561772/3166697) helps me – Dima Kozhevin Jul 08 '18 at 07:21

2 Answers2

12

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.

Dmitriy Puchkov
  • 1,530
  • 17
  • 41
  • Suppose your decision was correct. How then can you explain that it does not happen at all of the `Android-Nougat` devices? And, I can be wrong, but the re-creation of notifications occurs after the update but it doesn't help. You can see it here - https://github.com/OneSignal/OneSignal-Android-SDK/commit/3211ee299514f35b6f277b65874b9871d05aa033#diff-625070073d150a81fe409d56e0ef98f2R42 – Dima Kozhevin Oct 31 '17 at 07:48
  • @DimaKozhevin Google developers make some changes in the Notifications on Android N https://android-developers.googleblog.com/2016/06/notifications-in-android-n.html Perhaps this affected the correction of this bug. About re-creation of notifications actually I'm not sure that the receiver works, it would be nice to check it out. – Dmitriy Puchkov Oct 31 '17 at 08:08
  • Google developers make some changes all times. It's a very general statement. Could you suggest a solution of this problem? Any way I upvoted your answer. Thanks for your attention to my question. – Dima Kozhevin Oct 31 '17 at 08:17
  • Thank you man. I can only advise to wrap it in try-catch block. – Dmitriy Puchkov Oct 31 '17 at 08:33
  • 1
    @DmitriyPuchkov I have the same issue without the one signal sdk, and this issue occurs to me, the issue that results in this exception is recreation of a persistent notification. Unfortunately this cannot be wrapped with a try catch, the flow is entirely through internal classes – Kirill Kulakov Nov 06 '17 at 07:54
  • I have the same issue and I rebuild all my notifications after 'MY_PACKAGE_REPLACED'. And this doesn`t help. I still get the crashes after the update, from Motorola devices on Nougat Android version. – kolombo Nov 20 '17 at 11:11
  • @KirillKulakov @kolombo Are you are able to reproduce the issue at all? This is key as I believe there will be some logcat entires before the ending `Couldn't expand RemoteViews for:` crash. – jkasten Dec 01 '17 at 05:32
  • @jkasten unfortunately I don't I was only able to see the crash in fabric – Kirill Kulakov Dec 03 '17 at 07:33
0

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.

Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
  • this solution only for Android Nougat. i got this crash but the suspect is Kitkat (4.4.2) almost all kitkat got impact – Nanda Z Jun 12 '19 at 06:38