通过发送以下APNS有效负载:Xamarin的iOS不能从PushNotification有效载荷获取特定的变量
{"aps":{"alert":"test Push","badge":1,"sound":"default"},
"objid":"SomeID","vid":"4229", "title":"aTitle","type":"queryform",
"idArray":["-100", "SomeID"]}`
以下方法可转换NSDictionary的一类(notificationInfo是“receivedNotification”的用户信息 继问题:如果获得“idArray”的ObjectForKey:“idArray”等于有效载荷中的密钥,则变量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
将被设置。如果有效载荷中的阵列被调用:idArray
,objid = null
这真的很奇怪。我希望有人能帮帮忙。
谢谢,但问题是:若我在有效载荷写“idArray”的objid不会设置,如果我写“idarray”(或删除“idArray”)的objid将被设置。 – Klinki