Parse Push Notification Cloud Code Error? - ios

UPDATE: I was getting this error because of a bug that Parse-Server 2.2.17 has. I fixed it by going back to 2.2.16.
Does anyone know why I am getting this error? Here is my cloud code:
`Parse.Cloud.define("Messages", function(request, response) {
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
});
});`
Here is the error I am getting:
And then this is how I am calling the code: `PFCloud.callFunctionInBackground("Messages", withParameters: nil) { (object, error) in
if error == nil {
print("Success!")
} else {
print(error)
}
}
index.js:
`

Can you please try the following code:
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
}
});
Please notice that i remove the useMasterKey if you want to add useMasterKey you need to insert it inside closures but for me it's works without the useMasterKey
the useMasterKeyVersion should look like the following:
Parse.Push.send({
where: query, // Set our Installation query
data: payload
},
{
useMasterKey: true
},
{
success: function () {
},
error: function (error) {
// Handle error
}
});
The promises version (according to best practices):
Parse.Push.send({where: query,data: payload})
.then(function(){
// success
},function(error){
// error ..
});
Update
by looking at your index.js file it looks like you didn't add the facebook oauth to your 3-party authentication logins.
so you will need to add the following:
oauth: {
facebook: {
appIds: "FACEBOOK APP ID"
}
}
below your emailAdapter config and inside "FACEBOOK APP ID" put the app ID that you created in facebook developers

in main.js put this code
// 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');
});
in xcode project do like that to send push notification
// 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)")
}
})

Related

Errors occur submit batch request

I am trying to use batch request for sending http post to the server.
The code snippet, which generate the request:
_.each(aNewDates, function (oNew) {
oModel.create("/CostCenterCalendarSet", oNew, {
groupId: "newDates"
});
});
oModel.setDeferredGroups(["newDates"]);
and the submit method:
oModel.submitChanges({
groupId: "newDates",
oSuccess: function (oMsg) {
return observer.next(oMsg);
},
oError: function (oErr) {
return observer.error(oErr);
},
});
as response I've got following error:
What am I doing wrong?
Update
I tried with ODataModel read method and does not get any result.
let oPlantFilter = new sap.ui.model.Filter("Plant", sap.ui.model.FilterOperator.EQ, oSelectedData.sPlant);
let oWcFilter = new sap.ui.model.Filter("WorkCenter", sap.ui.model.FilterOperator.EQ, oSelectedData.sWc);
oModel.read("/CostCenterCalendarSet", {
groupId: "query-dates",
filters: [oPlantFilter, oWcFilter]
});
oModel.setDeferredGroups(["query-dates"]);
return Rx.Observable.create(function (subscriber) {
oModel.submitChanges({
groupId: "query-dates",
success: function (oData, oResponse) {
return subscriber.next(oResponse);
},
error: function (oErr) {
return subscriber.error(oErr);
},
});
});

How Do I send VoIP push notifications to an iOS device using Amazon SNS in Node JS

