简体   繁体   中英

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

My local notification shows only status bar Here is the example: 在此处输入图片说明

But I want to show the notification Over the screen Like This:

在此处输入图片说明

Here is my code for Local Notification:

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

            }
        }

So how to show the notification top of the screen? Is it possible to show the notification over the screen?

You should change Notification priority or NotificationChannel importance.

The notification priority, set by SetPriority() . The priority determines how intrusive the notification should be on Android 7.1 and lower. (For Android 8.0 and higher, you must instead set the channel importance)

On Android 7.1 (API level 25) and lower:

Set notification priority to NotificationCompat.PRIORITY_HIGH or NotificationCompat.PRIORITY_MAX .

Set ringtone and vibrations - you can use SetDefaults(Notification.DEFAULT_ALL)

Android 8.0 (API level 26) and higher:

Set notification channel priority to NotificationManager.IMPORTANCE_HIGH

Note: Notification.PRIORITY_HIGH and Notification.PRIORITY_MAX was deprecated in API level 26. use NotificationCompat instead.

For more information please read Heads-up Notifications .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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