我手头有其他来源的代码,可以使用 Plugin.LocalNotification 发送本地通知。好东西。该代码适用于 Windows。它似乎不适用于 IOS(使用热重启)。
我发现其他消息来源表明有必要请求通知权限。好的
var notificationsEnabled = await LocalNotificationCenter.Current.AreNotificationsEnabled();
这表明我的应用程序中未启用通知。
那么如何请求通知权限?
这适用于我的热重启部署的 IOS 应用程序:在 Platforms/iOS/AppDelegate.cs 中,使用源自 Dima Ordenov 的 MauiSamples
的以下代码using UIKit;
using UserNotifications;
...
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
RegisterForRemoteNotifications(application);
return base.FinishedLaunching(application, launchOptions);
}
/// <summary>
/// Reqeust permission for notifications
/// </summary>
/// <param name="application"></param>
/// <remarks>from https://github.com/DimaOrdenov/MauiSamples/blob/main/MauiPushNotifications/Platforms/iOS/AppDelegate.cs</remarks>
private void RegisterForRemoteNotifications(UIApplication application)
{
UNUserNotificationCenter.Current.RequestAuthorization(
UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
(isSuccess, error) =>
{
if (error != null)
{
// no op, not going to do anything about this for now
}
});
application.RegisterForRemoteNotifications();
}