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

时间:2017-07-24 11:16:07

标签: ios node.js heroku push parse-server

我正在使用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 \
-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

推送正在运行,但它正在所有设备上发送。我想要一种方法,以便我可以在一台设备上发送它。

...谢谢

2 个答案:

答案 0 :(得分:0)

someKey对所有用户来说都是唯一的吗?如果不是,您的推送应该发送到具有该匹配密钥的所有安装。要发送给特定用户,您需要为Installation对象上的user字段添加查询约束。如果您想将其发送到该用户的所有设备,或者您可以使用deviceId或installationId定位特定设备,则这可能是唯一的查询约束。

答案 1 :(得分:0)

我使用以下代码从用户1向用户1发送通知以进行测试。您需要修改某些关键字'针对您的用例定位特定用户。不要忘记你的app代理中的link your installation to the user

swift

1/1/2017   55
1/3/2017   23
1/3/2017   87
1/3/2017   45
1/4/2017   77
1/4/2017   90
1/4/2017   34

云代码

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)
相关问题