Parse Sending push notification to specific user from Cloud code - javascript

I want to send a push notification from parse cloud code to a specific user.
So i have created a user section in installation class of my parse table and i save the user object id there so i can target the user by id and send push from cloud code.
https://www.dropbox.com/s/dvedyza4bz3z00j/userObjec.PNG?dl=0
From parse.com it is very simple to do, i did it like this with condition
https://www.dropbox.com/s/1mb3pb2izb0jlj9/pushs.PNG?dl=0
But what i want to do is to send a push notification when a user adds new object in the class my class is "Ticket".
This class has ACL enabled.
What i want to do is very simple send push to the user which created the object through cloud code
Here is my cloud code
Parse.Cloud.afterSave("Ticket", function(request) {
var pushQuery = new Parse.Query(Parse.Installation);
Parse.Push.send({
where: pushQuery,
data: {
alert: "New Ticket Added",
sound: "default"
}
},{
success: function(){
response.success('true');
},
error: function (error) {
response.error(error);
}
});
});
This code sends push to all users.
Please help

This can be a solution:
Parse.Cloud.afterSave( "Ticket", function(request) {
//Get value from Ticket Object
var username = request.object.get("username");
//Set push query
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("username",username);
//Send Push message
Parse.Push.send({
where: pushQuery,
data: {
alert: "New Ticket Added",
sound: "default"
}
},{
success: function(){
response.success('true');
},
error: function (error) {
response.error(error);
}
});
});

You have to add a filter to the pushQuery for the user created the object.

Related

Parse Server Push Notification to Specific user issue

