你实施了吗用户通知 https://developer.apple.com/documentation/usernotifications?如果您在 iOS 10+ 上部署项目,您可以尝试在FinishedLaunching(UIApplication application, NSDictionary launchOptions)
like:
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) => {
Console.WriteLine(granted);
});
UNUserNotificationCenter.Current.Delegate = new MyNotificationCenterDelegate();
}
然后,当通知到来并且应用程序位于前台时,WillPresentNotification()
将触发,此句柄事件返回通知到来时此应用程序将响应的操作。最后就可以得到userInfo了DidReceiveNotificationResponse()
event:
public class MyNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
completionHandler();
}
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Alert);
}
}
但如果你不想实施UserNotification
或者您只需使用按键推送无声远程通知content-available
确实如此,didReceiveRemoteNotification()
即使应用程序位于前台,也会被触发。