如何在 iPhone 的 MAUI 中显示本地通知(Plugin.LocalNotification)

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

我正在使用 MAUI 并尝试在 iPhone 上显示本地通知,但它不显示通知,也不会引发任何错误。然而,相同的代码在 Android 上运行良好。我已经在 MauiProgram.cs 中添加了 UseLocalNotification()

                //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);
ios maui localnotification
1个回答
0
投票

这里是 iPhone 上本地通知的代码。但您需要获得用户的许可才能执行此操作。 请求使用通知的许可

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);
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.