为什么推送通知中的有效负载为零?
Why is the payload in the push notification nil?
我使用 Parse.com 的推送服务将数据保存到推送通知,但是当我打印负载时。它似乎是零。为什么会发生这种情况,我该如何解决?谢谢。任何帮助表示赞赏。
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)
}
相反,我认为您应该打印出 userInfo
中的内容。从 Parse.com 文档中,您不需要在从 userInfo
检索自定义数据之前检索 aps
,如下所示:
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)
}
根据 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"]
我使用 Parse.com 的推送服务将数据保存到推送通知,但是当我打印负载时。它似乎是零。为什么会发生这种情况,我该如何解决?谢谢。任何帮助表示赞赏。
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)
}
相反,我认为您应该打印出 userInfo
中的内容。从 Parse.com 文档中,您不需要在从 userInfo
检索自定义数据之前检索 aps
,如下所示:
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)
}
根据 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"]