- 我在提醒申请中实施了通知。
- 由于我需要自定义通知,因此我使用了RemoteView并为其创建布局。
- 在布局中,我使用了两个textview并设置了内容。
- 此处显示通知,但此处未显示另一个文本视图。
自定义通知代码
private void sendCustomNotification() {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, AlertActivity.class);
notificationIntent.putExtra("reminder", reminderModel);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
RemoteViews notificationView = new RemoteViews(
DateReceiverService.this.getPackageName(),
R.layout.app_shortcut_activity
);
notificationView.setImageViewResource(
R.id.imgAppIcon,
R.mipmap.ic_launcher);
notificationView.setTextViewText(R.id.txtReminder, "Hiii");//First Textview
notificationView.setTextViewText(R.id.txtStartReminder, "Good");//Second Textview
NotificationCompat.Builder builder = new NotificationCompat.Builder(DateReceiverService.this)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("HIiiiiiii")
.setAutoCancel(false)
.setContentIntent(contentIntent)
.setContent(notificationView);
Notification notification = builder.build();
notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
notification.defaults |= Notification.DEFAULT_SOUND; // Sound
notification.flags = Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(notificationId, notification);
}
app_shortcut_activity.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="wrap_content"
android:background="#8e8989"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="@+id/imgAppIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#8e8989"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/txtReminder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="@+id/txtStartReminder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</LinearLayout>
<ImageView
android:id="@+id/imgAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:src="@drawable/icon_add" />
<ImageView
android:id="@+id/imgSetting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:src="@drawable/icon_setting_white" />
</LinearLayout>
通知
的屏幕截图
Custom Notification