Flutter push notification using FCM by topic - firebase

hello I'm new to flutter and trying to get push notification in flutter , I have successfully subscribed the user to a topic and sent a notification through the firebase console by topic , what I'm trying to do is to have a button for admins in the app and when they post for a particular topic the users with that topic get the notification I have heard about cloud functions but I don't know exactly where to start ! i hope I made my point ! I have seen some videos that talks about type script but is there is any workaround this ?

The Firebase documentation names notifying the user when something interesting happens as an example use-case. From there:
Developers can use Cloud Functions to keep users engaged and up to date with relevant information about an app. Consider, for example, an app that allows users to follow one another's activities in the app. Each time a user adds themselves as a follower of another user, a write occurs in the Realtime Database. Then this write event could trigger a function to create Firebase Cloud Messaging (FCM) notifications to let the appropriate users know that they have gained new followers.
The function triggers on writes to the Realtime Database path where followers are stored.
The function composes a message to send via FCM.
FCM sends the notification message to the user's device.
To review working code, see Send FCM notifications.

Related

Flutter Notifications with Firebase Realtime Database

I have a Flutter app integrated with a Firebase Realtime Database.
I want a notification (actually an alarm kind of thing if possible), when an item (lets call it alarm) in the database is set to "true". I always see firebase_messaging plugin for notifications, but I am not sure if I'm supposed to use this plugin despite that my app doesn't have anything to do with messaging.
I am totally new to both Flutter and Firebase, can you tell me how to listen to the database even if the app is not running?
By the way, I am currently building the app for only Android, but I want to build it for IOS too in the future.
Thanks.
You can use/write firebase cloud fucntion. Using firebase cloud function you can watch any document/field and try to write trigger logic like if a field is set to true then this cloud function will throw an notification via firebase messaging.
https://firebase.flutter.dev/docs/functions/overview/
When the user is not actively using the app there is no reliable way to continue to listen to changes in the Firebase Realtime Database. To notify the user of changes to the database in that situation, you'll need to listen for those change on an environment that is always on, and then send a message to the user through Firebase Cloud Messaging.
One environment that is always in is Cloud Functions, which is also part of Firebase, and allows you to run small snippets of JavaScript code on Google's servers in response to things that happen in your Firebase project. The documentation of Cloud Functions for Firebase as example of how to notify the user when something interesting happens in the database:
Developers can use Cloud Functions to keep users engaged and up to date with relevant information about an app. Consider, for example, an app that allows users to follow one another's activities in the app. Each time a user adds themselves as a follower of another user, a write occurs in the Realtime Database. Then this write event could trigger a function to create Firebase Cloud Messaging (FCM) notifications to let the appropriate users know that they have gained new followers.
The function triggers on writes to the Realtime Database path where followers are stored.
The function composes a message to send via FCM.
FCM sends the notification message to the user's device.
To review working code, see Send FCM notifications.

Firebase Notification by topic

I am new to firebase. I want to send notification to my list of followers(group of members like instagram).
I created a unique topic for each user and all the followers of a user will subscribe to the topic. when i send a notification to the topic, notification not delivered to all followers consistently.
When User clicks follow button i am subscribing to the topic by
FirebaseMessaging.getInstance().subscribeToTopic(FollowerUserID);
by clicking unfollow i am unsubscribing the topic
FirebaseMessaging.getInstance().unsubscribeFromTopic(FollowerUserID);
I have used Firebase cloud functions for pushing notification to the Topic.
If Firebase Token changed for a user whether we need subscribe the topic again?
I don't know how the subscribe/unsubscribe model works. will anyone explain how its works ?
kindly help me get out of this problem.
Suggest me the best way to send notifications to group of peoples(Followers).
First thing first.
If a firebase user token changes and that token was subscribed to a cloud messaging notification then he will no longer receive any notifications, also if he was subscribed to a topic and that topic changes he won't get any notifications from the new topic
For your business logic, I'd make a cloud messaging topic for each user and whenever someone follows that user I will subscribe that user token to the topic of the followed user.
say user X followed user Y, and user Z followed user X. both Y and Z will subscribe to topic X.
check this link
https://firebase.google.com/docs/cloud-messaging/admin/manage-topic-subscriptions
Firebase Cloud Messaging topics subscribe based on Instance ID, not FCM Token. An Instance ID uniquely identifies an app device instance and does not change unless the user uninstalls the app. If an app instance is subscribed to a topic, it will remain subscribed to it. While FCM Tokens periodically refresh, this does not affect topic subscriptions. There is no need to resubscribe users every time a token refreshes. See the guide for some useful information. You can also see the reference indicating how subscribeToTopic works with the Instance ID.

Is it possible to send messages through Firebase Cloud Messaging (FCM) to offline users?

I'm working on a Progressive Web App (PWA) and I need to send important reminders through push messages. Users should receive them even if they are offline.
Is it possible to use Firebase Cloud Messaging (FCM) for that (maybe preload the messages or something like that?) or do I need to get a different route for offline?
Adapted after AL's comments below and Frank's comment above
It is possible to send "push messages" via Firebase Cloud Messaging (FCM) to a device that is offline, BUT the user will only see the message when the device is online again. If the device is offline it cannot receive immediately any (push) message from the "external world". If you want to trigger some reminders that are immediately visible for a device which is offline, you will have to do it locally, on the device, and not by relying on a push from the "external world".
So, having said that, if you want to use Firebase Cloud Messaging to send messages, you can do it by using Cloud Functions, i.e. from a "trusted environment". Have a look at this official Firebase Cloud Function sample: https://github.com/firebase/functions-samples/tree/master/fcm-notifications
You could trigger this Cloud Function when e.g. a new item is saved in the database (Real Time Database or Firestore), or an existing one is modified/deleted, or a file is uploaded to Storage, etc.

Enabling the notification feature for my firebase app

I have made an app with the use of firebase. It is basically a chat application in which you van send and receive the text and images. I want to add a functionality in it that whenever a user sends a msg, then another user should get a notification . When I try to send a notification through the firebase console, then it is working, but when a user messages through the app, then it is not showing any notification to another user. So, can anyone tell me that how can this functionality be achieved ?Also, provide some sample code to see how things are working
You need to use cloud functions for that:
https://firebase.google.com/docs/functions
Cloud Functions for Firebase lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests
First, you can register the user to a topic:
FirebaseMessaging.getInstance().subscribeToTopic("news");
https://firebase.google.com/docs/cloud-messaging/android/topic-messaging
Then when the message is sent to the database, you can use onWrite() in cloud functions which is a database trigger to be able to send the notification.
https://firebase.google.com/docs/functions/database-events

Firebase notifications security / console activity log

i'm thinking about using Firebase for the notification service that my app needs. However, its important that the process of sending notifications is secure. I want to avoid a notification being sent from any member of the team without permission.
One way could be assigning restricted roles and permissions for the team members. However it also would be good to have any kind of activity log in the console, or have a log of all notifications sent including the name of the user. The console currently allows you to see all notifications sent, but i cannot see who sent it, and also the notification can be deleted from the log.
Any ideas on how to manage this? Which specific roles would you recommend me to use? Is it possible with Firebase?
You could write a function in Firebase which sends notifications, and you have logs on that function. Here is how to write a function which send notifications

Resources