使用云代码发送解析推送
Sending Parse Push with Cloud Code
我找不到 Parse Cloud Code
中使用的 Parse.Push
的任何文档。我看到的使用案例是这样的...
// Send the push notification to results of the query
Parse.Push.send({
where: pushQuery,
data: {
alert: message
}
}).then(function() {
response.success("Push was sent successfully.")
}, function(error) {
response.error("Push failed to send with error: " + error.message);
});
我想做的是,如果收件人用户设置了通知(即具有与其用户关联的有效安装实例),则发送推送通知。
目前我创建查询并使用 pushQuery 将其传递到上面。我注意到在 Parse 仪表板中创建了一个推送,但发送的推送为 0。
真的,我只想在用户存在时创建推送。我已经创建了查询,并且可以 运行 这个和 return 如果我得到结果或不喜欢这样...
Parse.Cloud.define("sendTurnNotificationToUser", function(request, response) {
var senderUser = request.user;
var recipientUserId = request.params.recipientId;
var message = request.params.message;
// Validate the message text.
// For example make sure it is under 140 characters
if (message.length > 140) {
// Truncate and add a ...
message = message.substring(0, 137) + "...";
}
// Send the push.
// Find devices associated with the recipient user
var recipientUser = new Parse.User();
recipientUser.id = recipientUserId;
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("user", recipientUser);
pushQuery.find({
success: function(results) {
response.success("push user lookup was ok");
response.success(results);
},
error: function() {
response.error("push user lookup failed");
}
});
我可以将 Parse.Push.send 调用添加到查询成功。但是 Parse.Push.send
有一个 where
子句,我不知道那里需要什么?我不想 运行 查询两次。
你走在正确的轨道上。 Push "advanced targeting" 允许应用程序推送到查询产生的安装。这就是 where 子句的用途...
// don't run find on the pushQuery. set it up as you have it
// then, assuming it returns some installation(s)...
Parse.Push.send({ where: pushQuery, data: "hello" }).then(function(result) {
response.success(result);
}, function(error) {
response.error(error);
});
顺便说一句,您可以在 Parse.User 上使用 createWithoutData
作为快捷方式...
var recipient = Parse.User.createWithoutData(request.params.recipientId);
但您的较长表格也应该有效。
看来您可能想多了。向 0 个安装发送推送通知没有什么坏处,因为推送查询不会匹配任何收件人。我不会对此太担心,也不会在您的代码中添加这样的 pre-check 。它会给您的代码增加不必要的延迟,当然会导致查询 运行 两次。
如果您无论如何都想这样做——也许您希望让您的推送日志保持整洁——您确实可以查询安装 class 以检查查询是否与一组安装相匹配,如果是,您可以将相同的查询传递给 Parse.Push.send()
。
是的,这将导致查询 运行 两次,但这是预料之中的,因为如果不 运行 查询,您将无法知道将匹配多少对象。
我找不到 Parse Cloud Code
中使用的 Parse.Push
的任何文档。我看到的使用案例是这样的...
// Send the push notification to results of the query
Parse.Push.send({
where: pushQuery,
data: {
alert: message
}
}).then(function() {
response.success("Push was sent successfully.")
}, function(error) {
response.error("Push failed to send with error: " + error.message);
});
我想做的是,如果收件人用户设置了通知(即具有与其用户关联的有效安装实例),则发送推送通知。
目前我创建查询并使用 pushQuery 将其传递到上面。我注意到在 Parse 仪表板中创建了一个推送,但发送的推送为 0。
真的,我只想在用户存在时创建推送。我已经创建了查询,并且可以 运行 这个和 return 如果我得到结果或不喜欢这样...
Parse.Cloud.define("sendTurnNotificationToUser", function(request, response) {
var senderUser = request.user;
var recipientUserId = request.params.recipientId;
var message = request.params.message;
// Validate the message text.
// For example make sure it is under 140 characters
if (message.length > 140) {
// Truncate and add a ...
message = message.substring(0, 137) + "...";
}
// Send the push.
// Find devices associated with the recipient user
var recipientUser = new Parse.User();
recipientUser.id = recipientUserId;
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("user", recipientUser);
pushQuery.find({
success: function(results) {
response.success("push user lookup was ok");
response.success(results);
},
error: function() {
response.error("push user lookup failed");
}
});
我可以将 Parse.Push.send 调用添加到查询成功。但是 Parse.Push.send
有一个 where
子句,我不知道那里需要什么?我不想 运行 查询两次。
你走在正确的轨道上。 Push "advanced targeting" 允许应用程序推送到查询产生的安装。这就是 where 子句的用途...
// don't run find on the pushQuery. set it up as you have it
// then, assuming it returns some installation(s)...
Parse.Push.send({ where: pushQuery, data: "hello" }).then(function(result) {
response.success(result);
}, function(error) {
response.error(error);
});
顺便说一句,您可以在 Parse.User 上使用 createWithoutData
作为快捷方式...
var recipient = Parse.User.createWithoutData(request.params.recipientId);
但您的较长表格也应该有效。
看来您可能想多了。向 0 个安装发送推送通知没有什么坏处,因为推送查询不会匹配任何收件人。我不会对此太担心,也不会在您的代码中添加这样的 pre-check 。它会给您的代码增加不必要的延迟,当然会导致查询 运行 两次。
如果您无论如何都想这样做——也许您希望让您的推送日志保持整洁——您确实可以查询安装 class 以检查查询是否与一组安装相匹配,如果是,您可以将相同的查询传递给 Parse.Push.send()
。
是的,这将导致查询 运行 两次,但这是预料之中的,因为如果不 运行 查询,您将无法知道将匹配多少对象。