解析推送通知云代码错误?
Parse Push Notification Cloud Code Error?
更新: 我收到这个错误是因为 Parse-Server 2.2.17 有一个错误。我通过回到 2.2.16 来修复它。
有谁知道我为什么会收到这个错误?这是我的云代码:
`Parse.Cloud.define("Messages", 函数(请求, 响应) {
var pushQuery = new Parse.Query(Parse.Installation);
Parse.Push.send({
where: pushQuery,
data: {
alert: "New Event Added",
sound: "default"
}
},{
success: function(){
console.log("Push Sent!")
},
error: function (error) {
console.log(error)
},
useMasterKey: true
});
});`
这是我收到的错误:
然后这就是我调用代码的方式:`PFCloud.callFunctionInBackground("Messages", withParameters: nil) { (object, error) in
if error == nil {
print("Success!")
} else {
print(error)
}
}
index.js:
`
能否请您尝试以下代码:
var query = new Parse.Query(Parse.Installation);
// query condition (where equal to .. etc.)
var payload = {
alert: "New Event Added",
sound: "default"
};
Parse.Push.send({
where: query, // Set our Installation query
data: payload
}, {
success: function () {
},
error: function (error) {
// Handle error
}
});
请注意我删除了 useMasterKey 如果你想添加 useMasterKey 你需要将它插入闭包中但对我来说它没有 useMasterKey
useMasterKeyVersion 应如下所示:
Parse.Push.send({
where: query, // Set our Installation query
data: payload
},
{
useMasterKey: true
},
{
success: function () {
},
error: function (error) {
// Handle error
}
});
承诺版本(根据最佳实践):
Parse.Push.send({where: query,data: payload})
.then(function(){
// success
},function(error){
// error ..
});
更新
通过查看您的 index.js 文件,您似乎没有将 facebook oauth 添加到您的 3 方身份验证登录。
所以你需要添加以下内容:
oauth: {
facebook: {
appIds: "FACEBOOK APP ID"
}
}
在您的 emailAdapter 配置下方和内部 "FACEBOOK APP ID" 放置您在 facebook 开发人员中创建的应用程序 ID
在 main.js 中放入此代码
// SEND PUSH NOTIFICATION
Parse.Cloud.define("push", function(request, response) {
var user = request.user;
var params = request.params;
var someKey = params.someKey
var data = params.data
var recipientUser = new Parse.User();
recipientUser.id = someKey;
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("userID", 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');
});
// SEND PUSH NOTIFICATION FOR ANDROID
Parse.Cloud.define("pushAndroid", function(request, response) {
var user = request.user;
var params = request.params;
var someKey = params.someKey
var data = params.data
var recipientUser = new Parse.User();
recipientUser.id = someKey;
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("userID", someKey);
Parse.Push.send({
where: pushQuery, // Set our Installation query
data: {
alert: data
}
}, { success: function() {
console.log("#### PUSH OK");
}, error: function(error) {
console.log("#### PUSH ERROR" + error.message);
}, useMasterKey: true});
response.success('success');
});
在 xcode 项目中发送推送通知
// Send Push notification
let pushStr = "@\(PFUser.current()![USER_USERNAME]!) | \n\(self.lastMessageStr)"
let data = [ "badge" : "Increment",
"alert" : pushStr,
"sound" : "bingbong.aiff",
] as [String : Any]
let request = [
"someKey" : self.userObj.objectId!,
"data" : data
] as [String : Any]
PFCloud.callFunction(inBackground: "push", withParameters: request as [String : Any], block: { (results, error) in
if error == nil {
print ("\nPUSH SENT TO: \(self.userObj[USER_USERNAME]!)\nMESSAGE: \(pushStr)\n")
} else {
print ("\(error!.localizedDescription)")
}
})
更新: 我收到这个错误是因为 Parse-Server 2.2.17 有一个错误。我通过回到 2.2.16 来修复它。
有谁知道我为什么会收到这个错误?这是我的云代码:
`Parse.Cloud.define("Messages", 函数(请求, 响应) {
var pushQuery = new Parse.Query(Parse.Installation);
Parse.Push.send({
where: pushQuery,
data: {
alert: "New Event Added",
sound: "default"
}
},{
success: function(){
console.log("Push Sent!")
},
error: function (error) {
console.log(error)
},
useMasterKey: true
}); });`
这是我收到的错误:
然后这就是我调用代码的方式:`PFCloud.callFunctionInBackground("Messages", withParameters: nil) { (object, error) in
if error == nil {
print("Success!")
} else {
print(error)
}
}
index.js:
能否请您尝试以下代码:
var query = new Parse.Query(Parse.Installation);
// query condition (where equal to .. etc.)
var payload = {
alert: "New Event Added",
sound: "default"
};
Parse.Push.send({
where: query, // Set our Installation query
data: payload
}, {
success: function () {
},
error: function (error) {
// Handle error
}
});
请注意我删除了 useMasterKey 如果你想添加 useMasterKey 你需要将它插入闭包中但对我来说它没有 useMasterKey
useMasterKeyVersion 应如下所示:
Parse.Push.send({
where: query, // Set our Installation query
data: payload
},
{
useMasterKey: true
},
{
success: function () {
},
error: function (error) {
// Handle error
}
});
承诺版本(根据最佳实践):
Parse.Push.send({where: query,data: payload})
.then(function(){
// success
},function(error){
// error ..
});
更新
通过查看您的 index.js 文件,您似乎没有将 facebook oauth 添加到您的 3 方身份验证登录。 所以你需要添加以下内容:
oauth: {
facebook: {
appIds: "FACEBOOK APP ID"
}
}
在您的 emailAdapter 配置下方和内部 "FACEBOOK APP ID" 放置您在 facebook 开发人员中创建的应用程序 ID
在 main.js 中放入此代码
// SEND PUSH NOTIFICATION
Parse.Cloud.define("push", function(request, response) {
var user = request.user;
var params = request.params;
var someKey = params.someKey
var data = params.data
var recipientUser = new Parse.User();
recipientUser.id = someKey;
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("userID", 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');
});
// SEND PUSH NOTIFICATION FOR ANDROID
Parse.Cloud.define("pushAndroid", function(request, response) {
var user = request.user;
var params = request.params;
var someKey = params.someKey
var data = params.data
var recipientUser = new Parse.User();
recipientUser.id = someKey;
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("userID", someKey);
Parse.Push.send({
where: pushQuery, // Set our Installation query
data: {
alert: data
}
}, { success: function() {
console.log("#### PUSH OK");
}, error: function(error) {
console.log("#### PUSH ERROR" + error.message);
}, useMasterKey: true});
response.success('success');
});
在 xcode 项目中发送推送通知
// Send Push notification
let pushStr = "@\(PFUser.current()![USER_USERNAME]!) | \n\(self.lastMessageStr)"
let data = [ "badge" : "Increment",
"alert" : pushStr,
"sound" : "bingbong.aiff",
] as [String : Any]
let request = [
"someKey" : self.userObj.objectId!,
"data" : data
] as [String : Any]
PFCloud.callFunction(inBackground: "push", withParameters: request as [String : Any], block: { (results, error) in
if error == nil {
print ("\nPUSH SENT TO: \(self.userObj[USER_USERNAME]!)\nMESSAGE: \(pushStr)\n")
} else {
print ("\(error!.localizedDescription)")
}
})