Reputation: 151
I have successfully figured out push notifications in Cloud Code in the basic sense. I can send a test message from my app and it shows up on my phone.
But I don't understand how to change the values of request.params.message in my Cloud Code.
Also, what I have below causes a Cloud Code error of "invalid type for key message, expected String, but got array." What array?
In case it isn't clear, I am trying to send a push notification to users who subscribe to a particular channel.
My Swift code:
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.
}
}
My Cloud Code
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) }
});
});
Upvotes: 1
Views: 575
Reputation: 2717
Because you are sending B08873
as key and This is a test message.
as its value. If you want to send both channel and message key/value pairs you need to do it like this instead:
PFCloud.callFunctionInBackground("alertUser", withParameters: ["channels": channels , "message": message], .....
In your Cloud function you should be bale to access these paramerts like this:
var theChannels = request.params.channels; // returns B08873
var theMessage = request.params.message; // returns This is a test message.
Then call the Push function like this:
Parse.Push.send({
channels: [ theChannels ],
data: {
alert: theMessage ,
badge: "Increment"
}}, {
success: function() {
response.success();
},
error: function(error) {
response.error(error);
}
});
Upvotes: 1