【问题标题】:How to handle user clicks local notification on Xamarin iOS when the user turns off app?用户关闭应用程序时如何处理用户在 Xamarin iOS 上单击本地通知?
【发布时间】:2020-01-25 20:17:10
【问题描述】:

我的应用收到通知并显示本地User Notification

当用户关闭此应用并点击此本地通知时。

我想处理打开这个本地通知的事件。

我尝试了以下链接:

Handle local notification tap Xamarin Forms iOS

但是方法 ReceivedLocalNotification() 似乎不起作用(我尝试显示警报,但没有显示警报。)

我尝试在方法 DidReceiveRemoteNotification()

中显示警报

类似于Xamarin.iOS - Red & Green Notifications 示例,但我无法处理“应用程序关闭时用户单击本地通知”事件。

这是我的代码:

        [Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
    public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, System.Action completionHandler)
    {
        // Show Alert 
        var alert = new UIAlertView()
        {
            Title = "LocalNotification",
            Message = $"Content: Method DidReceiveNotificationResponse()"
        };
        alert.AddButton("OK");
        alert.Show();

        if (response.IsDefaultAction)
        {
            Console.WriteLine("ACTION: Default");
        }
        if (response.IsDismissAction)
        {
            Console.WriteLine("ACTION: Dismiss");
        }
        else
        {
            Console.WriteLine($"ACTION: {response.ActionIdentifier}");
        }

        completionHandler();
    }

请帮帮我!

如何处理“应用关闭时用户点击本地通知”事件? 我正在 ios 12.4 和 ios 13.0 上进行测试。

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.ios


    【解决方案1】:

    您必须在 FinishedLaunching 覆盖内的 AppDelegate.cs 内部检查传递到应用程序的选项。它将包含推送通知信息。

    UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();
    
    if (options != null)
    {
        if (options.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey))
        {
            NSDictionary dic = options[UIApplication.LaunchOptionsRemoteNotificationKey] as NSDictionary;
    
            // decide what to do with push info here
        }
    
        Console.WriteLine($"Startup: {nameof(AppDelegate.FinishedLaunching)} : {nameof(options)} {options}");
    }
    

    如果您使用的是 ios 10+,您可能还想查看 UNUserNotificationCenterDelegate 以更好地处理您的推送通知。 https://docs.microsoft.com/en-us/xamarin/ios/platform/user-notifications/enhanced-user-notifications?tabs=macos#handling-foreground-app-notifications

    【讨论】:

    • 我阅读了文档docs.microsoft.com/en-us/xamarin/ios/platform/…,我知道您的代码会在应用程序处于活动状态时监视通知。但我的情况是当用户关闭应用程序并在应用程序关闭时打开通知。
    • @HuuBaoNguyen 如果您查看代码,您必须在 FinishedLaunching 中的 OPTIONS 中获取键 UIApplication.LaunchOptionsRemoteNotificationKey。当应用程序关闭时,它会被填充。
    • 它适用于选项options.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 2019-02-19
    • 2016-12-20
    • 2021-01-27
    • 2017-07-17
    • 2014-03-05
    相关资源
    最近更新 更多