Xamarin.iOS 在应用程序未关闭时处理推送通知 [英] Xamarin.iOS handle push notification when app is not closed

查看:34
本文介绍了Xamarin.iOS 在应用程序未关闭时处理推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果应用程序在后台使用 didReceiveRemoteNotification 方法,我设法处理推送通知.如果应用程序在前台并且没有关闭,有没有办法处理推送通知?谢谢

I managed to handle push notification if the app is in the background using the method didReceiveRemoteNotification. Is there a way to handle the push notification if the app is in the foreground and not closed ? Thanks

推荐答案

您是否实施了 UserNotification?如果您在 iOS 10+ 上部署您的项目,您可以尝试订阅 FinishedLaunching(UIApplication application, NSDictionary launchOptions) 中的通知,例如:

Have you implemented UserNotification? If you deploy your project on iOS 10+ you can try to subscribe the notification in the 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() 将触发,此句柄事件返回当通知到来时此应用程序将响应哪个操作.最后你可以在 DidReceiveNotificationResponse() 事件中获取 userInfo:

Then when the notification comes and the application is in the foreground, the WillPresentNotification() will fire, this handle event returns which action will this app response when notification comes. At last you can get the userInfo in the 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() 即使应用程序在前台也会被触发.

But if you don't want to implement UserNotification or you just push a silent remote notification with the key content-available true, the didReceiveRemoteNotification() will also be fired even though the app is in foreground.

这篇关于Xamarin.iOS 在应用程序未关闭时处理推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