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

时间:2015-04-14 20:33:28

标签: ios swift parse-platform notifications apple-push-notifications

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

这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

在Parse仪表板中进入应用的“设置”页面。在“推送通知”部分中,有一个名为“启用客户端推送?”的选项。确保已打开。

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

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

然后继续使用之前的代码。