本地通知在打开应用程序时可以正常工作。但在一些情况下,本地通知在关闭申请后仍保留在通知中心。然后单击notification应用程序,然后在AppDelegate.FinishedLaunching方法中的选项中传递通知数据。选项包含键UIKit.UILocalNotification,其值为UIKit.UILocalNotification类型。此值包含应该用通知数据填充的UserInfo属性。但是这个UserInfo是空的。
另一个问题是,当本地通知保持在通知中心并重新启动应用程序时。单击通知会导致应用程序重新启动,并立即停止。
你有过这样的问题吗?扎马林有问题吗?如何处理这种情况?
创建通知:
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
}发布于 2019-03-11 08:50:05
NSDictionary似乎不应该包含NSNull值。删除NSNull值后,一切都正常,即使NSDictionary文档,https://developer.apple.com/documentation/foundation/nsdictionary说NSNull是允许的。
https://stackoverflow.com/questions/55062654
复制相似问题