xamarin - 当用户关闭应用程序时,如何处理 Xamarin iOS 上的用户点击本地通知?

标签 xamarin xamarin.forms xamarin.ios

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

local notification click_open_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 上进行测试。

最佳答案

您必须检查 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://learn.microsoft.com/en-us/xamarin/ios/platform/user-notifications/enhanced-user-notifications?tabs=macos#handling-foreground-app-notifications

关于xamarin - 当用户关闭应用程序时,如何处理 Xamarin iOS 上的用户点击本地通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58101769/

相关文章:

c# - Monotouch 和 WCF : difference of SVCUTIL. EXE 和 SLSVCUTIL.EXE 以及如何避免不受支持的通用 ChannelFactory?

iphone - 异步更新数据后重绘 UITableView

xamarin - 有关如何从 Xamarin Forms 应用程序获取用户评论的任何建议。可能是使用 appcenter.ms 的东西?

mvvm - 嵌套 View 模型的嵌套 View

Xamarin Forms - 隐藏 MasterDetailPage 中的导航栏

c# - Xamarin Forms 导航问题

ios - 适用于 iOS 的 Xamarin.Forms Firebase Analytics

ios - Xamarin 警告 : The app icon set "AppIcons" has 2 unassigned children

c# - MVVMCROSS - 将参数传递给 ViewModel

VS2019找不到Android模拟器