但我想在屏幕上显示通知像这样:
这是我的本地通知代码:
public void GetLocalNotification(string title, string message)
{
try
{
NotificationManager notificationManager = Xamarin.Forms.Forms.Context.GetSystemService(Context.NotificationService) as NotificationManager;
Notification notification = new Notification();
int notificationId;
if (Build.VERSION.SdkInt >= Build.VERSION_CODES.O)
{
String CHANNEL_ID = "com.ShipActivePOS.android";
string CharSequence = "MyChannel";
String Description = "This is my channel";
NotificationImportance importance = NotificationManager.ImportanceHigh;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, CharSequence, importance);
mChannel.Description = message;
mChannel.EnableLights(true);
mChannel.EnableVibration(true);
mChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 });
mChannel.SetShowBadge(false);
// notificationManager.CreateNotificationChannel(mChannel);
notificationId = 1100;
// string URGENT_CHANNEL = "com.ShipActivePOS.android.urgent";
// Instantiate the builder and set notification elements:
NotificationCompat.Builder builder = new NotificationCompat.Builder(global::Android.App.Application.Context, CHANNEL_ID)
.SetContentTitle("Sales123")
.SetContentText(message)
.SetChannelId(CHANNEL_ID)
.SetSmallIcon(Resource.Drawable.AppLogo);
// Build the notification:
Notification notificationO = builder.Build();
// Get the notification manager:
// Publish the notification:
notificationManager.Notify(notificationId, notificationO);
}
else
{
Notification.Builder builder = new Notification.Builder(global::Android.App.Application.Context)
.SetContentTitle(title)
.SetContentText(message)
.SetDefaults(NotificationDefaults.Sound)
.SetSmallIcon(Resource.Drawable.AppLogo);
builder.SetDefaults(NotificationDefaults.Sound | NotificationDefaults.Vibrate);
notification = builder.Build();
notificationId = 0;
}
// Publish the notification:
notificationManager.Notify(notificationId, notification);
}
catch (Exception ex)
{
}
}
那么如何在屏幕顶部显示通知?是否可以在屏幕上显示通知?
您应该更改Notification
优先级或NotificationChannel
重要性。
通知优先级,由SetPriority()
设置。优先级决定了Android 7.1及更低版本通知的侵入程度。 (对于Android 8.0及更高版本,您必须改为设置频道重要性)
在Android 7.1(API级别25)及更低版本:
将通知优先级设置为NotificationCompat.PRIORITY_HIGH
或NotificationCompat.PRIORITY_MAX
。
设置铃声和振动 - 您可以使用SetDefaults(Notification.DEFAULT_ALL)
Android 8.0(API级别26)及更高版本:
将通知通道优先级设置为NotificationManager.IMPORTANCE_HIGH
注意:Notification.PRIORITY_HIGH
和Notification.PRIORITY_MAX
在API级别26中被弃用。请使用NotificationCompat
。
欲了解更多信息,请阅读Heads-up Notifications。