首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关闭应用程序后,Xamarin.IOS UILocalNotification.UserInfo为空。

关闭应用程序后,Xamarin.IOS UILocalNotification.UserInfo为空。
EN

Stack Overflow用户
提问于 2019-03-08 11:48:55
回答 1查看 275关注 0票数 0

本地通知在打开应用程序时可以正常工作。但在一些情况下,本地通知在关闭申请后仍保留在通知中心。然后单击notification应用程序,然后在AppDelegate.FinishedLaunching方法中的选项中传递通知数据。选项包含键UIKit.UILocalNotification,其值为UIKit.UILocalNotification类型。此值包含应该用通知数据填充的UserInfo属性。但是这个UserInfo是空的。

另一个问题是,当本地通知保持在通知中心并重新启动应用程序时。单击通知会导致应用程序重新启动,并立即停止。

你有过这样的问题吗?扎马林有问题吗?如何处理这种情况?

创建通知:

代码语言:javascript
复制
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中处理通知

代码语言:javascript
复制
if (options != null)
{
    var notification = options["UIApplicationLaunchOptionsLocalNotificationKey"] as UIKit.UILocalNotification;
    var userInfo = notification.UserInfo;//the userInfo is null
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-11 08:50:05

NSDictionary似乎不应该包含NSNull值。删除NSNull值后,一切都正常,即使NSDictionary文档,https://developer.apple.com/documentation/foundation/nsdictionary说NSNull是允许的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55062654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档