I am trying to send VoIP push notifications directly to an iOS device from an App server using the NodeJS package called sns-mobile and Amazon SNS API.
However, When I try to send VoIP pushes using the below code, here is the error message that I get. Could someone please suggest me where I am going wrong, I have been spending nearly half a day to resolve this.
Invalid parameter: JSON must contain an entry for 'default' or
'APNS_VOIP
var iOSApp = new SNS({
platform: SNS.SUPPORTED_PLATFORMS.IOS,
region: 'us-west-2',
apiVersion: '2010-03-31',
accessKeyId: 'XXXXXXXXXXXXX',
secretAccessKey: 'XXXXXXXXXXXXX',
platformApplicationArn: 'arn:aws:sns:us-west-2:3303035XXXXX:app/APNS_VOIP/VoIPPushesApp'
});
iOSApp.addUser('deviceID',
JSON.stringify({
"APNS_VOIP": JSON.stringify({aps:{alert:"Hello and have a good day."}})
})
, function(err, endpointArn) {
if(err) {
console.log("The Error is :****: "+JSON.stringify(err, null, 4));
throw err;
}
// Send a simple String or data to the client
iOSApp.sendMessage(endpointArn, 'Hi There!', function(err, messageId) {
//iOSApp.sendMessage(endpointArn, messageTest, function(err, messageId) {
if(err) {
console.log("The Error in end message is :****: "+JSON.stringify(err, null, 4));
throw err;
}
console.log('Message sent, ID was: ' + messageId);
});
});
Here is the code that send a VoIP notifications to the receiver device using its device VoIP token. When a VoIP notification is received, the device calls a function called, didReceiveIncomingPushWithPayload
var AWS = require('aws-sdk');
// Amazon SNS module
AWS.config.update({
accessKeyId : 'XXXXXXXXXXXXXXXX',
secretAccessKey : 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
region : 'us-west-2'
});
var amazonSNS = new AWS.SNS();
// 1. Create Platform Endpoint
var createPlatformEndpoint = function(req, res, next){
var deviceVoipToken = req.deviceVoipToken; // Obtaining the device VoIP token from the request object
amazonSNS.createPlatformEndpoint({
// App in Sandboxmode (ie running on device directly from Xcode)
PlatformApplicationArn: "arn:aws:sns:us-west-2:xxxxxxxxxxxx:app/APNS_VOIP_SANDBOX/CurieVoip",
// App in Production mode (ie running on device after archiving and installed on device with a provisioning profile)
//PlatformApplicationArn: "arn:aws:sns:us-west-2:xxxxxxxxxxxx:app/APNS_VOIP/CurieVoip",
Token: deviceVoipToken
}, function(err, data) {
if (err) {
console.log(err.stack);
response.json({"status": "failure", "statusCode" : 402})
return;
}
var endpointArn = data.EndpointArn;
req.endpointArn = data.EndpointArn; // Passing the EndpointArn to the next function
next()
})
}
// 2. Publish notification
var publishVoipNotification = function(req, res, next){
var endpointArn = req.endpointArn;
var payload = {
default : 'Hello World, default payload',
APNS_VOIP : {
aps: {
alert: 'Hi there',
sound: 'default',
badge: 1
}
}
};
// first have to stringify the inner APNS object...
payload.APNS = JSON.stringify(payload.APNS);
// then have to stringify the entire message payload
payload = JSON.stringify(payload);
console.log('sending push');
amazonSNS.publish({
MessageStructure : 'json',
Message : payload,
TargetArn : endpointArn
}, function(err, data) {
if (err) {
console.log("Error stack: "+err.stack);
var message = "There has been an error in Publishing message via AmazonSNS with error: "+err.stack;
res.json({"status": "failure", "statusCode" : 402, "message" : message})
return;
}
next();
});
}
// 3. Deleting platform endpoint after sending a voipNotification; Ref: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#deleteEndpoint-property
var deleteEndpointArn = function(req, res){
var endpointArn = req.endpointArn;
var roomName = req.roomName;
var params = {
EndpointArn: endpointArn /* required */
};
amazonSNS.deleteEndpoint(params, function(err, data) {
if (err){
var message = "Unable to deleteEndpointArn, with error: "+err.stack;
res.json({"status": "failure", "statusCode" : 400, "message":message})
}
else{
var message = "Deleted endpointArn successfully";
res.json({"status": "success", "statusCode" : 200, "message":message})
}
});
}
router.post('/sendVoipNotificationToReceiver', [createPlatformEndpoint, publishVoipNotification, deleteEndpointArn]);
Go to Amazon SNS
Click on Mobile: Push notifications
Click on Create platform Application,
User Apple Credentials "Used for development in sandbox" check this checkbox if you want to test voip notification on sandbox.
Push certificate type -- Voip Push certificate
Upload Certificate .P12 files.
Save Form and copy ARN
Create Lambda function. Code example below
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set region
AWS.config.update({region: 'us-east-1'});
var amazonSNS = new AWS.SNS();
const isset = (s) => {
return typeof s !== typeof undefined ? true : false;
};
exports.handler = (event, context, callback) => {
let data = event['body-json'];
if (isset(data)) {
let getAction = data['action'];
let userToken = data['token'];
let webPayload = data['payload'];
if (isset(getAction) && getAction == 'APNS_VOIP') {
amazonSNS.createPlatformEndpoint({
PlatformApplicationArn: 'YOUR APPLICATION ARN',
Token: userToken
}, function (err, data1) {
if (err) {
//handle error
console.log(err)
} else {
//Publish notification
var endpointArn = data1.EndpointArn;
console.log("endpointArn",endpointArn);
var payload = {
default: 'Silent voip push notification',
APNS_VOIP: {
//aps:webPayload
"aps" : {
"alert" : {
"title" : "Call Request"
},
"data":webPayload,
"content-available":1,
"category":"GENERAL"
}
}
};
payload.APNS_VOIP = JSON.stringify(payload.APNS_VOIP);
payload = JSON.stringify(payload);
amazonSNS.publish({
MessageStructure: 'json',
Message: payload,
MessageAttributes:{
"AWS.SNS.MOBILE.APNS.PRIORITY":{"DataType":"String","StringValue":"10"},
"AWS.SNS.MOBILE.APNS.PUSH_TYPE":{"DataType":"String","StringValue":"voip"}
},
TargetArn: endpointArn
}, function (err, data2) {
if (err) {
//handle error
console.log(err)
} else {
var params = {
EndpointArn: endpointArn /* required */
};
//Deleting platform endpoint after sending a voipNotification;
amazonSNS.deleteEndpoint(params, function (err, data3) {
if (err) {
//handle error
//console.log(err);
callback(null, {
"status": "error",
"data": [],
"message": []
});
} else {
//code success
console.log("delete",data3);
callback(null, {
"status": "Success",
"data": data3,
"message":"notification sent successfully"
});
}
});
}
});
}
});
}
else if (isset(getAction) && getAction == 'APNS_VOIP_SANDBOX') {
amazonSNS.createPlatformEndpoint({
PlatformApplicationArn: 'YOUR APPLICATION ARN',
Token: userToken
}, function (err, data1) {
if (err) {
//handle error
console.log(err)
} else {
//Publish notification
var endpointArn = data1.EndpointArn;
console.log("endpointArn",endpointArn);
var payload = {
default: 'Silent voip push notification',
APNS_VOIP_SANDBOX: {
"aps" : {
"alert" : {
"title" : "Call Request"
},
"data":webPayload,
"content-available":1,
"category":"GENERAL"
}
}
};
payload.APNS_VOIP_SANDBOX = JSON.stringify(payload.APNS_VOIP_SANDBOX);
payload = JSON.stringify(payload);
amazonSNS.publish({
MessageStructure: 'json',
Message: payload,
MessageAttributes:{
"AWS.SNS.MOBILE.APNS.PRIORITY":{"DataType":"String","StringValue":"10"},
"AWS.SNS.MOBILE.APNS.PUSH_TYPE":{"DataType":"String","StringValue":"voip"}
},
TargetArn: endpointArn
}, function (err, data2) {
if (err) {
//handle error
console.log(err)
} else {
var params = {
EndpointArn: endpointArn /* required */
};
//Deleting platform endpoint after sending a voipNotification; Ref: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#deleteEndpoint-property
amazonSNS.deleteEndpoint(params, function (err, data3) {
if (err) {
//handle error
//console.log(err);
callback(null, {
"status": "error",
"data": [],
"message": []
});
} else {
//code success
console.log("delete",data3);
callback(null, {
"status": "Success",
"data": data3,
"message":"notification sent successfully"
});
}
});
}
});
}
});
}
else {
callback(null, {
"status": "error",
"data": [],
"message": "You have not posted valid data."
});
}
}
};
https://docs.aws.amazon.com/sns/latest/dg/sns-send-custom-platform-specific-payloads-mobile-devices.html
How Do I send VoIP push notifications to an iOS device using Amazon SNS in Node JS

