2014-10-09 74 views
1

我想發送一個推送通知給我的應用程序的JSON格式包含自定義數據,但我不知道如何從中提取數據,或者如果我的JSON格式是正確的。 (我認爲這是因爲解析成功將其發送)Xcode - 推送通知Json

的JSON解析:

{ 
    "aps": { 
     "badge": 1, 
     "alert": "Test", 
     "sound": "" 
    }, 
    "url": "http://www.google.com" 
} 

的appdelegate:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: NSDictionary!) { 
    var notificationPayload: NSDictionary = userInfo["url"] as NSDictionary! 

    if (notificationPayload["url"] != nil) { 
    var url = notificationPayload["url"] as String 

var feed: FeedTableViewController = navigation.viewControllers[0] as FeedTableViewController 

     feed.messages.append(url) 
     feed.sections.append("url") 

    }else { 
     PFPush.handlePush(userInfo) 
    } 
} 

回答

1

試試這個:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: NSDictionary!) { 
    if let url = userInfo["url"] as? String { 

     var feed: FeedTableViewController = navigation.viewControllers[0] as FeedTableViewController 
     feed.messages.append(url) 
     feed.sections.append("url") 

    } else { 
     PFPush.handlePush(userInfo) 
    } 
} 
+0

是啊,這完美地工作,我可以添加儘可能多的數據嗎? 例如:「URL」,「日期」,「類別」 也熱來實現它的功能didLaunchWithOptions – Abdou023 2014-10-09 23:31:49

+0

只要它是小於256個字節http://stackoverflow.com/a/9183146/2611971 – Logan 2014-10-10 00:24:16