繁体   English   中英

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

[英]How to Show the Notification Popup on Over The screen in Xamarin Forms Android?

我的本地通知仅显示状态栏这是示例: 在此处输入图片说明

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

在此处输入图片说明

这是我的本地通知代码:

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_HIGHNotificationCompat.PRIORITY_MAX

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

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

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

注意:在API级别26中不建议使用Notification.PRIORITY_HIGHNotification.PRIORITY_MAX 。请改用NotificationCompat

有关更多信息,请阅读提示通知

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM