Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android自定义推送通知,显示空白文本和标题_Android_Notifications - Fatal编程技术网

Android自定义推送通知,显示空白文本和标题

Android自定义推送通知,显示空白文本和标题,android,notifications,Android,Notifications,我正在尝试设置自定义通知布局 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

我正在尝试设置自定义通知布局

 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());
这是我正在尝试的代码

这是我的xml文件

   <?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>


当我收到通知时,不会设置自定义视图,并且通知视图完全为空。。。请帮助

这是您编辑的代码片段,我已尝试过,并显示通知

        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());
还更正了布局如下(第二个文本视图)


问题出在xml中

  • 您没有“标题”的id。您需要将第二个文本视图设置为android:layout_down=“@id/title_customNote”
  • 删除样式-并将文本颜色设置为文本视图-文本已设置,但颜色是透明的
  • 这个xml对我有用:

    <?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>
    
    
    

    试试看。是否可以更改版面的父级高度和宽度以匹配父级而不是换行内容?已尝试..尽管如此,自定义版面尚未设置,通知正在显示,但标题和文本尚未设置。您好,我尝试更改xml文件。。但问题依然存在,图像看起来很漂亮,但没有标题和文本。这可能是什么原因?你确定要删除样式吗?设置textColor?我已经编辑了我正在使用的xml文件,请检查它是否正确。尝试删除setColor(ContextCompat.getColor(this,R.color.colorPrimary))最终使其正常工作。。谢谢你的帮助。。只有图标显示为白色背景。。现在正在努力。
    <?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>