如果应用程序在后台使用方法 didReceiveRemoteNotification
,我设法处理推送通知。如果应用程序在前台且未关闭,有没有办法处理推送通知?谢谢
最佳答案
你实现了吗UserNotification ?如果您在 iOS 10+ 上部署项目,您可以尝试在 FinishedLaunching(UIApplication application, NSDictionary launchOptions)
中订阅通知,例如:
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()
将触发,此处理事件返回通知到来时此应用程序将响应的操作。最后你可以在 DidReceiveNotificationResponse()
事件中获取 userInfo:
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
true 推送静默远程通知,则 didReceiveRemoteNotification()
即使应用程序在前台也会被触发。
关于c# - Xamarin.iOS 在应用未关闭时处理推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50684355/