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

时间:2017-01-27 06:34:32

标签: android push-notification

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

4 个答案:

答案 0 :(得分:64)

在Notification Builder

上使用自定义contentView
  

要定义自定义通知布局,请先实例化a   扩展XML布局文件的 RemoteViews 对象。然后,而不是   调用setContentTitle(),调用 setContent()等方法。设置   自定义通知中的内容详细信息,请使用方法   RemoteViews设置视图子项的值:

     

在单独的文件中为通知创建XML布局。您   可以使用您希望的任何文件名,但您必须使用扩展名.xml       在您的应用中,使用RemoteViews方法定义通知的图标和文字。将此RemoteViews对象放入您的   NotificationCompat.Builder致电 setContent()。避免设置   background在您的RemoteViews对象上绘制,因为您的文本   颜色可能变得难以理解。

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>

实例化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);

enter image description here

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

答案 1 :(得分:5)

我使用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);

答案 2 :(得分:3)

我猜你要找的是.setSubText()。 您指出的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());

答案 3 :(得分:2)

这段代码对我有用。

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

这是我的通知布局

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

我希望这可以帮助您