Parse Cloud Code to send push notification to group of other users - parse-platform

I have an app where one user can invite other users to join an event by push notification. Let's say when creating an event, the user add other users to this event, then save the event to Parse.
So basically I have an array of user_id and I will call a function from cloud code to push notification to those Id, after saving the event.
1)Will the following Cloud code work?
Parse.Cloud.afterSave( "Event", function(request) {
//Get value from Ticket Object
var ids = request.object.get("inviteeIds");
//Set push query
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.containedIn("objectId",ids);
//Send Push message
Parse.Push.send({
where: pushQuery,
data: {
alert: "New Event Added",
sound: "default"
}
},{
success: function(){
response.success('true');
},
error: function (error) {
response.error(error);
}
});
});
I am not sure if the containedIn function exist or not:
pushQuery.containedIn("objectId",ids);
When I search I only find documentation about equalTo function, e.g:
query.equalTo('injuryReports', true);
2) I also read about Channel, but I still not understand how to apply it in my situation. From the documentation:
Devices start by subscribing to one or more channels, and
notifications can later be sent to these subscribers.
In my case how can I create a Channel and then add ids of friends who I want to invite to this Channel?
If possible, I would like to use Cloud Code rather than pushing from mobile device.

1)Will the following Cloud code work?
Why don't you try it and see for yourself, then come back with the errors, if any? Anyway, there's no response in afterSave. It will return a success regardless of what happens in it.
Otherwise it may work. Try running it.
I am not sure if the containedIn function exist or not:
Parse.Query.containedIn
2) I also read about Channel, but I still not understand how to apply it in my situation
Basically you subscribe to a particular channel in the client. Like this (Android)
ParsePush.subscribeInBackground("channelName");
Then in the Cloud
Parse.Push.send({
channels: channelList,
data: {
// etc
}
});
Obviously you'll need to know the channels you want to target.
You can subscribe multiple users to the same channel (for example you can have a dedicated channel for a particular event) or you can have one channel per user (for example you can name it something like channel_<userId> and only subscribe that user to it). Up to you what you need or what you want.
One last thing...
So basically I have an array of user_id
Keep in mind that objects stored in the database have a limited size. If your object gets too big and has too much data, you won't be able to add any more to it.

Related

ReactiveCrud, how can I delay for a moment without blocking IO before deleting data

