2016-10-26 3 views
1

Ich versuche, eine benutzerdefinierte Benachrichtigung LayoutAndroid Benutzerdefinierte Push-Benachrichtigung zeigt leere Text und Titel

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout); 
     contentView.setImageViewResource(R.id.image_customNote,R.mipmap.ic_notification); 
     contentView.setTextViewText(R.id.title_customNote,title); 
     contentView.setTextViewText(R.id.text_customNote, messageBody); 


     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setPriority(Notification.PRIORITY_HIGH) 
       .setAutoCancel(true) 
       //.setContentText(title) 
       // .setContentText(messageBody) 
       .setTicker(title) 
       .setSound(defaultSoundUri) 
       /* .setStyle(new NotificationCompat.BigTextStyle().bigText(title)) 
       .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))*/ 
       .setCustomContentView(contentView) 
       .setCustomBigContentView(contentView) 
       .setContentIntent(pendingIntent) 
       .setSmallIcon(getNotificationIcon()).setColor(ContextCompat.getColor(this, R.color.colorPrimary)); 
      // .addAction(R.drawable.ic_launcher, "Previous", pendingIntent) 
      // .setColor(ContextCompat.getColor(this, R.color.colorPrimary)); 

     notificationManager.notify(new Random().nextInt(), notificationBuilder.build()); 

Dies ist der Code den ich versuche,

Hier eingestellt ist meine XML-Datei

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:id="@+id/image_customNote" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_marginRight="10dp" /> 

    <TextView 
     android:id="@+id/title_customNote" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@android:color/black" 
     android:layout_toRightOf="@id/image_customNote" 

     /> 

    <TextView 
     android:id="@+id/text_customNote" 
     android:textColor="@android:color/black" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/title_customNote" 
     android:layout_toRightOf="@id/image_customNote" /> 
</RelativeLayout> 

Wenn ich eine Benachrichtigung bekomme, wird die benutzerdefinierte Ansicht nicht eingestellt und die Benachrichtigungsansicht ist komplett leer ... bitte helfe

+0

nur einen Versuch. Können Sie die übergeordnete Höhe und Breite des Layouts in match_parent statt in wrap_content ändern? –

+0

versucht ..Stil das gleiche, wird das benutzerdefinierte Layout nicht eingestellt –

Antwort

1

Das Problem in Ihrem xml ist.

  1. Sie haben keine ID von 'title'. Sie müssen Ihre zweite textView auf Android: layout_below = "@ id/title_customNote"
  2. Entfernen Sie den Stil - und legen Sie Textfarbe auf Ihre Textansichten - der Text ist eingestellt, aber die Farbe ist transparent.

Diese xml für mich gearbeitet:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:id="@+id/image_customNote" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_marginRight="10dp" /> 

    <TextView 
     android:id="@+id/title_customNote" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@android:color/holo_blue_dark" 
     android:layout_toRightOf="@id/image_customNote" /> 

    <TextView 
     android:id="@+id/text_customNote" 
     android:textColor="@android:color/holo_blue_dark" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/title_customNote" 
     android:layout_toRightOf="@id/image_customNote" /> 
</RelativeLayout> 

enter image description here

+0

Hallo, ich habe versucht, die XML-Datei zu ändern .. aber das Problem besteht weiterhin, das Bild ist vor, aber kein Titel und Text. Was könnte der Grund dafür sein? –

+0

hast du den Stil sicher entfernt? und setze textColor? – Dus

+0

Ich habe die XML-Datei editiert, die ich verwende, bitte überprüfen Sie es –

0

Dies ist Ihr bearbeitetes Code-Snippet, das ich versucht habe und eine Benachrichtigung erscheint.

 int icon = R.drawable.icon; 
     NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

     RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout); 
     contentView.setImageViewResource(R.id.image_customNote, R.mipmap.app_icon); 
     contentView.setTextViewText(R.id.title_customNote, "Title"); 
     contentView.setTextViewText(R.id.text_customNote, "Custom"); 

     Intent notificationIntent = new Intent(this, MainActivity.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setPriority(Notification.PRIORITY_HIGH) 
       //.setContentTitle(title) 
       //.setStyle(new NotificationCompat.BigTextStyle().bigText(title)) 
       //.setContentText(messageBody).setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody)) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(contentIntent) 
       .setContent(contentView) 
       .setColor(ContextCompat.getColor(this, R.color.colorPrimary)) 
       //.setContentTitle(title).setContentText(messageBody) 
       .setSmallIcon(icon); 

     mNotificationManager.notify(new Random().nextInt(), notificationBuilder.build()); 

korrigiert auch das Layout wie unten (2. Textview)

<TextView android:id="@+id/text_customNote" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_toRightOf="@id/image_customNote" 
       android:layout_below="@id/title_customNote" 
       style="Custom Notification Text" /> 
+0

Benachrichtigung wird angezeigt, aber der Titel und Text wird nicht festgelegt. –

Verwandte Themen