.net 7 maui ios - 在前台时无法获取推送通知

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

将我的应用程序从 xamarin.forms 转换为 .net 7 maui。 在 xamarin.forms 中,我重写了 RegisteredForRemoteNotifications 和 ReceivedRemoteNotification 方法。 我无法在毛伊岛工作类似的代码。 当应用程序未运行时我可以看到通知。运行时什么也没有发生。

public class UserNotificaitonCenterDelegate : UNUserNotificationCenterDelegate
{
    public UserNotificaitonCenterDelegate() { }
    public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
        if (UIDevice.CurrentDevice.CheckSystemVersion(14, 0))
            completionHandler(UNNotificationPresentationOptions.Banner);
        else
            completionHandler(UNNotificationPresentationOptions.Alert);
    }
}

Xamarin.forms:

[Register("AppDelegate")]
public class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    private void RegisterForRemoteNotifications()
    {
        UIApplication.SharedApplication.RegisterForRemoteNotifications;
        UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();
    }

    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) {}
    public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo) {}
}

毛伊岛:

[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
    private void RegisterForRemoteNotifications()
    {           
        UIApplication.SharedApplication.RegisterForRemoteNotifications();
        UNUserNotificationCenter.Current.Delegate = new UserNotificaitonCenterDelegate();
    }
            
    // WORKS! App successfully gets deviceToken
    [Export("application:didRegisterForRemoteNotificationsWithDeviceToken:")]
    public void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) {} 
            
    // Doesn't work
    [Export("application:didReceiveRemoteNotification:")]
    public void ReceivedRemoteNotification(UIApplication application, NSDictionary notification) {} 
}

还尝试使用 IUNUserNotificationCenterDelegate 但没有运气:

[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate, IUNUserNotificationCenterDelegate
{
    [Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
    public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler) {}

    [Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
    public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler) {}
}
ios push-notification maui .net-7.0
1个回答
0
投票

尝试改变注册流程。像这样的东西:

UNUserNotificationCenter.Current.Delegate = this;

UNUserNotificationCenter.Current.RequestAuthorization(
    UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
    (isSuccess, error) =>
    {
        _logger.LogDebug($"Registration is success: {isSuccess}");

        if (error != null)
        {
            _logger.LogDebug($"Registration for push notifications error: {error.DebugDescription}");
        }
    });

application.RegisterForRemoteNotifications();

application
是 UIApplication 实例
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)

© www.soinside.com 2019 - 2024. All rights reserved.