打开应用程序时,本地通知应能正常工作。但是有些情况下,关闭申请后本地通知仍保留在通知中心中。然后单击通知将启动应用程序,并且通知数据应在 AppDelegate.FinishedLaunching 方法中的选项中传递。选项包含键 UIApplicationLaunchOptionsLocalNotificationKey ,其值类型为 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.FinishedLaunching中处理通知
if (options != null)
{
var notification = options["UIApplicationLaunchOptionsLocalNotificationKey"] as UIKit.UILocalNotification;
var userInfo = notification.UserInfo;//the userInfo is null
}
答案 0 :(得分:1)
似乎NSDictionary不应包含NSNull值。删除NSNull值后,即使NSDictionary文档https://developer.apple.com/documentation/foundation/nsdictionary说允许使用NSNull,一切正常。