How can I display Local Notification (Plugin.LocalNotification) in MAUI for iPhone

868 Views Asked by At

I am using MAUI and trying to display the local notification on iPhone, but it doesn't show notification neither it throws any error. However, the same code is working fine on Android. I have already added UseLocalNotification() in MauiProgram.cs

//Send Push Notication
var request = new NotificationRequest
{ 
    Image = new NotificationImage { ResourceName = "icon.png", FilePath = "/Assets/icon.png" },
    NotificationId = 1000,
    Title =  "Changed",
    Subtitle = "Tab to see..",
    Description = $"updated",
    BadgeNumber = 50, //42

    Schedule = new NotificationRequestSchedule
    {
        NotifyTime = DateTime.Now.AddSeconds(1),
        // NotifyRepeatInterval = TimeSpan.FromDays(1),
    }
};
await LocalNotificationCenter.Current.Show(request);
1

There are 1 best solutions below

3
Peter Wessberg On

Here is code for local notification on iPhone. But you need permission to do this from the user. Asking permission to use notifications

using UserNotifications;

var content = new UNMutableNotificationContent
{
    Title = "Warning! ",
    Body = "This is an alert!",
    CategoryIdentifier = "WARNING_ALERT",
    Sound = UNNotificationSound.DefaultCriticalSound
};

var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(5, false);

var request = UNNotificationRequest.FromIdentifier("ALERT_REQUEST", content, trigger);

UNUserNotificationCenter.Current.AddNotificationRequest(request, (error) =>
{
    if (error != null)
    {
        Console.WriteLine("Error: " + error);
    }
});