Firestore, FCM, and Cloud functions for a chat app notification system - swift

Using these three tools, I'm trying to think of the best way to notify a user when the other user in a chat sends them a message (using APNs).
Would it be reasonable to store (and always update to newest value) the fcm registration token as a field under the user document in firestore, then in cloud functions, create a trigger to respond to new messages being sent (where each message is a document)? Then in order to determine which device to send it to, we use the registration token field?
Is there a better way to do this?

Related

check weather the user readed data or not in flutter using firestore

I was developing the chat app in flutter using firestore, the users successfully chat one-to-one. but I also want to send the notification to the user that someone sends him/her a message. For that I decided to use the cloud messaging service of firebase, but how can I check that the user read the message or not.
There is nothing built into Cloud Firestore to track which user has read a specific document already.
The two most common approaches that I know of are:
To track for each individual message and each individual user whether they have read it, so by keeping a list of the user IDs in each message document. While this potentially stores a lot of extra data, it is the most direct mapping of your use-case.
In most chat apps though, tracking of what messages each user has read is done as an "up to here". So if the user has read all messages up until a certain one or a certain time, the assumption is that they've also read all the messages before that one/moment.
For this model you can instead just keep a single document ID or timestamp for each user (i.e. readUpTo), instead of having to mark each message for each user that has read it. In your UI, you then determine whether to show each message as unread based on the message ID/timestamp compared to the timestamp of readUpTo.

Device to device push notifications logic Swift IOS Firebase

My goal is to implement a function where, when a user (lets call him User1) sends a message to another user (User2), a notification is sent to User2´s device, so he can see the message right on the lock screen. I have already implemented a function where this works. My only problem now is that when I send the push Notification from User1´s device to User2´s device, I don't know if User2 is on the account that he should receive the message on.
If User2 has logged into a different account and User1 sends him a message, he sees this message, although User2 is in a different account and shouldn't see this message. Is there any way to know if a user logged into a different account and to then block the notification from showing on User2´s device?
Firebase Cloud Messaging has no concept of a user. It only knows about devices, or more explicitly app instances (a specific app on a specific device is an app instance).
If your use-case is based around users, your application logic is making a mapping from a user to their FCM instance ID/IDs. If you want the user to not receive a message anymore on a specific app instance, you need to remove the mapping you made.
The most common way to do this is to remove the mapping when the user signs out from your application on a specific device.
Since this is all rather abstract, I recommend also checking out:
When to register an FCM token for a user
Removing a user from Firebase
How to send FCM messages to a different user
Android: How to handle user logout Firebase Cloud Messaging in 2021
How to get Firebase user id from FCM token? (in admin code on server)

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!

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"
}
};

Real-time notifications with Firebase & Ionic

NOTE : Don't say it's possible duplicate of this, Read through.
Think of a simple TASK Management application which can have n number of users. Say User A can create a task & assign it to User B. I want to show a notification to User B.
I am using Firebase as BAAS and following code-snippet prints on console only when there is a task assigned to User B. To simplfy, Users collection in Firebase has a node called tasksAssignedToThisUser array and callback function will be watching tasksAssignedToThisUser location. Whenever a new task gets added to that array, callback function gets executed. Here is the code snippet:
var rootRef = firebase.database().ref();
rootRef.child('users').child(loggedInUserID).child('tasksAssignedToThisUser')
.on('child_added', function (newMessageSnapshot) {
console.log('Someone assigned a task to this user.');
console.log(newMessageSnapshot.val);
});
Everything works fine until here.
What I have understood by reading Firebase docs & other post's on SO about Push notifications or Firebase Cloud Messaging is:
Firebase Notifications lets users easily send messages to reengage and
retain users, foster app growth, and support marketing campaigns.
Before you can write client apps that use Firebase Cloud Messaging,
you must have an app server that meets the following criteria:
...
You'll need to decide which FCM connection server protocol(s) you want
to use to enable your app server to interact with FCM connection
servers. Note that if you want to use upstream messaging from your
client applications, you must use XMPP. For a more detailed discussion
of this, see Choosing an FCM Connection Server Protocol.
This is definitely not what I want and maintaining a server for sending push notifications makes sense when bulk messages needs to be pushed to large number of devices. So I didn't name my question as Real Time Push Notifications.
I see a function being executed, which meets my use-case and I just want app to display a notification to the user in his mobile system tray on that callback function execution.
I am using Ionic 1 for developing app, completely new to Firebase and Ionic too. If there is a simple serverless approach that Firebase itself provides which can suffice my requirement, then I would be more than happy to hear about it.