2016-08-08 151 views
11

由於Android的默認通知僅允許圖片,因此需要製作自定義通知。因此,我在擴展時如何爲通知添加自定義UI,現成的答案是創建自定義視圖並傳遞給通知管理器,並允許來自API級別16.因此,我做了一個,然後在這裏執行我的佈局xml代碼 - 文件名:notification_custom_view_new:GCM推送通知。發佈錯誤通知 - 無法展開RemoteViews:StatusBarNotification

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

分配customview到通知管理器:

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

但是,這給了我錯誤說不好的通知,我已經確定了超過15 +相同的問題在這裏沒有一個正確的答案stackoverflow ..有人建議它是一個資源缺失,因此錯誤。我肯定從我的結束沒有任何值是空的,也沒有資源缺失錯誤不存在於我的遠程視圖中傳遞。

任何幫助將不勝感激,而且我已經嘗試了幾天來追蹤錯誤,但似乎沒有什麼好事發生。請幫幫我!

通知的OBJ初始化:

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外觀的截圖:

layout UI

+0

評論來自xml的'View'並嘗試運行你的應用程序 –

+0

發表一些代碼。如何初始化通知對象? –

+0

@BurhanuddinRashid但我需要陰影視圖.. – DJphy

回答

1

刪除在視圖中使用的「視圖」(或將視圖替換爲TextView),並且它工作正常。用於Remoteview的XML應該使用具有@android.widget.RemoteViews.RemoteView註釋的Views。由於一個普通的View沒有這個註解,所以你不能在你的xml中使用它。

0

你不能把一個EditText到RemoteViews。 RemoteViews僅限於少數可能的小部件,對於通知,我懷疑AdapterView選項(如ListView)也可以使用。 have a look here。希望這將幫助你按照我的經驗。

+0

嘿@sanat chandravanshi我知道你,你從LocalQueen,我曾接受過你的採訪。好吧,這可能會已經評論..好吧,請通過我的佈局XML文件,它沒有這樣的小部件 – DJphy