展示
源码
var btn01 = (Button)FindViewById(Resource.Id.btn_01);
var btn02 = (Button)FindViewById(Resource.Id.btn_02);
var largeBitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.avatar);
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
if (btn01 != null)
btn01.Click += (sender, args) =>
{
Notification notification;
const string id = "ChannelId";
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
// id: 每个包必须是唯一的,太长,可能会被截断
// name: 用户可见名称,最大建议长度为40个字符,太长可能会被截断
// importance: 重要性
var notificationChannel = new NotificationChannel(id, "name", Notificationimportance.High);
notificationManager?.CreateNotificationChannel(notificationChannel);
notification = new Notification.Builder(this)
.SetChannelId(id)
.SetContentTitle("SetContentTitle")
.SetContentText("SetContentText")
.SetSubText("SetSubText")
.SetTicker("SetTicker")
.SetWhen(JavaSystem.CurrentTimeMillis()) // 设置通知时间
.SetSmallIcon(Resource.Drawable.small_icon) // 小图标
.SetLargeIcon(largeBitmap) // 大图标
.SetAutoCancel(false) // 用户点击Notification点击面板后是否让通知取消(默认不取消)
.SetDefaults(NotificationDefaults.All)
?.Build();
}
else
{
notification = new NotificationCompat.Builder(this)
.SetContentTitle("SetContentTitle")
.SetContentText("SetContentText")
.SetSubText("SetSubText")
.SetTicker("SetTicker")
.SetWhen(JavaSystem.CurrentTimeMillis()) // 设置通知时间
.SetSmallIcon(Resource.Drawable.small_icon) // 小图标
.SetLargeIcon(largeBitmap) // 大图标
.SetAutoCancel(false) // 用户点击Notification点击面板后是否让通知取消(默认不取消)
.SetDefaults((int)NotificationDefaults.Sound)
.Build();
}
notificationManager?.Notify(1, notification);
};
if (btn02 != null)
btn02.Click += (sender, args) => notificationManager?.Cancel(1);