1. ホーム
  2. android

[解決済み] 修正方法:android.app.RemoteServiceException: パッケージから投稿された不正な通知 *: アイコンを作成できませんでした。StatusBarIcon

2022-05-10 23:42:43

質問

クラッシュログに以下のような例外が表示されるのですが。

android.app.RemoteServiceException: Bad notification posted from package com.my.package: Couldn't create icon: StatusBarIcon(pkg=com.my.package user=0 id=0x7f02015d level=0 visible=true num=0 )
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1456)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5487)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
    at dalvik.system.NativeStart.main(Native Method)

AlarmManagerで設定したPendingIntentからIntentServiceでNotificationを投稿するには、以下のメソッドを使用します。 ここで渡される値はすべて、PendingIntent / IntentServiceのバンドルエクストラからです。

/**
 * Notification 
 *
 * @param c
 * @param intent
 * @param notificationId
 * @param title
 * @param message
 * @param largeIcon
 * @param smallIcon
 */
public static void showNotification(Context c, Intent intent,
        int notificationId, String title, String message, int largeIcon,
        int smallIcon) {
    PendingIntent detailsIntent = PendingIntent.getActivity(c,
            notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    // BUILD
    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
            c);
    // TITLE
    mNotifyBuilder.setContentTitle(title).setContentText(message);

    // ICONS
    mNotifyBuilder.setSmallIcon(smallIcon);
    if (Util.isAndroidOSAtLeast(Build.VERSION_CODES.HONEYCOMB)) {
        Bitmap large_icon_bmp = ((BitmapDrawable) c.getResources()
                .getDrawable(largeIcon)).getBitmap();
        mNotifyBuilder.setLargeIcon(large_icon_bmp);
    }

    mNotifyBuilder.setContentIntent(detailsIntent);
    mNotifyBuilder.setVibrate(new long[] { 500, 1500 });
    mNotifyBuilder.setTicker(message);
    mNotifyBuilder.setContentText(message);

    // NOTIFY
    NotificationManager nm = (NotificationManager) c
            .getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(notificationId, mNotifyBuilder.build());
}

他の方の回答を拝見したところ、私が見ている例外は、以下の場合に発生するようです。 setSmallIcon() が正しく呼び出されていない。

渡されるResource IDがすべて正しいことを確認し、ダブルチェックしました。

解決方法は?

PendingIntent バンドルにアイコンへの整数リファレンスを含め、その整数が後で NotificationManager にポストされる際に参照される、というものでした。

整数値の参照を取得してから保留中のインテントが消えるまでの間に、アプリが更新され、すべての描画可能な参照先が変更されました。 以前は正しい drawable を参照していた整数が、現在は正しくない drawable を参照しているか、まったく参照していません (まったく参照していない - このクラッシュの原因)。