IBM MFP onReadyToSubscribe method is not called

I have an iOS hybrid application written on IBM MFP 7.1 with angular. Currently I'm trying to use push notifications but the code never enters in onReadyToSubscribe method.
I get all the code from the documentation about the push notifications and still I have the problem.
My application-descriptor.xml is
<application xmlns="http://www.worklight.com/application-descriptor" id="B" platformVersion="7.1.0.00.20151227-1725">
<displayName>A</displayName>
<description>A</description>
<author>
<name>application's author</name>
<email>application author's e-mail</email>
<homepage>http://mycompany.com</homepage>
<copyright>Copyright My Company</copyright>
</author>
<mainFile>index.html</mainFile>
<features/>
<thumbnailImage>common/images/thumbnail.png</thumbnailImage>
<ipad bundleId="xxx.xxx.xxx" version="1.0" securityTest="PushSecurityTest" >
<worklightSettings include="false"/>
<pushSender password="123456"/>
<security>
<encryptWebResources enabled="false"/>
<testWebResourcesChecksum enabled="false" ignoreFileExtensions="png, jpg, jpeg, gif, mp4, mp3"/>
</security>
</ipad>
main.js file the one where we should have the magic
function wlCommonInit() {
PushAppRealmChallengeHandler.init();
WL.Client.connect({
onSuccess: connectSuccess,
onFailure: connectFailure
});
//---------------------------- Set up push notifications -------------------------------
if (WL.Client.Push) {
WL.Client.Push.onReadyToSubscribe = function() {
WL.SimpleDialog.show("Push Notifications", "onReadyToSubscribe", [ {
text : 'Close',
handler : function() {}
}
]);
$('#SubscribeButton').removeAttr('disabled');
$('#UnsubscribeButton').removeAttr('disabled');
WL.Client.Push.registerEventSourceCallback(
"myPush",
"PushAdapter",
"PushEventSource",
pushNotificationReceived);
};
}
}
function connectSuccess() {
WL.Logger.debug ("Successfully connected to MobileFirst Server.");
}
function connectFailure() {
WL.Logger.debug ("Failed connecting to MobileFirst Server.");
WL.SimpleDialog.show("Push Notifications", "Failed connecting to MobileFirst Server. Try again later.",
[{
text : 'Reload',
handler : WL.Client.reloadapp
},
{
text: 'Close',
handler : function() {}
}]
);
}
function loginButtonClicked() {
var reqURL = '/j_security_check';
var options = {
parameters : {
j_username : $('#usernameInputField').val(),
j_password : $('#passwordInputField').val()
},
headers: {}
};
PushAppRealmChallengeHandler.submitLoginForm(reqURL, options, PushAppRealmChallengeHandler.submitLoginFormCallback);
}
function isPushSupported() {
var isSupported = false;
if (WL.Client.Push){
isSupported = WL.Client.Push.isPushSupported();
}
alert(isSupported);
WL.SimpleDialog.show("Push Notifications", JSON.stringify(isSupported), [ {
text : 'Close',
handler : function() {}}
]);
}
function isPushSubscribed() {
var isSubscribed = false;
if (WL.Client.Push){
isSubscribed = WL.Client.Push.isSubscribed('myPush');
}
WL.SimpleDialog.show("Push Notifications", JSON.stringify(isSubscribed), [ {
text : 'Close',
handler : function() {}}
]);
}
// --------------------------------- Subscribe ------------------------------------
function doSubscribe() {
WL.Client.Push.subscribe("myPush", {
onSuccess: doSubscribeSuccess,
onFailure: doSubscribeFailure
});
}
function doSubscribeSuccess() {
WL.SimpleDialog.show("Push Notifications", "doSubscribeSuccess", [ {
text : 'Close',
handler : function() {}}
]);
}
function doSubscribeFailure() {
WL.SimpleDialog.show("Push Notifications", "doSubscribeFailure", [ {
text : 'Close',
handler : function() {}}
]);
}
//------------------------------- Unsubscribe ---------------------------------------
function doUnsubscribe() {
WL.Client.Push.unsubscribe("myPush", {
onSuccess: doUnsubscribeSuccess,
onFailure: doUnsubscribeFailure
});
}
function doUnsubscribeSuccess() {
WL.SimpleDialog.show("Push Notifications", "doUnsubscribeSuccess", [ {
text : 'Close',
handler : function() {}}
]);
}
function doUnsubscribeFailure() {
WL.SimpleDialog.show("Push Notifications", "doUnsubscribeFailure", [ {
text : 'Close',
handler : function() {}}
]);
}
//------------------------------- Handle received notification ---------------------------------------
function pushNotificationReceived(props, payload) {
WL.SimpleDialog.show("Push Notifications", "Provider notification data: " + JSON.stringify(props), [ {
text : 'Close',
handler : function() {
WL.SimpleDialog.show("Push Notifications", "Application notification data: " + JSON.stringify(payload), [ {
text : 'Close',
handler : function() {}}
]);
}}
]);
}
And the last magic js file handles the authentication on the MFP server
var PushAppRealmChallengeHandler = (function(){
var challengeHandler;
function init() {
challengeHandler = WL.Client.createChallengeHandler("PushAppRealm");
challengeHandler.isCustomResponse = isCustomResponse;
challengeHandler.handleChallenge = handleChallenge;
challengeHandler.submitLoginFormCallback = submitLoginFormCallback;
}
function isCustomResponse(response) {
if (!response || response.responseText === null) {
return false;
}
var indicatorIdx = response.responseText.search('j_security_check');
if (indicatorIdx >= 0){
return true;
}
return false;
}
function handleChallenge(response) {
$('#AppBody').hide();
$('#AuthBody').show();
$('#passwordInputField').val('');
}
function submitLoginFormCallback(response) {
var isLoginFormResponse = challengeHandler.isCustomResponse(response);
if (isLoginFormResponse){
challengeHandler.handleChallenge(response);
} else {
$('#AppBody').show();
$('#AuthBody').hide();
challengeHandler.submitSuccess();
}
}
function submitLoginForm(url, options, callback) {
challengeHandler.submitLoginForm(url, options, callback)
}
return {
init: init,
submitLoginForm: submitLoginForm,
submitLoginFormCallback: submitLoginFormCallback
}
})();
I already checked the certificate and it is okay, also I redeploy everything when I add the certificate.
Do you have some ideas where I can have a problem?
When onReadyToSubscribe should be called?
Is it related with the authentication of the application?
Thanks in advance
This was an issue with Apple Sandbox APNs not providing token as reported in the following links:
https://forums.developer.apple.com/message/155239#155239
https://forums.developer.apple.com/thread/52224

