Cordova-plugin-local-notifications: Fatal Exception: android.app.RemoteServiceException Bad notification posted from package:* Couldn't create icon: StatusBarIcon

Created on 12 Feb 2020  路  8Comments  路  Source: katzer/cordova-plugin-local-notifications

My app is crashing when showing a local notification on Samsung devices using Android 8.1.

I tested using an emulator on Android 8.1 and everything is fine, I'm guessing this is a problem with Samsung, anybody knows what can be done to solve this problem?

Your Environment

  • Plugin version: 0.9.0-beta.2
  • Platform: Android
  • OS version: 8.1
  • Device manufacturer / model: Samsung Galaxy J7 Prime
  • Cordova version (cordova -v): 9.0.1
  • Cordova platform version (cordova platform ls): 8.1.0
  • Plugin config: "cordova-plugin-local-notification": {}
  • Ionic Version (if using Ionic): @ionic-native/core: 5.18.1-beta.5

Expected Behavior

Show a notification

Actual Behavior

App crashes

Steps to Reproduce

Send a local notification, app crashes immediately

Context

Send a local notification

Debug logs

_Include iOS / Android logs_

This is what showed in Firebase Crashlytics stacktrace:

Fatal Exception: android.app.RemoteServiceException
Bad notification posted from package: ** Couldn't create icon: StatusBarIcon(icon=Icon(typ=RESOURCE pkg=** id=0x01020006) visible user=0 )
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1906)
android.os.Handler.dispatchMessage (Handler.java:106)
android.os.Looper.loop (Looper.java:164)
android.app.ActivityThread.main (ActivityThread.java:7000)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:441)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1408)

Most helpful comment

@GitFr33 It was: res://mipmap-xxxhdpi/ic_stat_zin.png

Where ic_stat_zin.png was a icon that has transparency and is pure white. The name of the file may differ.
If you do not make an icon that is pure white it will not work, as there are configurations in android to color the icon in xml. Or in the case of localnotification this is done through a function call if I'm not mistaken.

This is also the reason why the notification icon generator in android asset studio only generates white icons.
https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type=clipart&source.clipart=ac_unit&source.space.trim=1&source.space.pad=0&name=ic_stat_ac_unit
(I hope this doesn't sound racist, it's just how google made it work for notifications)

The location of where to put this icon is in: /android/app/src/main/res/mipmap-xxxhdpi/ this can be any of the mipmap directories, I went for the biggest one because it would guarantee quality on all screens. I think res:// points to /android/app/src/main/res/ but I haven't tested whether it'll work with custom directories yet.

Since there was no documentation I kind of figured this out on my own through some logical reasoning.
I basically looked at how native apps do it. And also the word res (which I assume is resource) gave it away.

All 8 comments

@katzer any idea why is this happening?

Hi,

I'm currently having the same issue, have you resolved this already somehow?
Apparently capacitor's local notifications work, but when I try to use this plugin I'm greeted with the same error message as you do above.

The thing is capacitor's local notifications doesn't have a badge option, which I need to use.

Hi,

I'm currently having the same issue, have you resolved this already somehow?
Apparently capacitor's local notifications work, but when I try to use this plugin I'm greeted with the same error message as you do above.

The thing is capacitor's local notifications doesn't have a badge option, which I need to use.

Yes. I manually fixed the plugin.

I'm going to copy my fix here but be aware that:

I didn't fork this project so I changed the files inside the Android's platform folder to be easier to test my modifications.
If you do the same remember to backup the modified files before reinstalling this plugin again (if you ever do) because you gonna lose all changes made.

platform/android/app/src/main/java/de/appplant/cordova/plugin/localnotification/LocalNotification.java

// EXTRA IMPORTS

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import android.util.Log;

// NEW METHOD

private void createNotificationChannel() {
        Log.d("notification", "Called notification creation.");
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Log.d("notification", "Called notification creation.");
            CharSequence channelName = "Default Notifications";
            String channelId = "default_notifications";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            String description = "Notifica莽玫es gerais do app.";

            NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            Manager notificationManager = getNotMgr();
            notificationManager.createNotificationChannel(channel);
        }
    }


// CALL NEW METHOD UNDER "schedule" FUNCTION

private void schedule (JSONArray toasts, CallbackContext command) {
        createNotificationChannel(); // <--- HERE
        Manager mgr = getNotMgr();

        for (int i = 0; i < toasts.length(); i++) {
            JSONObject dict    = toasts.optJSONObject(i);
            Options options    = new Options(dict);
            Request request    = new Request(options);
            Notification toast = mgr.schedule(request, TriggerReceiver.class);

            if (toast != null) {
                fireEvent("add", toast);
            }
        }

        check(command);
    }

platform/android/app/src/main/java/de/appplant/cordova/plugin/notification/Manager.java

// EXTRA IMPORT

import android.os.Build;
import androidx.core.app.NotificationManagerCompat;
import static androidx.core.app.NotificationManagerCompat.IMPORTANCE_DEFAULT;

// NEW METHOD

public void createNotificationChannel(NotificationChannel channel){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager mgr = getNotMgr();
            mgr.createNotificationChannel(channel);
        }
    }

What I did to fix the issue was set a correct smallIcon in the local notification properties.
Apparently the default icon for the local notification causes issues.

EDIT: Presumably because it is not white?

I'm running into the same problem. What did you set the smallIcon to that fixed it @Simbaclaws? I tried setting it to false and to res://calendar as it shows in the read me but I'm still getting this error.

@GitFr33 It was: res://mipmap-xxxhdpi/ic_stat_zin.png

Where ic_stat_zin.png was a icon that has transparency and is pure white. The name of the file may differ.
If you do not make an icon that is pure white it will not work, as there are configurations in android to color the icon in xml. Or in the case of localnotification this is done through a function call if I'm not mistaken.

This is also the reason why the notification icon generator in android asset studio only generates white icons.
https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type=clipart&source.clipart=ac_unit&source.space.trim=1&source.space.pad=0&name=ic_stat_ac_unit
(I hope this doesn't sound racist, it's just how google made it work for notifications)

The location of where to put this icon is in: /android/app/src/main/res/mipmap-xxxhdpi/ this can be any of the mipmap directories, I went for the biggest one because it would guarantee quality on all screens. I think res:// points to /android/app/src/main/res/ but I haven't tested whether it'll work with custom directories yet.

Since there was no documentation I kind of figured this out on my own through some logical reasoning.
I basically looked at how native apps do it. And also the word res (which I assume is resource) gave it away.

Excellent! Thank you @Simbaclaws, that fixed it for me.

You're welcome :+1:
Maybe someone can be kind enough to close this issue?

Was this page helpful?
0 / 5 - 0 ratings