我使用Parse.com的Push Service将数据保存到推送通知中,但是当我打印有效负载时。它似乎是零。为什么会发生这种情况,我该如何解决这个问题?谢谢。任何帮助表示赞赏。
func pushNotifications(){
let data = [
"alert" : "\(username!) wants you to listen to \(titleofsong) by \(artist)",
"identifier":"PlayerController", "title" : "test title", "artist" : "test artist"
]
let push = PFPush()
push.setQuery(pushQuery)
push.setData(data)
push.sendPushInBackgroundWithBlock {
success, error in
if success {
print("The push succeeded.")
} else {
print("The push failed.")
}
}
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
PFPush.handlePush(userInfo)
let title = userInfo["aps"]!["title"]
let artist = userInfo["aps"]!["artist"]
print(title)
print(artist)
}
答案 0 :(得分:1)
根据documentation您的APNS有效负载应该看起来像
let data = [
"aps" : [
"alert" : [
"title" : "New Request",
"body" : "\(username!) wants you to listen to \(titleofsong) by \(artist)",
"action-loc-key" : "Listen"
],
],
"username" : "test user",
"identifier" : "PlayerController",
"title" : "test title",
"artist" : "test artist"
]
然后你可以用
提取标题和艺术家let title = userInfo["title"]
let artist = userInfo["artist"]
答案 1 :(得分:0)
相反,我认为你应该打印出userInfo
中的内容。在Parse.com文档中,您无需在aps
之前检索自定义数据之前检索userInfo
,如下所示:
let title = userInfo["title"]
let artist = userInfo["artist"]
我实际上没有对它进行测试,我根据Parse文档得出了这个结论:https://parse.com/docs/ios/guide#push-notifications-responding-to-the-payload
Parse.com发送的示例:
let data = [
"alert" : "James commented on your photo!",
"p" : "vmRZXZ1Dvo" // Photo's object id
]
let push = PFPush()
push.setQuery(photoOwnerQuery)
push.setData(data)
push.sendPushInBackground()
Parse.com收到的示例:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
if let photoId: String = userInfo["p"] as? String {
let targetPhoto = PFObject(withoutDataWithClassName: "Photo", objectId: photoId)
targetPhoto.fetchIfNeededInBackgroundWithBlock { (object: PFObject?, error: NSError?) -> Void in
// Show photo view controller
if error != nil {
completionHandler(UIBackgroundFetchResult.Failed)
} else if PFUser.currentUser() != nil {
let viewController = PhotoVC(withPhoto: object)
self.navController.pushViewController(viewController, animated: true)
completionHandler(UIBackgroundFetchResult.NewData)
} else {
completionHandler(UIBackgroundFetchResult.NoData)
}
}
}
handler(UIBackgroundFetchResult.NoData)
}