使用解析将推送通知从一个用户发送到另一个用户

Send a push notification from a user to another using parse

在我的应用程序中,当用户请求从另一个用户购买时,将使用解析发送通知。通知已成功发送以进行解析,但未发送给用户(接收者)!我试图直接从解析网站发送通知并收到了。正如您在下图中看到的(我的解析通知的屏幕截图),当我从解析网站发送它时推送发送=1,当我从我的代码发送它时推送发送=0。我试了很多次,但我不知道我的代码有什么问题 (不幸的是,我需要 10 个声誉才能 post 图片)

这是我的代码:

            var pushQuery:PFQuery = PFInstallation.query()
            pushQuery.whereKey("user", equalTo: self.recipientObjectId)

            let data = [
                "alert" : "username requested to buy your item",
                "itemId" : "KotJR9dygE"]

            var push:PFPush = PFPush()
            push.setQuery(pushQuery)
            push.setData(data)

            push.setMessage("Username requested to buy your item") 
            push.sendPushInBackgroundWithBlock({
                (isSuccessful: Bool, error: NSError!) -> Void in
                println(isSuccessful) 
            })

在 AppDelegate 中:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let notficationTypes:UIUserNotificationType = UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge
let notficationSettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notficationTypes, categories: nil)  UIApplication.sharedApplication().registerUserNotificationSettings(notficationSettings)
return true}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
UIApplication.sharedApplication().registerForRemoteNotifications()}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
var currentInstallation:PFInstallation = PFInstallation.currentInstallation()
currentInstallation.setDeviceTokenFromData(deviceToken)
currentInstallation.save() }

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
println(error.localizedDescription)}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
var notification:NSDictionary = userInfo["aps"] as! NSDictionary
if (notification["content-available"] != nil){
    if notification.objectForKey("content-available")!.isEqualToNumber(1){
        NSNotificationCenter.defaultCenter().postNotificationName("reloadTimeline", object: nil)
    }
}else{
    PFPush.handlePush(userInfo)
}   }

在 Parse 仪表板中进入您的应用程序的“设置”页面。在推送通知部分,有一个名为 "Client push enabled?" 的选项。确保它已打开。

编辑: 您的实际问题似乎是您正在使用用户对象 ID 而不是实际的 PFUser 对象创建推送查询,后者存储在数据库中。试试这个。我不擅长 Swift,所以这在语法方面可能不正确。

var user:PFUser = PFQuery.getUserObjectWithId(self.recipientObjectId)
var pushQuery:PFQuery = PFInstallation.query()
pushQuery.whereKey("user", equalTo: user)

然后继续您之前的代码。