三天,测试了好多网上的示例,均失败,最终在StackOverflow找到了能够成功运行的示例,做了精简后如下:
重点在于新增的NotificationChannel
private void btnTestNotifyAgain_Click(object sender, System.EventArgs e)
{
NotificationChannel channel = new NotificationChannel(BaseContext.PackageName, "Testing Messages", NotificationImportance.High);
channel.EnableVibration(true);
channel.EnableLights(true);
channel.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification), new AudioAttributes.Builder().SetUsage(AudioUsageKind.Notification).Build());
channel.LockscreenVisibility = NotificationVisibility.Public;
NotificationManager notificationManager = NotificationManager.FromContext(BaseContext);
notificationManager.CreateNotificationChannel(channel);
Notification.Builder builder = new Notification.Builder(BaseContext, BaseContext.PackageName);
builder.SetContentTitle("标题");
builder.SetContentText("StackOverflow例子改写,忽略判断版本.");
builder.SetAutoCancel(true);
builder.SetSmallIcon(Resource.Drawable.ic_little_notification);
builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.monkey_icon));
//在通知中增加图像
Notification.BigPictureStyle picStyle = new Notification.BigPictureStyle();
picStyle.BigPicture(BitmapFactory.DecodeResource(Resources, Resource.Drawable.x_bldg));
builder.SetStyle(picStyle);
Notification notification = builder.Build();
notificationManager.Notify(1331, notification);
}