我想要推送通知的自定义UI。为此我使用RemoteViews。 用于创建通知的代码:
RemoteViews notificationView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);
RemoteViews expandedNotificationView = new RemoteViews(context.getPackageName(), R.layout.expanded_custom_notification);
notificationView.setTextViewText(R.id.notification_title, messagesToBeShown);
expandedNotificationView.setTextViewText(R.id.expanded_notification_title, "Message Received in expanded ");
expandedNotificationView.setTextViewText(R.id.expanded_notification_message, messagesToBeShown);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setAutoCancel(true);
builder.setContentIntent(pendingIntent);
builder.setPriority(Notification.PRIORITY_MAX);
Notification notification = builder.build();
notification.contentView = notificationView;
// Notification notification = new NotificationCompat.Builder(context).setSmallIcon(R.mipmap.ic_launcher)
// .setContentIntent(pendingIntent).setContent(notificationView)
// .setAutoCancel(true).build();
if (Build.VERSION.SDK_INT >= 16) {
// Inflate and set the layout for the expanded notification view
notification.bigContentView = expandedNotificationView;
}
notification.flags |= Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(R.string.notification_number, notification);
自定义布局的XMl文件:
expanded_custom_notification.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/expanded_notifiation_image"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="80dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/expanded_notification_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000" />
<TextView
android:id="@+id/expanded_notification_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000" />
</LinearLayout>
custom_notification.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100">
<ImageView
android:id="@+id/notifiation_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/notification_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="40"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#fff" />
<Button
android:id="@+id/mark_task_complete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="40"
android:text="mark as complete" />
</LinearLayout>
此代码的问题是:
请帮帮我。提前谢谢。
答案 0 :(得分:0)
尝试在 builder.build()
之前添加这些行builder.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())