Push notification does not receive (phonegap-push-plugin, ios, GCM)

I'm trying to recive push from gcm on the ios.
I have registered my application there: https://developers.google.com/mobile/add?platform=ios&cntapi=gcm&cnturl=https:%2F%2Fdevelopers.google.com%2Fcloud-messaging%2Fios%2Fclient&cntlbl=Continue%20Adding%20GCM%20Support&%3Fconfigured%3Dtrue
uploaded p12 certificates, added obtained GoogleService-Info.plist to the root of ios xcode project.
var params = {
"android": {"senderID": APP.senderId},
"ios": {"alert": "true", "badge": "true", "sound": "true", "senderID": APP.senderId},
"windows": {}
};
var push = PushNotification.init(params);
push.on('registration', function(data) {
console.log(data);
...
});
push.on('notification', function(data) {
console.log(data);
...
});
push.on('error', function(data) {
console.log(data);
});
registration is successful.
Then I'm trying to send push notification from my server:
public boolean send(String recipient, PushNotification notification) {
LOG.info("sending message :" + recipient);
Message message = new Message.Builder()
.addData("title", notification.getTitle())
.addData("message", notification.getMessage())
.priority(Message.Priority.HIGH)
.build();
try {
Result result = sender.send(message, recipient, RETRIES_COUNT);
LOG.debug("result : " + result);
if (result.getSuccess() != null && result.getSuccess() > 0) {
return true;
}
if (result.getFailure() != null) {
LOG.error("Error: " + result.getErrorCodeName());
}
return false;
} catch (Exception e) {
LOG.error(e);
return false;
}
}
}
I get the following in the log :
16-06-06 20:28:32,868 INFO : [pool-2-thread-1] sending message :k3YUHDkEaR0:APA91bGSc9Wc3dvrS1-6EKBPL4Duu_xZDIujiUZjhkxaaHz3BS3n_ZciuQZLfFiEszXAX4kNUu7Oq0555jI3zzrgIVeNKtD0p63ftH6BPuhOXkCk-ujHlBTi9gSPRXJ6ixd_kTyCfrwS
16-06-06 20:28:33,246 DEBUG: [pool-2-thread-1] result : [ messageId=0:1465234217220513%ca70b4fbf9fd7ecd ]
But on the phone I get nothing. What I did wrong?

