但是我想在屏幕上显示通知 像这样:
这是我的本地通知代码:
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)
{
}
}
那么如何在屏幕顶部显示通知? 可以在屏幕上显示通知吗?
答案 0 :(得分:0)
您应更改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
注意:
API级别26不推荐使用Notification.PRIORITY_HIGH
和Notification.PRIORITY_MAX
。请改用NotificationCompat
。
有关更多信息,请阅读Heads-up Notifications。