从包中发布错误通知,无法展开RemoteViews

时间:2014-08-23 08:23:24

标签: java android notifications push-notification

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)
当app没有运行时,当我发送推送通知时,任何人都可以帮我解决这个问题。请任何人帮忙...... 此问题的代码是按要求的。还想了解远程视图的详细概念。我试图从开发者网站研究它。但不能在那边更好地解释。还请粘贴一些有用的链接。

代码如下:

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);

1 个答案:

答案 0 :(得分:0)

尝试使用Notification.Builder而不是Notification。例如:

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

对于待定意图,请尝试以下方法:

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