How can I get Notification Heads-Up working with SetPriority?

ChaseFlorellChaseFlorell CAInsider, University mod

I've got a notification in my Android app (this app is part of XamForms).
I can get notifications working, however, I cannot get the notification to show up as a Heads-Up notification.

The two issues I'm having are as follows.

  1. The SetPriority() method in the Navigation.Builder takes an int instead of NotificationPriority.High enum
  2. Even setting the int to 2 which is MAX as per the documentation, the notification shows up in the shade, but does not display a HUD.

    using Android.App;
    using Android.Content;
    using Android.Content.Res;
    using Android.Graphics;
    using Android.Support.V4.App;
    using Java.Lang;
    using Notification = TimeTracker.Notifications.Notification;

    namespace TimeTracker.Droid.Notifications
    {
    [BroadcastReceiver]
    public class LocalNotificationReceiver : BroadcastReceiver
    {
    private static readonly Resources Resources = Application.Context.Resources;

        public override void OnReceive(Context context, Intent intent)
        {
            var notificationId = intent.GetIntExtra("id",-1); // default value is -1 because it should never associate with an enum.
    
            var notification = new Notification
            {
                NotificationId = notificationId
            };
    
            notification.Build(); 
            if (notification.ShouldNotify())
            {
                var iconColor = Resources.GetColor(Resource.Color.notification_icon_color);
                var notIntent = new Intent(context, typeof(MainActivity));
                var contentIntent = PendingIntent.GetActivity(context, notificationId, notIntent, PendingIntentFlags.CancelCurrent);
                var manager = NotificationManager.FromContext(context);
    
                var style = new Android.App.Notification.BigTextStyle();
                style.BigText(notification.Message);
    
                const int resourceId = Resource.Drawable.ic_launcher;
                var wearableExtender = new Android.App.Notification.WearableExtender()
                    .SetBackground(BitmapFactory.DecodeResource(Resources, resourceId));
    
                //Generate a notification
                var nativeNotificationBuilder = new Android.App.Notification.Builder(context)
                    .SetPriority(2)
                    .SetContentIntent(contentIntent)
                    .SetSmallIcon(Resource.Drawable.smallNotification)
                    .SetColor(iconColor)
                    .SetContentTitle(notification.Title)
                    .SetContentText(notification.Message)
                    .SetStyle(style)
                    .SetWhen(JavaSystem.CurrentTimeMillis())
                    .SetAutoCancel(true)
                    .Extend(wearableExtender);
    
                manager.Notify(notificationId, nativeNotificationBuilder.Build());
            }
    
            notification.ScheduleNextNotification();
        }
    }
    

    }

Best Answer

Answers

  • ChaseFlorellChaseFlorell CAInsider, University mod

    whoops, I thought I indented that enough, didn't double check until now.. .can't edit.

  • ChaseFlorellChaseFlorell CAInsider, University mod

    bump

  • ChaseFlorellChaseFlorell CAInsider, University mod

    oh interesting @cheesebaron, I must have missed that piece. Thanks.

  • batmacibatmaci DEMember ✭✭✭✭✭
    edited December 2018

    this answer doesnt work for oreo now. i have sound and vibration enabled and priority and visibility set to 1 but still not appearing on top. any idea?

    i tried to add on channel but no luck

      NotificationChannel channel = new NotificationChannel(
                                    channelId,
                                   new Java.Lang.String("Channel"),
                                    NotificationImportance.High
                                );
    
  • batmacibatmaci DEMember ✭✭✭✭✭
    edited December 2018

    code above actually works. just debugging requires app to be uninstalled and installed again otherwise changed not reflected. somebody wrote here on comments under the accepted answer

Sign In or Register to comment.