How to minimize Firestore data? Mutiple fields with boolean type or one Map field type? - flutter

I'm making a social media app using flutter and firebase.
I'm making push notifications to alert users on certain actions like somebody is following or send a comment or press like button. Also, there will be a push notification settings page.
I want to let users to choose to turn on and off push notifications on certain actions. For example, a user only wants to get push notification when someone is following on him. Then, the user can just turn off all the other push notifications except following push notification.
What I did was make every fields of every push notifications on certain actions.
For example, I made 4 data fields in user's firebase document for each certain push notifications.
I have a different idea which is making one field of Map type data that contains multiple push notifications like this.
Which way would be the better idea to minimize the size of Firestore data and reduce the cost?
Thank you so much for reading this and if you have other ideas, please let me know :)

Better have different fields - anyway you will receive one dataset, and you don't need to make another "dataset" in this (in same situation you will receive more long response because of additional string markup for inner json)
Also, if you have different fields you can more efficient query it if you need, with less data exchange between client and server

Related

Flutter - If get this keyword in the title from a push notification do this action

I'd like to be able to do actions when receiving specific keywords in the title of notifications coming from specific apps. I've read about the notifications package and notificationlistener. The first one says that it can just give me the app name and timestamp, but I don't understand if it would fit my needs, and the second one (notificationlistener) I'm not sure it could solve my issue.
How can I do what I want? Let's say I receive a notification containing in the title "Breaking News" and I want to copy paste the title and the notification content in my app automatically every time I receive that, how do I do that?
I'm very new to Flutter, so, please explain easily what I should do and if it's feasible in Flutter without recurring to native code.

Firebase cloud functions chess game Swift

I am making a chess app which has a Firebase backend, i am using cloud firestore and cloud functions. basically i am using node 8 to avoid billing issues on cloud functions, i can call and trigger, but i can't wrap my head about how to make an action happen on another device instead of on my own.
After authenticating and logging in, the user gets into a lobby which is a tableViewController and there are shown only the users that are currently logged in.
What i want to achieve is by tapping on a certain row the user that got the tap on it gets an alert shown for him which states whether he accepts the challenge or he declines it. Based on that i proceed to the game or do something else.
The question is though how to make this alert trigger on the other user's device?
Also i've seen some posts that it can be done with snapshotListener on a document, but again i run into the problem of understanding how to trigger the alert on another device. If you've got some other good ideas please share!
Thank you for any feedback!
I think snapshot listeners are really the only way to go. You could use other Firebase services but those aren’t the right tools for the job. Consider creating a collection, maybe call it requests:
[requests]
<userId-userId> (recipientUserId-initiatorUserId)
initiator: string
recipient: string
date: date
Each user has a snapshot listener that listens to this collection where their own userId is equal to recipient. You can add a date field to sort the list by most recent, for example. When a user wants to challenge another user, all they need to do is create a document in this collection:
<userId-userId> (recipientUserId-initiatorUserId)
initiator: myUserId
recipient: theirUserId
date: now
And the recipient's client will instantly see this document.
You could include dressing data in this document, like the initiator's display name or a path to their avatar. But this data may be stale by the time it's rendered so the alternative is to fetch the dressing data from the database using the userId. You could also auto-gen the document ID but if you configure it (like this), it can make operations like deleting simpler. You could also configure the userIds to be alphanumerical so only one request can exist between two users; but if they requested each other at the same time, one would overwrite the other and you'd have to handle that potential edge case. There are lots of things to consider but this is the starting point.

REST API Designs with categories

