如何创建像Android的通知一样的短命令

Nik*_*hil 2 java android push-notification android-notifications android-studio

如何为andriod创建推送通知等方法.通知包括通知底部带有文本的完整图像.

小通知只是文本,但是当你展开它时,它会在背景中显示图像,底部是文本.

有人可以帮我这个.以下是截图

在此输入图像描述

Sur*_*ija 7

您需要将自定义远程视图充气为通知布局,如下所示:

layout_custom_notification.xml

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

    <ImageView
        android:id="@+id/image_news"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@mipmap/dine_main_item_bg1" />

    <TextView
        android:id="@+id/tv_news_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        android:layout_toLeftOf="@+id/tv_news_time"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:text="Trump signs order to roll back Obama-era climate policies"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/tv_news_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="20dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:text="9:14 AM"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginTop="20dp"
        android:padding="8dp"
        android:text="Share"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

此后,将此布局设置为通知中的内容视图,如下所示:

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.layout_custom_notification);
        contentView.setImageViewResource(R.id.image_news, R.drawable.ic_launcher);
        contentView.setTextViewText(R.id.tv_news_time, "9:14 AM");
        contentView.setTextViewText(R.id.tv_news_title, "Trump signs order to roll back Obama-era climate policies");

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

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(icon)
            .setContent(contentView)
            .setContentTitle("Custom Notification")
            .setContentIntent(contentIntent)   
            .setWhen(when);

...
mNotificationManager.notify(1, notificationBuilder.build());
Run Code Online (Sandbox Code Playgroud)