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

问题描述 投票:0回答:1

我的本地通知仅显示状态栏以下是示例:enter image description here

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

enter image description here

这是我的本地通知代码:

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

            }
        }

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

xamarin xamarin.forms xamarin.android
1个回答
0
投票

您应该更改Notification优先级或NotificationChannel重要性。

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

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

将通知优先级设置为NotificationCompat.PRIORITY_HIGHNotificationCompat.PRIORITY_MAX

设置铃声和振动 - 您可以使用SetDefaults(Notification.DEFAULT_ALL)

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

将通知通道优先级设置为NotificationManager.IMPORTANCE_HIGH

注意:Notification.PRIORITY_HIGHNotification.PRIORITY_MAX在API级别26中被弃用。请使用NotificationCompat

欲了解更多信息,请阅读Heads-up Notifications

© www.soinside.com 2019 - 2024. All rights reserved.