当应用程序打开时应尽其所能。但是在某些情况下,关闭申请后,本地通知仍在通知中心中。然后单击通知开始应用程序,并应在 appdelegate.finedlaunching 方法的选项中传递通知数据。选项包含键 uiapplicationlaunchoptionslocalnotificationkeykey ,其具有 uikit.uilocalnotification类型的值。此值包含应填充通知数据的UserInfo属性。但是此UserInfo为null。
另一个问题是何时在通知中心中保留本地通知并重新启动应用程序。单击通知引起应用程序重新启动并立即停止。
您有这样的问题吗?Xamarin有问题吗?如何处理这种情况?
创建通知:
public void DisplayNotification(MessageInfo info)
{
var notificationCenter = UNUserNotificationCenter.Current;
var content = new UNMutableNotificationContent();
content.Title = info.Title;
content.Body = info.Body;
content.UserInfo = IosStaticMethods.CreateNsDictFromMessageInfo(info);
UNNotificationTrigger trigger;
trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(0.1, false);
var id = (++_LastNotificationId).ToString();
var request = UNNotificationRequest.FromIdentifier(id, content, trigger);
notificationCenter.Delegate = new UserNotificationCenterDelegate();
notificationCenter.AddNotificationRequest(request, (error) =>
{
//handle error
});
}
internal class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
completionHandler(UNNotificationPresentationOptions.Alert);
}
public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
//do something
}
}
在AppDelegate.finedlaunching中处理通知
if (options != null)
{
var notification = options["UIApplicationLaunchOptionsLocalNotificationKey"] as UIKit.UILocalNotification;
var userInfo = notification.UserInfo;//the userInfo is null
}
似乎Nsdictionary不应包含nsnull值。删除NSNULL值后,即使Nsdictionary文档https://developer.apple.com/documentation/foundation/foundation/nsdictionary表示允许NSNULL。