我正在尝试让我的应用程序向个人用户发送推送通知(用户之前已经注册过)。我试图做的目的是通知用户他们之前在我的商店下的订单已经发货。
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *loggedUser = [userDefaults stringForKey:@"loggedUser"];
PFInstallation *installation = [PFInstallation currentInstallation];
[installation addUniqueObject:loggedUser forKey:@"channels"];
[installation saveInBackground];
(我没有使用
PFUser
而是有一个变量 loggedUser
可以正常工作。)上面的代码似乎工作正常,因为如果我去 Parse 上的数据浏览器,我可以在安装表 channel 列上看到用户名。
但是,虽然我在云代码上看到了用户,这意味着我在发送推送通知时已成功上传,但我看到它已发送给 0 个收件人。
使用以下代码通过 JavaScript 发送推送:
(变量
user
如上所述,我确保它也能正常工作。)Parse.Push.send({
channels: "[\""+user+"\"]",
data: {
alert: "Your order has been shipped."
}
}, {
success: function() {
alert('The push notification has been sent correctly')
},
error: function(error) {
alert('Error: '+ error)
}
});
有人知道我在做什么错吗?是吗
channels:
写错了吗?或者是因为我不能在这个 Parse 发送操作中使用变量?编辑:
这是整个文件
<script type="text/javascript">
Parse.$ = jQuery;
Parse.initialize("APPLICATION ID", "JAVASCRIPT KEY");
var PhotoObject = Parse.Object.extend("PhotoObject");
var query = new Parse.Query(PhotoObject);
query.equalTo("allowed", false);
query.equalTo("appear", true);
query.descending("createdAt");
query.find({
success: function(results) {
// Do something with the returned Parse.Object values
for (var i = 0; i < results.length; i++) {
var object = results[i];
var profilePhoto = object.get("image");
var description = object.get("description");
var location = object.get("location");
var user = object.get("user");
var copyLocation = '';
var copyDescription = '';
var copyUser = '';
if (description === '') {
copyDescription = '<em>No description.</em>';
}else {
copyDescription = description;
}
if (location === '') {
copyDescription = '<em>Could not determine location.</em>';
}else {
copyLocation = location;
}
if (user === undefined ) {
copyUser = '<em>Could not determine user.</em>';
}else {
copyUser = user;
}
$('#images').append("<li class='listItem"+i+"' id='"+object.id+"listItem''><img class='liPhoto' id='imagesID"+i+"' src='"+profilePhoto.url()+"'/></li>");
$("#imagesID"+i).wrap("<a href='"+profilePhoto.url()+"'/>");
$(".listItem"+i).append("<p class='description'><strong>Description: </strong>"+copyDescription+"<br><br><strong>User: </strong>"+copyUser+"<br><br><strong>Location: </strong>"+copyLocation+"</p>");
$(".listItem"+i).append("<br><br><button class='selectionButton' id='"+object.id+"'>Accept</button><button class='selectionButton'id='"+object.id+"decline'>Decline</button>");
}
$("button").click(function() {
if ((this.id).indexOf("decline") <= 0){
console.log('approve button pressed');
var query = new Parse.Query(PhotoObject);
query.get(this.id, {
success: function(photoObject) {
// The object was retrieved successfully.
photoObject.set("allowed", true);
photoObject.save(null, {
success: function(photoObject) {
// Execute any logic that should take place after the object is saved.
var idName = '#'+photoObject.id+'listItem';
console.log(idName);
$(idName).slideUp();
alert('The image was approved succesfully - '+ photoObject.id);
Parse.Push.send({
channels: [user],
data: {
alert: "Your image has been successfully moderated and has been published on the gallery!"
}
}, {
success: function() {
alert('The push notification has been sent correctly')
},
error: function(error) {
console.log(error);
alert('Error: '+ error)
}
});
},
error: function(photoObject, error) {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and description.
alert('Failed to approve the image, with error code: ' + error.description);
}
});
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and description.
}
});
} else {
console.log('decline button pressed');
var query = new Parse.Query(PhotoObject);
var idFormated = this.id.replace('decline','');
console.log(idFormated);
query.get(idFormated, {
success: function(photoObject) {
// The object was retrieved successfully.
photoObject.set("appear", false);
photoObject.save(null, {
success: function(photoObject) {
// Execute any logic that should take place after the object is saved.
var idName = '#'+photoObject.id+'listItem';
console.log(idName);
$(idName).slideUp();
alert('The image was declined succesfully - '+ photoObject.id);
},
error: function(photoObject, error) {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and description.
alert('Failed to declined the image, with error code: ' + error.description);
}
});
},
error: function(object, error) {
// The object was not retrieved successfully.
alert('Failed to declined the image, with error code: ' + error.description);
}
});
}
});
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
</script>
最佳答案
我要做的第一件事是更改您在 Parse.Push.send()
中发送 channel 的方式
channels: [user],
假设
user
是匹配 loggedUser
的字符串从前面的代码。
关于javascript - 解析发送给 0 个收件人的推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24853834/