如何在Xamarin Forms Android中在屏幕上显示通知弹出窗口?

如何在Xamarin Forms Android中在屏幕上显示通知弹出窗口?,xamarin,xamarin.forms,xamarin.android,Xamarin,Xamarin.forms,Xamarin.android,我的本地通知仅显示状态栏 以下是一个例子: 但是我想在屏幕上显示通知 像这样: 以下是我的本地通知代码: public void GetLocalNotification(string title, string message) { try { NotificationManager notificationManager = Xamarin.Forms.Forms.Context.GetSys

我的本地通知仅显示状态栏 以下是一个例子:

但是我想在屏幕上显示通知 像这样:

以下是我的本地通知代码:

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)
            {

            }
        }
那么如何在屏幕顶部显示通知呢?
是否可以在屏幕上显示通知?

您应该更改
通知
优先级或
通知频道
重要性

通知优先级,由
SetPriority()
设置。优先级决定了通知在Android 7.1及更低版本上的侵入性。(对于Android 8.0及更高版本,您必须设置频道重要性)

在Android 7.1(API级别25)及更低版本上:

将通知优先级设置为
NotificationCompat.priority\u HIGH
NotificationCompat.priority\u MAX

设置铃声和振动-您可以使用设置默认值(Notification.DEFAULT\u ALL)

Android 8.0(API级别26)及更高版本:

将通知通道优先级设置为
NotificationManager.IMPORTANCE\u HIGH

注意:
Notification.PRIORITY\u HIGH
Notification.PRIORITY\u MAX
在API级别26中被弃用。请改用
NotificationCompat


有关更多信息,请阅读。

您想先显示通知,然后再将其放入托盘中吗?是的。先显示,然后发送。您可以参考以下内容:查看Abby wang的评论,它有您的答案!在更改优先级后,您可能需要卸载/重新安装应用程序才能使其生效。