gpt4 book ai didi

ios - 通过heroku解析服务器在单个设备上发送推送通知

转载 作者:太空宇宙 更新时间:2023-11-03 23:02:03 26 4
gpt4 key购买 nike

我正在使用heroku解析服务器应用程序,并尝试根据mongodb中可用的用户数据将推送通知发送到单个设备。我正在使用如下所示的云功能:

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请求,以检查推送通知,例如 -

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

推送正在工作,但正在所有设备上发送。我想要一种方法,以便我可以在单个设备上发送它。

谢谢...

最佳答案

我使用以下代码从用户 1 向用户 1 发送通知以进行测试。您需要修改“someKey”以针对您的用例定位特定用户。别忘了link your installation to the user在您的应用程序委托(delegate)中。

快速

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

关于ios - 通过heroku解析服务器在单个设备上发送推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45279222/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com