2013-04-05 114 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以及一些示例中找到更多關於自定義有效載荷的信息。

問候, 斯托伊奇