在适用于 IOS 的 .Net MAUI 应用程序中请求通知权限

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

我手头有其他来源的代码,可以使用 Plugin.LocalNotification 发送本地通知。好东西。该代码适用于 Windows。它似乎不适用于 IOS(使用热重启)。

我发现其他消息来源表明有必要请求通知权限。好的

var notificationsEnabled = await LocalNotificationCenter.Current.AreNotificationsEnabled();

这表明我的应用程序中未启用通知。

那么如何请求通知权限?

ios .net notifications maui uilocalnotification
1个回答
0
投票

这适用于我的热重启部署的 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();
}
© www.soinside.com 2019 - 2024. All rights reserved.