将我的应用程序从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) {}
}
1条答案
按热度按时间ekqde3dh1#
尝试更改注册流程。大概是这样的:
application
是来自public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
的UIApplication示例