Java – Error notification published from package, unable to expand RemoteViews

Error notification published from package, unable to expand RemoteViews… here is a solution to the problem.

Error notification published from package, unable to expand RemoteViews

08-23 13:42:57.649: E/AndroidRuntime(3836): FATAL EXCEPTION: main
08-23 13:42:57.649: E/AndroidRuntime(3836): android.app.RemoteServiceException: Bad notification posted from package com.test.nursery.rhymes.stories: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.test.nursery.rhymes.stories id=16104475 tag=null score=0 notn=Notification(pri=0 contentView=com.test.nursery.rhymes.stories/0x7f030012 vibrate=null sound=null defaults=0x4 flags=0x10 kind=[null]))
08-23 13:42:57.649: E/AndroidRuntime(3836):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1471)
08-23 13:42:57.649: E/AndroidRuntime(3836):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 13:42:57.649: E/AndroidRuntime(3836):     at android.os.Looper.loop(Looper.java:153)
08-23 13:42:57.649: E/AndroidRuntime(3836):     at android.app.ActivityThread.main(ActivityThread.java:5000)
08-23 13:42:57.649: E/AndroidRuntime(3836):     at java.lang.reflect.Method.invokeNative(Native Method)
08-23 13:42:57.649: E/AndroidRuntime(3836):     at java.lang.reflect.Method.invoke(Method.java:511)
08-23 13:42:57.649: E/AndroidRuntime(3836):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
08-23 13:42:57.649: E/AndroidRuntime(3836):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
08-23 13:42:57.649: E/AndroidRuntime(3836):     at dalvik.system.NativeStart.main(Native Method)

When I send push notifications when the app is not running, can anyone help me with such a problem. Ask anyone to help…
The code for this issue is provided as requested. You also want to learn more about remote views. I tried to research it from the developer site. But it can’t be better explained there. Please also paste some useful links for this.

The code is as follows:

long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(res_icon, message, when);

Intent notificationIntent = new Intent(context, FisrtActivityClass);
        notificationIntent.putExtra("message", message);

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(context,
                notificationid, notificationIntent, 0);
        MyLog.d(TAG, custom_layout_id + "; text");
        try {
             create custom notification view
            MyLog.d(TAG, "layout = " + custom_layout_id + "; image = "
                    + custom_image_id + "; text = " + custom_text_id);
             RemoteViews contentView = new
             RemoteViews(context.getApplicationContext().getPackageName(),
             custom_layout_id);
             contentView.setImageViewResource(custom_image_id, res_icon);
             contentView.setTextViewText(custom_text_id, message);
             notification.contentView = contentView;
             notification.contentIntent = contentIntent;

notification.setLatestEventInfo(context, title, message,
             contentIntent);
        } catch (Exception e) {
             TODO: handle exception
            e.printStackTrace();
             notification.setLatestEventInfo(context, title, message,
             contentIntent);
        }

notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        notificationManager.notify(notificationid, notification);

Solution

Try using Notification.Builder instead of Notification. For example:

Notification.Builder builder = new Notification.Builder(context)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContent(remoteViews);

For pending intent, try this:

Notification notification = builder.build();
notification.contentView.setOnClickPendingIntent(R.id.button, pendinglIntent);

Related Problems and Solutions