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

ikfrs5lh  于 12个月前  发布在  iOS
关注(0)|答案(1)|浏览(124)

将我的应用程序从xamarin.forms转换为.net 7 maui。在xamarin.forms中,我覆盖了RegisteredForRemoteNotification和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) {}
}
ekqde3dh

ekqde3dh1#

尝试更改注册流程。大概是这样的:

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是来自public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)的UIApplication示例

相关问题