简体   繁体   English

使用解析将用户的推送通知发送给另一个用户

[英]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. 如您在下图中所看到的(解析通知中的屏幕快照),当我从解析网站发送推送时,send send = 1,当我从代码发送推送时,push send = 0。 I tried many times but I don't know what is wrong with my code (Unfortunately I need 10 reputatuin to post the image) 我尝试了很多次,但是我不知道我的代码出了什么问题(不幸的是,我需要10个reputatuin才能发布图像)

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: 并在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. 编辑:您的实际问题似乎是您正在使用用户对象ID而不是实际的PFUser对象(这是存储在数据库中的对象)创建推式查询。 Try this instead. 试试这个吧。 I'm no good at Swift so this might not be correct syntax-wise. 我不擅长Swift,所以这可能不是正确的语法。

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. 然后继续您以前的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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