简体   繁体   English

如何在android中创建自定义通知布局?

[英]How to create a Custom Notification Layout in android?

如何使用通知样式或需要自定义布局在android中第一次在通知中显示完整内容?

Use a custom contentView on your Notification Builder在通知生成器上使用自定义contentView

To define a custom notification layout, start by instantiating a RemoteViews object that inflates an XML layout file.要定义自定义通知布局,首先要实例化一个可扩充XML 布局文件的RemoteViews对象。 Then, instead of calling methods such as setContentTitle(), call setContent() .然后,不要调用 setContentTitle() 等方法,而是调用setContent() To set content details in the custom notification, use the methods in RemoteViews to set the values of the view's children:要在自定义通知中设置内容详细信息,请使用RemoteViews的方法来设置视图子项的值:

Create an XML layout for the notification in a separate file.在单独的文件中为通知创建 XML 布局。 You can use any file name you wish, but you must use the extension .xml In your app, use RemoteViews methods to define your notification's icons and text.您可以使用任意文件名,但必须使用扩展名 .xml 在您的应用程序中,使用RemoteViews方法来定义通知的图标和文本。 Put this RemoteViews object into your NotificationCompat.Builder by calling setContent() .通过调用setContent()将此RemoteViews对象放入NotificationCompat.Builder Avoid setting a background Drawable on your RemoteViews object, because your text color may become unreadable.避免在您的 RemoteViews 对象上设置背景 Drawable,因为您的文本颜色可能变得不可读。

custom_push.xml has my custom views R.id.image,R.id.text,R.id.title custom_push.xml 有我的自定义视图 R.id.image,R.id.text,R.id.title

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="64dp"
    android:padding="10dp" >
    <ImageView
        android:src="@mipmap/ic_launcher"
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp" />
    <TextView
        android:textSize="13dp"
        android:textColor="#000"
        android:text="Testing"
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        />
    <TextView
        android:textSize="13dp"
        android:textColor="#000"
        android:text="Testing is awecome"
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        android:layout_below="@id/title"
         />
</RelativeLayout>

Instantiating a RemoteViews object and set it,实例化一个 RemoteViews 对象并设置它,

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_push);
contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
contentView.setTextViewText(R.id.title, "Custom notification");
contentView.setTextViewText(R.id.text, "This is a custom layout");

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContent(contentView);

Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(1, notification);

在此处输入图片说明

check : https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ApplyStyle检查: https : //developer.android.com/guide/topics/ui/notifiers/notifications.html#ApplyStyle

I used BitTextStyle() to add highlighted text in notification.我使用BitTextStyle()在通知中添加突出显示的文本。

return new NotificationCompat.Builder(context)
       .setSmallIcon(R.drawable.ic_mono)
       .setContentTitle(title)
       .setContentText(message)
       .setLargeIcon(icon)
       .setColor(ContextCompat.getColor(context, R.color.notification_color))
       .setStyle(new NotificationCompat.BigTextStyle().bigText(title))
       .setStyle(new NotificationCompat.BigTextStyle().bigText(message).setSummaryText("#hashtag"))
       .setShowWhen(true)
       .setAutoCancel(true);

This code worked for me.这段代码对我有用。

    private static RemoteViews contentView;
    private static Notification notification;
    private static NotificationManager notificationManager;
    private static final int NotificationID = 1005;
    private static NotificationCompat.Builder mBuilder;

    private void RunNotification() {

        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(getApplicationContext(), "notify_001");

        contentView = new RemoteViews(getPackageName(), R.layout.my_notification_layout);
        contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
        Intent switchIntent = new Intent(this, BackgroundService.switchButtonListener.class);
        PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 1020, switchIntent, 0);
        contentView.setOnClickPendingIntent(R.id.flashButton, pendingSwitchIntent);

        mBuilder.setSmallIcon(R.mipmap.newicon);
        mBuilder.setAutoCancel(false);
        mBuilder.setOngoing(true);
        mBuilder.setPriority(Notification.PRIORITY_HIGH);
        mBuilder.setOnlyAlertOnce(true);
        mBuilder.build().flags = Notification.FLAG_NO_CLEAR | Notification.PRIORITY_HIGH;
        mBuilder.setContent(contentView);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelId = "channel_id";
            NotificationChannel channel = new NotificationChannel(channelId, "channel name", NotificationManager.IMPORTANCE_HIGH);
            channel.enableVibration(true);
            channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            notificationManager.createNotificationChannel(channel);
            mBuilder.setChannelId(channelId);
        }

        notification = mBuilder.build();
        notificationManager.notify(NotificationID, notification);
    }

this is my notification layout这是我的通知布局

<?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="50dp"
    android:background="#e9ebe9">

    <ImageView
        android:id="@+id/flashButton"
        android:layout_width="180dp"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="-20dp"
        android:src="@drawable/turnoff2" />

    <ImageView
        android:layout_width="100dp"
        android:layout_height="45dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="-10dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:src="@mipmap/newicon" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="80dp">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="Flashlight"
            android:textColor="#000000"
            android:textSize="13sp" />

        <TextView
            android:id="@+id/charging"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/title"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="3dp"
            android:text="90% Charging"
            android:textColor="#000000"
            android:textSize="13sp" />
    </RelativeLayout>

</RelativeLayout>

I hope this could help you我希望这可以帮助你

I guess what you're looking for is .setSubText() .我猜你要找的是.setSubText() The flipkart notification you have pointed out is definitely not a custom view.您指出的flipkart 通知绝对不是自定义视图。

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(icon)
                        .setSubText("Limited Stocks, Don't Wait!") <-------
                        .setContentTitle("Custom Notification Title")
notificationBuilder.notify(1, notificationBuilder.build());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM