2013-04-05 161 views
17

我是所有iOS推送通知域的新手。我已经使用下面的代码尝试了一个基本的推送通知,它完美地工作。我正在使用“使用JdSoft.Apple.Apns.Notifications;”来完成这一点。下面的代码:iOS推送通知自定义格式

Notification alertNotification = new Notification(testDeviceToken); 

alertNotification.Payload.Alert.Body = "Hello World";   
alertNotification.Payload.Sound = "default"; 
alertNotification.Payload.Badge = 1; 

这给出了以下的结构输出到iPhone:

{ 
    aps =  { 
     alert = "Hello World"; 
     badge = 1; 
     sound = default; 
    }; 
} 

我现在拿到添加自定义标签的要求如下:

{ 
      "aps":   { 
        "alert": "Hello World", 
        "sound": "default", 
    "Person":     { 
           "Address": "this is a test address", 
           "Name": "First Name", 
           "Number": "023232323233" 
          
    }   
    } 
} 

我发现很难在“aps”中获得“Person”。我也知道您可以使用以下代码添加自定义属性:

alertNotification.Payload.AddCustom(“Person”,Newtonsoft.Json.JsonConvert.SerializeObject(stat));

但上面的代码并没有添加“aps”标签。请告诉我如何实现?

+0

自定义实体不应该在APS元素。 [苹果示例有效载荷](http://developer.apple.com/library/iOS/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW15) – rckoenes 2013-04-05 13:04:13

回答

32

您不允许在aps标签内放置自定义标签。以下是文档中提到的内容:

提供者可以在Apple保留的aps命名空间外指定自定义有效负载值。自定义值必须使用JSON结构化和基本类型:字典(对象),数组,字符串,数字和布尔值。

所以你的情况,你应该这样做:

{ 
    "aps": { 
     "alert": "Hello World", 
     "sound": "default" 
    }, 
    "Person": { 
     "Address": "this is a test address", 
     "Name": "First Name", 
     "Number": "023232323233" 
    } 
} 

因此你可以寻找它读取您的自定义负载在主JSON的关键,而不是在“APS”:

NSLog(@"%@",notification['Person']['Address']); 

上面会输出:

这是一个测试地址

您可以在Apple docs以及一些示例中找到更多关于自定义有效载荷的信息。

问候, 斯托伊奇