GCM推送通知。错误的通知发布 - 无法为StatusBarNotification扩展RemoteViews。

11

要求制作自定义通知,因为Android默认的通知只允许添加图片。所以我查找了如何在展开时附加自定义UI的方法,答案是创建一个自定义视图并传递给通知管理器,并且只能从API 16开始使用。因此我创建了一个自定义视图,以下是我的布局XML代码 - 文件名:notification_custom_view_new:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/GhostWhite">

<RelativeLayout
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="65dp">

    <ImageView
        android:id="@+id/big_icon"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="12dp"
        android:scaleType="fitCenter" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/big_icon"
        android:layout_alignTop="@+id/big_icon"
        android:layout_toRightOf="@+id/big_icon"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:weightSum="2">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.6"
            android:orientation="vertical">

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:maxLines="1"
                android:text="Title"
                android:textColor="@color/colorBlackDimText"
                android:textSize="16sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:maxLines="2"
                android:paddingTop="2dp"
                android:text="message"
                android:textColor="@color/colorBlackDimText"
                android:textSize="14sp" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.4"
            android:gravity="center">

            <TextView
                android:id="@+id/time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:maxLines="2"
                android:padding="8dp"
                android:text="24:59"
                android:textColor="@color/colorBlackDimText"
                android:textSize="12sp" />

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

<View
    android:id="@+id/shadow"
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:layout_below="@+id/header"
    android:background="@drawable/shadow" />

<ImageView
    android:id="@+id/big_picture"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/header"
    android:src="@drawable/vector_icon_collections"
    android:minHeight="240dp"
    android:scaleType="fitCenter" />
</RelativeLayout>

我在代码中引用它的方式是:
private RemoteViews assignRemote(Bitmap bitmap, String title, String message){
    RemoteViews expandedView = new RemoteViews(Application.getInstance().getPackageName(),
            R.layout.notification_custom_view_new);
    expandedView.setTextViewText(R.id.title, title);
    expandedView.setTextViewText(R.id.message, message);
    expandedView.setImageViewBitmap(R.id.big_picture, bitmap);
    expandedView.setImageViewResource(R.id.big_icon, R.mipmap.ic_launcher);
    expandedView.setTextViewText(R.id.time, getOnlyHrsMin());
    return expandedView;
}

将自定义视图分配给通知管理器:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        notification.bigContentView = assignRemote(bitmap, title, message);
    }

但是这让我出现了错误,显示不良通知,我在stackoverflow上找到了15多个相同的问题,没有正确的答案...有人建议它是资源缺失,因此出现了错误。我确定我的值都不为空,也没有远程视图中的资源缺失错误。
非常感谢您的帮助!我已经尝试了几天来跟踪错误,但好像没有什么进展。
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
    bigPictureStyle.setBigContentTitle(title);
    bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
    bigPictureStyle.bigPicture(bitmap);
    Notification notification;
    notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentIntent(resultPendingIntent)
            .setSound(alarmSound)
            .setLights(Color.WHITE, 1500, 2000)
            //.setStyle(bigPictureStyle)
            .setWhen(getTimeMilliSec(timeStamp))
            .setSmallIcon(iconToUse())
            .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
            .setContentText(message)
            .build();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        notification.bigContentView = assignRemote(bitmap, title, message);
    }

    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(Config.NOTIFICATION_ID_BIG_IMAGE, notification);

堆栈跟踪:

 android.app.RemoteServiceException: Bad notification posted from package com.goldadorn.main: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.goldadorn.main user=UserHandle{0} id=101 tag=null score=0 key=0|com.goldadorn.main|101|null|10220: Notification(pri=0 contentView=com.goldadorn.main/0x1090077 vibrate=null sound=android.resource://com.goldadorn.main/raw/notification defaults=0x0 flags=0x11 color=0x00000000 vis=PRIVATE))
                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1480)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                  at android.os.Looper.loop(Looper.java:135)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                  at java.lang.reflect.Method.invoke(Method.java:372)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

附上布局UI外观的截图:

布局UI


发布一些代码。你如何初始化通知对象? - Pravin Divraniya
@BurhanuddinRashid 但我需要阴影视图。 - DJphy
@Pravin Divraniya 是的,我会立即发布。 - DJphy
请粘贴完整的错误日志。 - Vikas B L
@user1728071 好的,我会做到。 - DJphy
显示剩余2条评论
2个回答

2

移除您在布局中使用的“View”作为阴影(或将其替换为TextView),它就可以正常工作了。用于Remoteview的XML应该使用具有@android.widget.RemoteViews.RemoteView注解的视图。由于普通视图没有此注解,因此您不能在xml中使用它。


0

你不能将EditText放入RemoteViews中。RemoteViews仅限于少数可能的小部件,对于通知,我怀疑AdapterView选项(如ListView)也将无法使用。在这里看看。希望根据我的经验这可以帮助到你。


嘿,@sanat chandravanshi,我认识你,你来自LocalQueen,我曾经被你面试过。好的,这本来可以是一条评论...好吧,请查看我的布局XML文件,它没有这样的小部件。 - DJphy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接