【发布时间】:2017-06-16 09:00:04
【问题描述】:
通过发送以下 APNS-Payload:
{"aps":{"alert":"test Push","badge":1,"sound":"default"},
"objid":"SomeID","vid":"4229", "title":"aTitle","type":"queryform",
"idArray":["-100", "SomeID"]}`
以下方法将 NSDictionary 转换为一个类(notificationInfo 是来自“receivedNotification”的 userInfo。
以下问题:如果获取“idArray”的ObjectForKey:“idArray”等于payload中的key,则变量objid为空。但是objid 存在于notificationInfo 中。如果我重命名“idArray”,例如将设置为Idarray,objid。
ApnsNotificationInfo GetApnsNotificationInfo(NSDictionary notificationInfo)
{
var apnsInfo = new ApnsNotificationInfo();
if (notificationInfo == null || notificationInfo.Count == 0)
return apnsInfo;
var idArray = notificationInfo.ObjectForKey(new NSString("idArray")) as NSArray;
if (idArray != null && idArray.Count > 0)
{
var articleId = (idArray.Count == 2) ?
idArray.GetItem<NSNumber>(1) :
idArray.GetItem<NSNumber>(0);
if (articleId == null)
return apnsInfo;
apnsInfo.ArticleId = articleId.Int32Value;
if (idArray.Count == 2)
{
var categoryId = idArray.GetItem<NSNumber>(0);
if (categoryId != null)
apnsInfo.CategoryId = categoryId.Int32Value;
}
}
NSString message = null;
var aps = notificationInfo.ObjectForKey(new NSString("aps")) as NSDictionary;
if (aps != null)
message = aps.ObjectForKey(new NSString("alert")) as NSString;
if (message != null)
apnsInfo.Message = message.ToString();
var title = notificationInfo.ObjectForKey (new NSString ("title")) as NSString;
var type = notificationInfo.ObjectForKey (new NSString ("type")) as NSString;
var objid = notificationInfo.ObjectForKey (new NSString ("objid")) as NSString;
var vid = notificationInfo.ValueForKey (new NSString ("vid")) as NSString;
if(!string.IsNullOrEmpty (title)){
apnsInfo.Title = title;
}
if (!string.IsNullOrEmpty (type)) {
apnsInfo.Type = type;
if(objid != null){
apnsInfo.requestID = objid.ToString ();
}
if(vid != null){
apnsInfo.vid = Convert.ToInt32 (vid);
}
}
return apnsInfo;
}
我的问题的另一种解释:如果我发送此有效负载:
{"aps":{"alert":"test Push","badge":1,"sound":"default"}, "objid":"SomeID","vid":"4229", "title":"aTitle","type":"queryform", "Idarray":["-100", "SomeID"]}
var objid = notificationInfo.ObjectForKey (new NSString ("objid")) as NSString;
objid 将被设置。如果调用payload中的数组:idArray,objid = null
这真的很奇怪。我希望有人可以提供帮助。
【问题讨论】:
标签: c# xamarin push-notification xamarin.ios nsdictionary