I need to send a Push notification after a class is updated by the user, the push notification is working from the Dashboard.
I added this code to my Cloud Code, i can see the logs with the success message, but the notification is not being sent to the user, on the past notification, it shows that was sent, but the audience is Everyone, but pushs sent is 0, this is my Cloud Code
Parse.Cloud.afterSave("Class", function(request, response) {
var inObject = request.object;
if (inObject.get("isLost")){
console.log("inObject is lost");
var targetUser = new Parse.User();
targetUser.id = inObject.get("UserID");
var query = new Parse.Query(Parse.Installation);
query.equalTo("user", targetUser);
Parse.Push.send({
where: query,
data: {
alert: "InObject",
badge: "Increment",
message: "Testando"
}
}, {
success: function() {
// Push was successful
console.log("success");
},
error: function(error) {
// Handle error
console.log("error: "+error.code+" message "+error.message);
throw "Got an error " + error.code + " : " + error.message;
},
useMasterKey: true
});
}
});
I tried to do the double query as the post link, but no success, i loged the user ID and is the same as show on my Installation in the Dashboard.
If i click the Push notification for more details this is the Target that it shows:
Target details from push
Edit
Save code for that class(it's in java):
ParseObject object = new ParseObject(Class.class.getSimpleName());
object.put(NAME, getName());
object.put(UUID, getUUID());
object.put(ICON_ID, getIconID());
object.put(ADDRESS, getAddress());
object.put(ID1, iD1.toString());
object.put(ID2, iD2.toString());
object.put(ID3, iD3.toString());
object.put(IS_LOST, getIsLost());
object.put(USER, parseUserID);
object.put(DISTANCE, getDistance());
object.put(LATITUDE, getLatitude());
object.put(LONGITUDE, getLongitude());
object.saveEventually();
The parseID is being generated by the ParseUser.getCurrentUser().getObjectID();
So, after trying this in a lot of ways, i ended up creating a relation between my object and the ParseUser object, this solved the issue, still i don't know why can't i just save the UserID, but my problem is solved now.

Form to post on website and send push to mobile devices Parse

I'm looking to create an html/javascript based form that posts an article to my website and at the same time will make a push notification through my iOS and future android app. I'd rather do an all in one rather than posting it on the website then doing the push through Parse's website.
I'm just not exactly sure how to go about it if it's even possible.
Yes its possible you have to make the HTML form. if your form name is as follows
<form id="postArticle">
<input type="text" it="articleID" name="article">
<input type="submit" value="Submit">
</form>
you can JS code to submit data and save data in parse and send push notifications to selected devices
$("#postArticle").submit(function(e) {
var TestObject = Parse.Object.extend("Articles");
var requestObj = new TestObject();
requestObj.set('startTime',$("#articleID").val();
requestObj.save({
success: function(object) {
//on success of inserting data to parse we will send notifications to device
Parse.Push.send({
channels: "yourChannel name",
deviceType:"ios" , //device types android or iOS
data: {
alert: "New Articles submitted",
badge: "Increment",
sound: "default",
}
}, {
success: function() {
// Push was successful
alert("Your request has been send");
},
error: function(error) {
// Handle error
//console.log(error);
alert(error.message);
}
});
},
error:function(error){
alert(error);
})
}

Quickblox one to one chat history not working

I am using Javascript SDK for 1-1 chat in Quickblox, but somehow I am not able to store the chat history.
I am following this link.
var message = {
body: text,
type: 'chat',
extension: {
nick: chatUser.email,
// token from session is set on window object
token: window.token,
// MyChat is a custom class_name
class_name: 'MyChat'
}
};
I am passing the class_name and token since I saw the android sdk following the same pattern.
private Message createMsgWithAdditionalInfo(int userId, String body, Map<?, ?> addinfoParams){
Message message = new Message(QBChatUtils.getChatLoginFull(userId), Message.Type.chat);
String addInfo = ToStringHelper.toString(addinfoParams, "", Consts.ESCAPED_AMPERSAND);
//
MessageExtension messageExtension = new MessageExtension(Consts.QB_INFO, "");
try {
messageExtension.setValue("token", QBAuth.getBaseService().getToken());
messageExtension.setValue("class_name", "ChatMessage");
messageExtension.setValue("additional", addInfo);
} catch (BaseServiceException e) {
e.printStackTrace();
}
message.addExtension(messageExtension);
message.setBody(body);
return message;
}
Also in instructions I see this.
<message id="123" type="chat" to="291-92#chat.quickblox.com" from="292-92#chat.quickblox.com"><body>Hi there</body><quickblox xmlns=""><token>848d4bf336d99532deff6bf7c8bb4b7e7b1a71f9</token><class_name>ChatMessage</class_name></quickblox></message>
Here also I see token & class passed so I am guessing how to I structure in my message object so that I get it to work.
The way I have created chatService is this.
chatService = new QBChat(params);
// to send message I am using sendMessage function
// message object is same as defined above.
chatService.sendMessage(recipientID, message);
This is an old and deprecated method to store chat history
Look at this guide http://quickblox.com/developers/Chat#Server-side_chat_history
var msg = {
body: "Hey",
extension: {
save_to_history: 1
},
senderId: currentUser.id,
};
You have to use 'save_to_history' to store a message
You can use this branch as a basis
https://github.com/QuickBlox/quickblox-javascript-sdk/tree/develop.chat/samples/chat

Adding data to Users and then saving them using Parse Cloud Code

I'm trying to make a cloud function which saves the sender's objectId and username as an array, inside the array "request", for the target and have the target's objectId and username saved as an array, in the array "pending" for the sender.
Parse.Cloud.define("newGameRequest", function(request, response) {//A
// Get the user who called the function
var user = request.user;
var target;
var query = new Parse.Query(Parse.User);
query.get(request.params.friendId, {
success: function(object) {
var target = object;
var friendInfo = [target.objectId, target.username];
var userInfo = [user.objectId, user.username];
user.add("pending",friendInfo);
target.add("request",userInfo);
Parse.Object.saveAll([user, target], { useMasterKey: true });
response.success("Success");
},
error: function(object, error) {
response.error(error);
}
});
});
Looking in the data browser shows that the arrays for each respective user were saved, but saved with null values only ([[null,null]] for both).
The call is from an iOS device and is the following:
[PFCloud callFunctionInBackground:#"newGameRequest"
withParameters:#{#"friendId": self.friend.objectId}
block:^(NSString *result, NSError *error) {
if (!error) {
}
else {
NSLog(#"%#",result);
}
}];
self.friend.objectId has been tested and is the right result.
What is the issue with my cloud code?
I'm an idiot.
getting the object Id of user is the like the following:
user.id
and getting the username is done like this:
user.getUsername()

Creating a Stripe Customer Using Parse

I'm trying to create a stripe customer using parse but can't seem to get the customer.id value from the response.
var newCustomer;
Stripe.Customers.create(
card: request.params.cardToken,
email: request.params.email
//These values are a success but below is where I have an issue.
).then(function(customer){
newCustomer = customer.id;
//newCustomer never gets set to the id, it's always undefined.
}, function(error){
});
Here is a way to create a new customer in parse cloud code using parse stripe module api.
Parse Reference: http://parse.com/docs/js/symbols/Stripe.Customers.html
Be sure you have stored your publish api key
var Stripe = require('stripe');
Stripe.initialize('sk_test_***************');
Parse.Cloud.define("createCustomer", function(request, response) {
Stripe.Customers.create({
account_balance: 0,
email: request.params.email,
description: 'new stripe user',
metadata: {
name: request.params.name,
userId: request.params.objectId, // e.g PFUser object ID
createWithCard: false
}
}, {
success: function(httpResponse) {
response.success(customerId); // return customerId
},
error: function(httpResponse) {
console.log(httpResponse);
response.error("Cannot create a new customer.");
}
});
});

Categories