Is it a way to get notification with CKSubscription that a user started to use app? - cloudkit

I need to know when a user started using app. I know I can set up CKSubscription for arbitrary record type, but it seems that for Users it does not work.
CKSubscription(recordType: "Users", predicate: NSPredicate(value: true), options: .FiresOnRecordCreation | .FiresOnRecordUpdate | .FiresOnRecordDeletion)

Users is a special recordType. As you can see in the CloudKit dashboard it also has a separate item in the menu. You won't be able to access it from the Public Data Default Zone. The easiest way around this is to create a recordType of your own when the app starts up. For instance you could make a UserSettings recordType. Then you can create a subscription for that. You do have to be aware that then everybody will receive notifications for every new user. Of course you don't want to handle thousands of notifications per day? :)

Related

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

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

How to Implement "Your contact just joined app" feature

I am building a mobile app (in flutter firebase, but the answer does not have to be firebase specific). And I would like to implement a feature that notifies users whenever anyone from their contact list joins the app. This seems like a very EXPENSIVE feature.
At the top of my head, I see a lambda/cloud function that is triggered everytime a user joins and then searches a database of users and their respective contacts for the existence of the new user's phone number. To me, this solution does not scale well for two reasons: if the number of total users is in the millions and the number of users joining simultaneously is a lot.
My better solution is to get the user's contacts upon joining and then searching a database of current users contacts for any of the phone numbers of the newly joined user.
Is there a solution better than the second one? If so, what is it? If the second solution is standard, what kind of backend storage mechanism provides the best search and retrieval time for a database of users and their respective contacts?
In the case of large users ill not do first solution because that may slow the sign up process instead i will creat a cron job that runs at a specific time or periodically it will get the list of the latest users signed up that day or that hour whatever you prefer then that cron will check the new user if related to any user in the databases and send a notification right away, or a better solution create a temporary table in a database in another server insert the notification informations into the other server, creat another cron job in the second server it will run at a specific time to sendthe notification

Ban a User in a particular firebase firestore group chat for predefined time

I am very new to coding. but i managed to build a group chat app using a low code platform called flutterflow. i managed to spend significant amount of time on it and was able to build a public group chat app except few functionalities. I am hoping to find help from here. for the following questions.
I have chat mods appointed on a group level. like if you create a group, you are a founder and you can assign mods to that perticular group chat. now i want these mods to be able to ban a user in that particular group chat.
I have tried created a subcollection in groups called "banned user" and created two feilds. one is "banned users" document reference to users. and another is "banned_till" to record a time stamp until the user gets banned.
Problem with this is when i ban a user twice, it creates two documents in the user reference with the same user. and two documents has different "banned_till" times. which one it is supposed to pick?
i tried to do this and put a conditional visibility to the chat that "if current time is less than or equal to banned_till time" it wont let user type in the textfield to chat. but this is giving me gray screen.
I am very new to this. any help would be appreciated.
there is specific way to do so. you have to set custom logic. like save all banned users in a firebase database object with thier max time and procced next.

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.

Firebase Triggers / PubSub / MongoDB

I am using Cloud Functions to handle events, I have some topics that trigger an event such as a write to my document store.
What I would like to do is on a change to my doc store, notify any users interested in that change that there is fresh data.
For example, a news feed. If User A triggers an activity that is written to the store, User B should receive an update that such an activity has taken place, either an instruction to poll new data or just the new event object.
I do not want to use the Firebase Realtime DB as it is a requirement to use MongoDB, however, I believe as Firebase can hook into the events on Google Cloud Functions, I should be able to trigger this still using events?
Is this correct? So far in Firebase I can only find triggers around Realtime DB..
I can sort of achieve this with FCM, however it feels like this is more aimed at giving the user notifications as it requires the user to accept them in the browser, where I want to notify the app itself, not the user.
I do not want to use the Firebase Realtime DB as it is a requirement to use MongoDB, however, I believe as Firebase can hook into the events on Google Cloud Functions, I should be able to trigger this still using events?
Yes, but
Using only Firebase storage triggers is a bit tricky thing to do, as Firebase gives you a Single bucket to work on, so if there is a change in FirebaseStorage(FS) the event will be triggered regardless of in which folder the file is stored.
I can sort of achieve this with FCM, however it feels like this is more aimed at giving the user notifications as it requires the user to accept them in the browser, where I want to notify the app itself, not the user.
Yes, you can use FCM for it, but you will need to store FCM Token of every device in some kind of database(If you dont want to store in Firebase Database) to send it to users.
I want to notify the app itself, not the user.
You can pass payload in FCM through data instead of notification which will be received in onMessageReceived and then you can decide what you want to do with it.
i.e.
var payload = {
data: {
score: "850",
time: "2:45"
}
};