My database structure looks like this
{
"user_notifications": {
"user1": {
"location1": {
"name": "Nice location",
"postCounter": "6",
"newPosts": "true"
},
"location2": {
"name": "Another location",
"postCounter": "0",
"newPosts": "false"
}
}
},
"location_users": {
"location1": {
"user1": "true",
"user2": "true"
},
"location2": {
"user1": "true"
}
}
}
I think that new notification for any new action in location for every user will be bad idea because it will be like 20 megabytes of data for a location with 100,000 subscribers. So I want to store just one instance of location notification for one user that will be containing location name, new posts counter and boolean value that will be informing user about new posts when he enter his notifications screen.
The question is: How to update every user notification about certain location when new post will be added to this location. I mean raise the post counter, set newPosts value to true.
I found Frank van Puffelen's post (https://stackoverflow.com/a/30699277/8437130) about Transactional updates, modified it and I've got something like this:
function updateNotifications(ref, locationId) {
var updates = {};
var query = ref.child('location_users'+'/'+locationId);
query.once('value', function(snapshot) {
snapshot.forEach(function(userSnapshot) {
updates['user_notifications/'+userSnapshot.(don't know how to get user id from there)+'/'+locationId+'/postCounter'] = (don't know how to raise the counter by 1);
updates['user_notifications/'+userSnapshot.(don't know how to get user id from there)+'/'+locationId+'/newPosts'] = true;
})
ref.update(updates);
});
}
Still I don't know if that is the right way and don't know how to make it work.