Parse Server Push Notification to Specific user issue - javascript

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.

Related

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

How to send push notification using javascript in parse?

we already implemented push notification concept using android but we are trying to send push notification using web application to mobile
here is my code
function authentication() {
debugger;
Parse.$ = jQuery;
// Initialize Parse with your Parse application javascript keys
Parse.initialize("App key",
"javascript key");
debugger;
var pushQuery = new Parse.Query(Parse.Installation);
debugger;
pushQuery.containedIn("channels", true);
Parse.Push.send({
where: pushQuery,
data: {
alert: "Your push message here!"
}
}, {
success: function() {
debugger;
response.success("pushed");
}, error: function(error) {
reponse.error("didn't push");
debugger;
}
})
We are got error
POST https://api.parse.com/1/push 400 (Bad Request)
Uncaught ReferenceError: reponse is not defined
We followed this link & Docs of parse.com
Plz guide to us

Using Parse User's Master Key to save relations not working

I am attempting to link a School class with a list of users (a School can have many Users, and a User belongs to a School). Per several suggestions, I am using Parse's Cloud Code and Master key to accomplish this.
My issue is that, instead of passing a string parameter or another User, I am linking a School object to the user.
Here is my Cloud Code, which is causing an error when ran and no changes made to Parse's backend:
Parse.Cloud.define('addNewSchoolRelation', function(request, response) {
var userId = request.params.userId,
associatedSchoolObject = request.params.associatedSchoolObject;
var User = Parse.Object.extend('_User'),
user = new User({ objectId: userId });
var relation = user.relation("schoolRelation");
relation.set(associatedSchoolObject);
Parse.Cloud.useMasterKey();
user.save().then(function(user) {
response.success(user);
}, function(error) {
response.error(error)
});
});
And inside my app, this is how I call it:
PFQuery *schoolQuery = [PFQuery queryWithClassName:#"School"];
[schoolQuery whereKey:#"objectId" equalTo:#"FgpHfOGIdC"]; //this is the test one
[schoolQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(#"Error %#", error);
} else {
PFObject *school = [objects firstObject];
[PFCloud callFunctionInBackground:#"addNewSchoolRelation" withParameters:
#{#"userId": #"BHqvZ6PrLl", //testing for now
#"associatedSchoolObject": school,
} //sets that user's newChats
block:^(id object, NSError *error) {
if (error){
NSLog(#"Error %#", error);
} else {
NSLog(#"Saved on remote user");
}
}];
}
}];
I'm pretty sure culprit is in my Javascript code - am I passing the parameter incorrectly? I'm not sure what I'm doing incorrectly and any help is much appreciated, thanks!

Parse Sending push notification to specific user from Cloud code

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.

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()

Categories