Not able to send push notifications in cloud code

I have saved all the user's location in the installation object. And i have another object named locationObject which gets updated when the current user sends his current location.When it does, i want to compare his current location with all the other saved locations and send push notifications to the users who are nearby.This is my code but this does not seem to work.
//this code should run when the locationObject is updated
Parse.Cloud.afterSave("locationObject", function (request) {
var geoPoint = request.object.get("myCurrentLocation");
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.near("significantLocationUpdate", geoPoint);
pushQuery.limit(100);
pushQuery.find({
success: function(results) {
if (results.length > 0) {
//console.log(JSON.stringify(results));
for (i = 0; i < results.length; 1++) {
Parse.Push.send({
where: pushQuery,
data: {
alert: "some user is nearby"
}
}, {
success: function() {
console.log("push was successfull")
},
error: function(error) {
console.log("sending push failed")// Handle error
}
});
}
} else {
console.log("failure");
}
},
error: function (error) {
console.log("error");
}
});
});
This is how i restructured the code.And it works. Thanks to Paulw11
Parse.Cloud.afterSave("locationObject", function (request) {
var geoPoint = request.object.get("myCurrentLocation");
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.near("significantLocationUpdate", geoPoint);
pushQuery.limit(100);
Parse.Push.send({
where: pushQuery,
data: {
alert: "some user is nearby"
}
}, {
success: function() {
console.log("push was successful");
},
error: function(error) {
console.log("sending push failed")// Handle error
}
});
});

Resources