android5.0 uses Notification to report RemoteServiceException solution

Sometimes using Notification under android5.0 will report the following error information (for example, when restarting the system, a notification will be sent)
android.app.RemoteServiceException: Bad notification posted from package *: Couldn't create icon: StatusBarIcon

This problem is mostly concentrated in setSmallIcon( R.drawable.scanner_small), in some cases (such as sending a notification when restarting the system), the resource R.drawable.scanner_small is not ready, resulting in an App exception. then what should we do?

This is a bug of android5.0, which is normal in android4.4 and 6.0. In general, there is no way to solve this, as foreigners said
http://stackoverflow.com/questions/25317659/how-to-fix- android-app-remoteserviceexception-bad-notification-posted-from-pac

However, if you don't mind the icon size, you can write:
setSmallIcon(context.getApplicationInfo().icon)
Get the application icon from ApplicationInfo as SmallIcon.
In short, it is necessary to get the icon before the system restarts, so as not to get an empty resId.

Solutions:
http://stackoverflow.com/questions/24968556/how-to-fix-this-bug-android-app-remoteserviceexception-bad-notification-post

attach my complete Notification code
private static final int NOTIFY_ID = 0;
	public static void showCustomNotification(Context context) {
		NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
		Intent intent = new Intent(context, MainActivity.class);
//		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// FLAG_ONE_SHOT
		intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
		PendingIntent contentIntent = PendingIntent.getActivity(context, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
		NotificationCompat.Builder mBuilder = new Builder(context);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.custom_notify);
        
        boolean isScan=(Boolean)SPUtils.get(context, App.KEY_SCAN, true);
        remoteViews.setTextViewText(R.id.btn_scan,isScan?"Hide scan key": "Show scan key");
        //click event handler
        Intent actionIntent = new Intent(App.ACTION_NOTIFICATION);
        actionIntent.putExtra(App.KEY_NOTIFICATION_CLICK, isScan);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.btn_scan, pendingIntent);
        mBuilder.setContent(remoteViews)
                .setContentIntent(contentIntent)
                .setTicker("Scan Wizard")
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setSmallIcon(context.getApplicationInfo().icon)//采用quick fallback image
				.setDefaults(Notification.DEFAULT_ALL);
        
        Notification notify = mBuilder.build();
        notify.flags = Notification.FLAG_NO_CLEAR;//|Notification.FLAG_ONGOING_EVENT;
        notificationManager.notify(NOTIFY_ID, notify);
	}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326606562&siteId=291194637