So suppose I have created an api for booking hotel room by making a ticket and then I will use the information from the ticket (ex. size, bed etc.) to find available room, but if it could not find any available room I will need to delete this ticket later. So for a better user experience I don't want to suddenly delete the ticket, but I want to change the ticket status to be reject or something else and then delete it after 30second. So here what I have tried.
val ticket = ticketRepostiory
.findById(request.ticketId)
.map { it.copy(status = TicketStatus.REJECT) } // mapping new status
.flatMap { ticketRepostiory.save(it) } // save
.then(Mono.delay(Duration.ofSeconds(30))) // display to user for a 30s.
.flatMap { ticketRepostiory.deleteById(request.ticketId) } // delete it
.block()
notificationService.notify(...etc) // notification service notify status
But I found the problems that this code has blocking the other code for 30s (suppose another user wants to create a new ticket it won't create or save any data to db until 30s.) So how can I delete the data after 30s without blocking other request
So I fixed it now by using .subscribe() method instead of .block()

Sending Private Message ( Socket.IO )

In many Posts or Articles.I often saw something like that.
Client-Side :
socket.emit("shhh!Secrets", {
To : "AlexId",
Message : "Hello World!"
})
Server-Side:
socket.on("shhh!Secrets", (Send) => {
io.in(Send.TO).emit("SO...Secrets", Send.Message)
})
Whatever it is socketId , Specific user socketObj or room base .
What If I change Client Source code and change with others room or socketId then my crazy message will saved to others chat timeline...
First Method
Socket.IO is stateful. So this Smart Socket will not forget who you are in every event call.
lets say user want to join room001
So when Joining a socket to a specific Room,Save RoomId To socket.roomId = "room001"
Then use io.in(socket.roomId).emit("SO...Secrets", "message")
Second Method
Never give a change a client directly send message to specific room.
Server-Side:
socket.on("shhh!Secrets", (Send) => {
// Send message only if the user already joined to this Room
if (Send instanceof Object && socket.rooms[Send.TO] === Send.TO)
io.in(Send.TO).emit("SO...Secrets", Send.Message);
})
Mohammed, of course you can change your client code, but you need to know real userId (AlexId in your example), and it is usually uuid, that is not easy to get... So there is very low chance to do that.
By the way, usually in articles use very simple examples and do not mention security aspects, so be careful with it!

How to fix the user info doesn't shows up on the page

I am following the Presence Channels section in Laravel docs.
1.Authorizing Presence Channels-I created I function to check is user is authorized to access them.
Broadcast::channel('chat', function ($user) {
...
return user_info;
})
2.Joining Presence Channels-They say I must use Echo's join method. So I did.
Echo.join('chat')
.here((users) => {
console.log('hello',users)
this.users = users;
})
.joining((user) => {
console.log('hey you', user)
this.users.push(user);
})
.leaving((user) => {
this.users.splice(this.users.indexOf(user), 1);
})
Here's the part that confuses me. "The data returned by the authorization callback will be made available to the presence channel event listeners in your JavaScript application". I assume that I suppose to have this Javascript. part and it should be an event listener. I just can't understand where should it be and how I must call it. Have it something to do with a function I use when user logged in?
So, help me understand how to implement these 'presence channel event listeners in your JavaScript application.'
"The data returned by the authorization callback will be made available to the presence channel event listeners in your JavaScript application."
https://laravel.com/docs/5.8/broadcasting#authorizing-presence-channels
This means that the data returned by your authorization callback Broadcast::channel(...) which is $user_info will be available to the joining() and leaving() listeners, or any custom listeners, within your JavaScript application.
The currently defined listeners are waiting to hear another user join or leave the chat channel. Therefore, each user must also fire the corresponding events within their own instance of the application.
// join the channel — trigger joining()
Echo.join('chat');
// leave the channel — trigger leaving()
Echo.leave('chat');

Live Queries subscription updates in Parse-server

Is it possible to get subscription alerts for live queries on a single client instead of everyone. I am using the following code to get update alerts for the object 'obj' on class 'class-name'. But the alert message comes on every client( i.e every instance of running app).
let query_add = new Parse.Query("class-name");
let subscription = query_add.subscribe();
subscription.on('update', (obj) => {
alert('object updated');
});
How can I modify this to notify only the single client.

Parse.com - setting up push notifications for single users

I'm working on a hybrid app using Ionic framework with Parse as a back end and I'm struggling to set up push notifications for single users. I'm sending the notifications through Parse's Cloud Code, and I have sending push notifications to all users working okay using the following;
Parse.Cloud.afterSave(
'_User',
function(request, response) {
Parse.Cloud.useMasterKey();
var installationQuery = new Parse.Query(Parse.Installation);
Parse.Push.send(
{
where: installationQuery,
data: {
alert: 'Test notification'
}
}, {
success: function () {
// Push was successful
},
error: function (error) {
// Push failed
}
}
);
}
);
Currently, when a user registers an entry is made into the Installation class (using phonegap-parse-plugin), adding their user ID as a channel. I did this with the intention of adding the channel as a query constraint when determining which users to push notifications to, but adding query.contains('channels', 'user-' + request.user.id); to the above doesn't push any notifications (but reports success in Cloud Code).
I've also read about adding the user as a pointer to the installation class, but that would mean a device could only have one user.
What is the best way to allow me to send push notifications to a single user via Cloud Code? Thank you!
Can you see the results of the Installation query where you add the constraint;
query.contains('channels', 'user-' + request.user.id);
The problem might be the empty result. Check it. One of suggestion to singe push single target is just use the user id. Save the user id in installation table and send push notification based on query constraint where equal to. Hope this helps.
Regards.

Resources