2017-07-24 133 views
0

我正在使用heroku解析服務器應用並嘗試根據mongodb中可用的用戶數據向單個設備發送推送通知。我使用雲功能如下表:通過heroku解析服務器在單個設備上發送推送通知

Parse.Cloud.define("iosPush", function(request, response) { 

var user = request.user; 
var params = request.params; 
var someKey = params.someKey 
var data = params.data 

var pushQuery = new Parse.Query(Parse.Installation); 
pushQuery.equalTo('deviceType', 'ios'); // targeting iOS devices only 
pushQuery.equalTo("someKey", someKey) 

Parse.Push.send({ 
    where: pushQuery, // Set our Installation query 
    data: data 
}, { success: function() { 
    console.log("#### PUSH OK"); 
}, error: function(error) { 
    console.log("#### PUSH ERROR" + error.message); 
}, useMasterKey: true}); 

response.success('success'); 
}); 

我打一個捲曲的請求,檢查如推送通知 -

curl -X POST -H "X-Parse-Application-Id: myAppId" -H "X-Parse-Master-Key: masterkey" -H "Content-Type: application/json" -d { "where": { "user_id": "cedsGn98ja" }, "data": { "alert": "Hello, Parse!" } }

http://myapp.herokuapp.com/parse/functions/iosPush

推工作,但它是在所有設備上發送。我想要一種方式,以便我可以在單個設備上發送它。

謝謝...

+0

我試過這樣 捲曲-X POST -H 「X-解析,應用ID:myAppId」 -H「X-Parse- Master-Key:masterkey「-H」Content-Type:application/json「-d'{」其中「:」用戶「:{」__type「:」指針「,」className「:」_User「,」objectId「 :「cedsGn98ja」}},「data」:{「alert」:「new one2」}}'http://myapp.herokuapp.com/parse/functions/iosPush 但是還是一樣。推送是在所有設備上發送的,而不是僅此用戶 –

回答

0

someKey將成爲所有用戶的獨特之處?如果不是,您的推送應該發送到具有該匹配密鑰的所有安裝。爲了發送給特定用戶,您需要爲安裝對象上的user字段添加查詢約束。這可能是唯一的查詢約束條件,如果您想將其發送給所有用戶的設備,或者您可以使用deviceId或installationId來定位特定設備。

+0

謝謝。你能解釋我應該如何寫安裝ID的查詢?我嘗試像「where」:{「user」:「instalation_id」}但它沒有奏效。你也可以解釋如何使用某些鍵? –

+0

問題在於「where」條件無效。我嘗試了一切,但推送發送給所有用戶。 –

0

我用下面的代碼發送用戶1的通知給用戶1進行測試。您需要修改'someKey'以針對您的用例的特定用戶。不要忘記在您的應用程序代理中使用link your installation to the user

迅速

let text = "\(PFUser.current()!.username!) followed you, or whatever you want this to say"; 
let data = [ 
     "badge" : "Increment", 
     "alert" : text, 
    ] 
    let request: [String : Any] = [ 
     "someKey" : PFUser.current()!.objectId!, 
     "data" : data 
    ] 
    print(PFUser.current()!.objectId!) 
    print("sending push notification...") 
    PFCloud.callFunction(inBackground: "pushToFollowers", withParameters: request as [NSObject : AnyObject], block: { (results:AnyObject?, error:NSError?) in 
     print("push \(String(describing: results!))") 
     if error == nil { 
      print (results!) 
     } 
     else { 
      print (error!) 
     } 
    } as? PFIdResultBlock) 

雲代碼

Parse.Cloud.define("pushToFollowers", function (request, response) { 
    console.log("Inside pushToFollowers"); 
    var params = request.params; 
    var user = request.user; 
    var sessionToken = user.getSessionToken(); 
    var someKey = request.someKey; 
    var data = params.data; 

// Set our query to target specific user 
    var recipientUser = new Parse.Query(Parse.User); 
    recipientUser.equalTo("objectId", request.params.someKey); 

// Set our installation query 
    var pushQuery = new Parse.Query(Parse.Installation); 
    pushQuery.equalTo('deviceType', 'ios'); 
    pushQuery.matchesQuery('user', recipientUser); 
    pushQuery.find({ useMasterKey: true }).then(function(object) { 
     response.success(object); 
     console.log("pushQuery got " + object.length); 
     }, function(error) { 
      response.error(error); 
      console.error("pushQuery find failed. error = " + error.message); 
    }); 

    // Send push notification to query 
    Parse.Push.send({ 
    where: pushQuery, 
    data: data 
    }, { useMasterKey: true }) 
    .then(function() { 
     // Push sent! 
     console.log("#### push sent!"); 
    }, function(error) { 
     // There was a problem :(
     console.error("#### push error" + error.message); 
    }); 
response.success('success, end of pushToFollowers') 
}); 
+0

@ s.jain如果這對您有用,請接受答案。乾杯! – brewswane