【问题标题】:Xamarin IOS - UserInfo custom data dictionary is always emptyXamarin IOS - UserInfo 自定义数据字典始终为空
【发布时间】:2020-07-23 10:40:51
【问题描述】:

下面的方法展示了我如何创建通知。我将一个新项目附加到 UserInfo 字典中。

private UNNotificationRequest CreateNotification(Geofence geofence)
{
        var content = new UNMutableNotificationContent();
        content.Title = "title";
        content.Subtitle = "subtitle";
        content.Body = "This is the message body of the notification.";
        content.Badge = 2;

        content.UserInfo.Append(new KeyValuePair<NSObject, NSObject>((NSString)"id", (NSString)"bla"));

        var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);

        var request = UNNotificationRequest.FromIdentifier("test1", content, trigger);
        return request;
}

问题如下:

public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
    foreach (var item in response.Notification.Request.Content.UserInfo)
    {
    }

    // Inform caller it has been handled
    completionHandler();
}

UserInfo 字典始终为空。为什么会这样?我该如何解决?

【问题讨论】:

    标签: xamarin.ios localnotification userinfo


    【解决方案1】:

    你需要得到switch语句中的逻辑

    public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
        {   
            // Take action based on Action ID
            switch (response.ActionIdentifier)
            {
                case "reply":
                    // Do something
                    break;
                default:
                    // Take action based on identifier
                    if (response.IsDefaultAction)
                    {
                        // Handle default action...
                        var item = response.Notification.Request.Content.UserInfo;
                    }
                    else if (response.IsDismissAction)
                    {
                        // Handle dismiss action
                    }
                    break;
            }
    
            // Inform caller it has been handled
            completionHandler();
        }
    

    更新

    您似乎以错误的方式添加 UserInfo 。检查以下代码

    content.UserInfo = NSDictionary.FromObjectAndKey(new KeyValuePair<NSObject, NSObject>((NSString)"id", (NSString)"bla"));
    

    【讨论】:

    • 是的,但这不会改变字典为空的事实。还是会?如果是这样,为什么..
    猜你喜欢
    • 1970-01-01
    • 2018-12-10
    • 2020-06-04
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    相关资源
    最近更新 更多