c# - Xamarin.iOS 在应用未关闭时处理推送通知

标签 c# push-notification xamarin.ios

如果应用程序在后台使用方法 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/

相关文章:

c# - 反序列化 json 抛出 'Additional text encountered' 错误

ios - 在 iOS 11+ (Swift) 中从推送通知操作按钮打开应用程序?

xamarin.ios - 无法使用 Xamarin 和 MvvmCross 从 XIB UI 创建 View

android - 当应用程序处于后台时,FCM Intent 与数据交付在哪里?

firebase - 如何根据用户属性、用户受众发送 Firebase Cloud Messaging 通知

xamarin - 我需要阻止 XamarinForms 上日期选择器中的粘贴功能

c# - 如何使用 Xamarin 应用程序开发自动注销

c# - 使用默认链接的 web api 版本控制

C# TCPClient/Socket 编写不抛出异常

c# - 如何释放为 Excel 创建的对象 (C#)