我的应用程序收到通知并显示本地 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 上进行测试。
最佳答案
您必须检查 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/