核心代马如下:
public static final String id = "channel_1";
public static final String name = "channel_name_1";
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
boolean highVersion = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
if (manager != null) {
manager.createNotificationChannel(channel);
}
highVersion = true;
}
NotificationCompat.Builder build = new NotificationCompat.Builder(
context);
if (highVersion) {
build = new NotificationCompat.Builder(
context, id);
}
// 设置远程试图RemoteViews对象
RemoteViews remoteViews = new RemoteViews(
context.getPackageName(), R.layout.notify_layout);
build.setContent(remoteViews);
build.setWhen(System.currentTimeMillis());
build.setTicker(msg.ticker);
build.setAutoCancel(true);
build.setSmallIcon(R.drawable.app_icons);
remoteViews.setImageViewResource(R.id.img, R.drawable.app_icons);
remoteViews.setTextViewText(R.id.title, bean.body.text.title);
remoteViews.setTextViewText(R.id.content, bean.body.text.desc);
return build.build();
layout-v21
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="64dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<!--<!– android:background="#012345" –>-->
<ImageView
android:id="@+id/img"
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="5dp"
android:scaleType="fitXY" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="4dp"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:gravity="center_vertical"
android:marqueeRepeatLimit="marquee_forever"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:singleLine="true"
android:text=""
android:textAppearance="@android:style/TextAppearance.Material.Notification.Title"
android:textSize="13dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="middle"
android:maxLines="2"
android:padding="1dp"
android:text=""
android:textAppearance="@android:style/TextAppearance.Material.Notification.Line2"
android:textSize="13dp" />
<!--style="@style/NotificationTitle"|android:textColor="@color/color_666666"-->
</LinearLayout>
</LinearLayout>
</LinearLayout>
21以下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="64dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<!--<!– android:background="#012345" –>-->
<ImageView
android:id="@+id/img"
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="5dp"
android:scaleType="fitXY" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="4dp"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:ellipsize="marquee"
android:gravity="center_vertical"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="标题"
android:textColor="#999999"
android:textSize="14sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="middle"
android:maxLines="2"
android:padding="1dp"
android:text=""
android:textColor="#999999"
android:textSize="12sp" />
<!--style="@style/NotificationTitle"|android:textColor="@color/color_666666"-->
</LinearLayout>
</LinearLayout>
</LinearLayout>
网友评论