我已经成功地在云代码中找到了基本意义上的推送通知。我可以从我的应用程序发送一条测试消息,它会显示在我的手机上。
但我不知道如何更改云代码中request.params.message的值。
另外,下面的内容会导致云代码错误“键消息的类型无效,需要字符串,但得到数组”。什么数组?
如果还不清楚,我将尝试向订阅特定频道的用户发送推送通知。
我的Swift代码:
import UIKit
import Parse
import Bolts
class ViewController: UIViewController {
var channels = "B08873"
var minyanID = "bethel08873"
var message = "This is a test message."
override func viewDidLoad() {
super.viewDidLoad()
let currentInstallation = PFInstallation.currentInstallation()
currentInstallation.addUniqueObject( (minyanID), forKey:"channels")
currentInstallation.addUniqueObject(message, forKey: "message")
currentInstallation.saveInBackground()
}
@IBAction func sendPush(sender: AnyObject) {
if minyanID == channels {
PFCloud.callFunctionInBackground("alertUser", withParameters: [channels:message], block: {
(result: AnyObject?, error: NSError?) -> Void in
if ( error === nil) {
NSLog("Rates: \(result) ")
}
else if (error != nil) {
NSLog("error")
}
});
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我的云代码
Parse.Cloud.define("alertUser", function(request,response){
var query = new Parse.Query(Parse.Installation);
var theMessage = request.params.channels;
var theChannel = request.params.message;
query.equalTo(theChannel)
Parse.Push.send({
where: query,
data : { alert: theMessage, badge: "Increment", sound: "", } },
{ success: function() { response.success() },
error: function(error) { response.error(err) }
});
});
最佳答案
因为您将B08873
作为键发送,并将This is a test message.
作为其值发送。如果要同时发送通道和消息键/值对,则需要这样做:
PFCloud.callFunctionInBackground("alertUser", withParameters: ["channels": channels , "message": message], .....
在Cloud函数中,您应该能够像这样访问这些参数:
var theChannels = request.params.channels; // returns B08873
var theMessage = request.params.message; // returns This is a test message.
然后像这样调用Push函数:
Parse.Push.send({
channels: [ theChannels ],
data: {
alert: theMessage ,
badge: "Increment"
}}, {
success: function() {
response.success();
},
error: function(error) {
response.error(error);
}
});
关于swift - 如何将变量从Swift传递到Cloud Code?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34322566/