简体   繁体   中英

Send a push notification from a user to another using parse

In my application a notification will be sent using parse when a user request to buy from another user. The notification sent successfully to parse but it doesn't sent to the user(The receiver)! I tried to send a notification from parse website directly and it received. As you can see in the image below (a screenshot from my notification on parse) the push sent=1 when I send it from parse website and push sent= 0 when I send it from my code. I tried many times but I don't know what is wrong with my code (Unfortunately I need 10 reputatuin to post the image)

Here is my code:

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

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

Go into the Settings page for your app in the Parse dashboard. In the Push Notifications sections, there's an option called "Client push enabled?". Make sure this is switched on.

EDIT: Your actual problem seems to be that you're creating your push query with an user object ID rather than an actual PFUser object, which is what's stored in the database. Try this instead. I'm no good at Swift so this might not be correct syntax-wise.

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

Then continue with the code you had previously.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM