2016-08-08 3 views
11

Die Anforderung war, eine benutzerdefinierte Benachrichtigung vorzunehmen, da die Standardbenachrichtigung des Android nur ein Bild zulässt. Also ging ich durch, wie man eine benutzerdefinierte Benutzeroberfläche für die Benachrichtigung anfügt, wenn sie erweitert wurde. Die fertige Antwort bestand darin, eine benutzerdefinierte Ansicht zu erstellen und an den Benachrichtigungsmanager zu übergeben und von der API-Stufe 16 zuzulassen. Also habe ich einen und hier meinen Layout-XML-Code Dateiname: notification_custom_view_new:GCM Push-Benachrichtigung. Ungültige Benachrichtigung gepostet - RemoteViews konnte nicht erweitert werden für: 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> 

Die Art und Weise verwiesen ich es im Code:

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

Zuordnung zu Custombenachrichtigungsmanager:

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

Aber das gab mir Fehler, schlechte Benachrichtigung zu sagen, ich habe mehr als 15 gleiche Frage hier auf stackoverflow ohne eine richtige Antwort identifiziert .. Einige schlagen vor, eine Ressource fehlt und damit der Fehler. Ich bin sicher, von meinem Ende keine der Werte sind Null, noch Ressourcen fehlen Fehler sind nicht in meiner Remote-Ansicht, die übergeben wird.

Jede Hilfe wird geschätzt und auch ich habe versucht, für ein paar Tage den Fehler zu verfolgen, aber nichts Gutes scheint zu passieren. Bitte hilf mir!

Mitteilung Obj Init:

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

Stacktrace:

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) 

Anbringen Screenshot des Layouts UI aussehen:

layout UI

+0

Kommentieren Sie 'View' von xml und versuchen Sie dann, Ihre App auszuführen –

+0

Geben Sie einen Code ein. Wie initialisierst du ein Benachrichtigungsobjekt? –

+0

@BurhanuddinRashid Aber ich brauche die Schattenansicht .. – DJphy

Antwort

1

Entfernen Sie die Ansicht, die Sie als Schatten in Ihrem Layout verwenden (oder ersetzen Sie die Ansicht mit TextView) und es funktioniert gut. XML, das für RemoteView verwendet wird, sollte Ansichten verwenden, die @android.widget.RemoteViews.RemoteView Annotation hat. Da eine einfache Ansicht diese Annotation nicht enthält, können Sie diese in Ihrem XML nicht verwenden.

0

Sie können keine EditText in eine RemoteViews setzen. RemoteViews ist auf eine Handvoll möglicher Widgets beschränkt, für eine Notification bezweifle ich, dass die AdapterView-Optionen, wie ListView, ebenfalls funktionieren. have a look here. Hoffe, das wird dir nach meiner Erfahrung helfen.

+0

Hey @sanat chandravanshi Ich kenne dich, deine von LocalQueen, ich wurde von Ihnen einmal interviewt .. Okay, das könnte war ein Kommentar .. Nun, bitte gehen Sie durch meine Layout-XML-Datei, es hat keine solchen Widgets – DJphy

Verwandte Themen