Xcode - Push Notification Json

2019-08-03 07:21发布

问题:

I would like to send a push notification to my app in Json format containing custom data, but i don't know how to extract the data from it, or if even my json format is correct. ( i think it is because Parse sends it successfully)

Json from Parse:

{
    "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:

Try this instead:

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)
    }
}