Let's say there is a SINGLE mobile application which receives various types of notifications (like WhatsApp notifications, Facebook Messenger notifications ,etc). What would be a better REST API structure for this?
/users/test#abc.com/notifications //Gives all the notifications
There is a confusion between the below two formats on how .
/users/test#abc.com/notifications?category=whatsapp,facebook //Gives all the notifications
vs
/users/test#abc.com/notifications/whatsapp //Gives only whatsapp notification
/users/test#abc.com/notifications/facebook //Gives only facebook notification
To access an individual notification resource
/users/test#abc.com/notifications/facebook/{notification-id}
vs
/users/test#abc.com/notifications/{notification-id}
If a resource has a unique ID then it should be directly accessible from that, so in my opinion
/notifications/{id}
Would make most sense. In terms of filtering, this is probably more about preference than anything. Here's what I think would be the most idiomatic approach
/notifications // fetch all notifications
/notifications/facebook // fetch all Facebook messages
/notifications/whatsapp // fetch all WhatsApp messages
/users/{id}/notifications // fetch user notifications
/users/{id}/notifications/facebook // fetch user Facebook notifications
/users/{id}/notifications/whatsapp // fetch user WhatsApp messages
It really depends on how you define a notification resource and the relation with its category type (whatsapp, facebook...).
Non category dependent
If the structure of a notification is not dependent on its category, then you want to access it without any category context:
/users/test#abc.com/notifications/{notification-id}
And you can use the category as a filter to a collection of notifications:
/users/test#abc.com/notifications?category=whatsapp,facebook
category dependent
Otherwise, if a notification is structurally dependent on its category (e.g., if you want to define different actions when you deal with whatsapp notifications than when you deal with facebook notifications), then you might want to distinguish a notification according to its category:
/users/test#abc.com/notifications/whatsapp/{whatsapp-notification-id}
/users/test#abc.com/notifications/facebook/{facebook-notification-id}
In this case, you could have:
/users/test#abc.com/notifications/whatsapp/1
/users/test#abc.com/notifications/facebook/1
That define 2 different notifications (although it uses the same identifier).
Now requesting a collection of this kind of notifications is a bit different than the previous "non category dependent" case.
If you only want to have whatsapp notifications then simply call the category resource does the job:
/users/test#abc.com/notifications/whatsapp
But if you want to search on different categories, then you cannot apply your request to a specific category resource. Indeed, it makes no sense to ask for facebook notifications when you deal with whatsapp ones:
/users/test#abc.com/notifications/whatsapp?category=facebook # weird
One solution would be to make as many requests as there are categories requested:
/users/test#abc.com/notifications/whatsapp
/users/test#abc.com/notifications/facebook
But you will have to merge your results later.
Another solution would be to apply your query directly from
/users/test#abc.com/notifications?category=whatsapp,facebook
But the result will be different than the "non category dependent" case. Indeed, you won't be able to directly have your list of notifications, but a list of categories to access to your list of notifications.

Custom Notification Depending on action

SWIFT 3 / Firebase
app like instagram/Facebook
Notifications
I am working on letting users know when another user e.g. liked their post, made a friend request etc.
I set up everything for cloud messaging with firebase and can send bulk messages at all my devices.
Now I want to introduce notifications which contain the current user, the user who did the action and what happened.
Unfortunately I did not find anything about this despite the general implementing of firebase cloud messaging.
My questions are:
1: Do i have to structure a database (e.g. .child("notifications").child(userId).... ?
2: How do I get the token from the stranger user I address with the notification?
3: How can I make the input variable?
I am happy about all kinds of help!

OneSignal Push Notification: How to set segment filter in code instead in the website

I am trying to use One Signal Push Notification and it is a great app so far. I currently have a question regarding how to set segment filter in the code. So my user would be able to create an event and right after they create this event, all user within certain miles will be notified. One Signal has a cool thing, where it can help track the user's location and send notification base on where the user is. So I used the following code to get the user's location. I can put the user into segments and then filter by giving some inputs on the website. Like the screen shot, but I was wondering how would I achieve this in code? So I don't have to go to the website to write this.
The user interface on One Signal
window.plugins.OneSignal.promptLocation();
According to OneSignal document:
Segments may only be created from the Dashboard, but segments may be referenced from the SDK and API.
There are no limits on the number of filters you may add to a segment or the number of segments you may create in OneSignal.
You can set filters in your code.
Send to Users Based on Filters
Filters are a powerful way to target users, allowing you to use both data that OneSignal has about a user and any Tags your app may send OneSignal.