Flutter notifications - firebase

I'm using firebase as database. Flutter responds when any changes are made in cloud while using app, but will is respond the changes even when the app is in background or killed?
Second: is it possible to create notifications within the flutter app without using Firebase notification feature? Like a new document is added in database and app is suppose to create notification using some specific lib from pubdev.

When the user is not actively using the app, the OS (Android or iOS) allows a very limited amount of network activity from that app. For this reason you'll typically stop receiving updates from Firebase once the app is backgrounded, and you won't receive any updates as soon as it's killed.
If you want to show updates to the user when they're not using the app, that typically takes using Firebase Cloud Messaging. With this scenario you:
Set up custom server-side code that listens to the database. This code can run on your own server, but also for example on Cloud Functions.
When this code detects a change to the data, it sends a message to the relevant user(s) through Firebase Cloud Messaging.
This requires that the app registers itself either in your database (with its FCM token) or for a certain FCM topic to receive updates about.
For more on this, see the Firebase documentation on Cloud Messaging, Cloud Functions, my answer here and the FlutterFire docs.

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.

Flutter live notifications

I am using Firestore as a database for my flutter app. Some actions on my app lead to change/update in Firestore data.
I am using Flutter native notifications which are generated once a button is clicked. However, the data passed to the notification remains constant despite the data having been updated later in the Firestore.
I would like to implement a feature similar to the one present in sport apps where the live score is updated regularly in the notification.
How can I achieve that in my app? Do I have to use Firestore Cloud Messaging? Citing resources would be helpful.
What you want to achieve sounds like an ideal case for Firebase Cloud Messaging to be used. I recommend that you take a look at the FlutterFire documentation for Cloud Messaging both overview and usage, which should give you a good starting point.
Alternatively you can also use a Cloud Function as the host of FCM and only configure your app to receive messages, you can watch this video for insights on how to create the Cloud Function side.

Firebase push notification device to device

I am coding a React Native app and I using Firebase push notification service.
In my app, users can send message for each other and I want to notify them when they get message.
I found one way for native android I can code for react native it is not problem just I want to know if there is better way. I can make post to directly to Firebase service with using Http post.
This is the link which way I found: https://blog.usejournal.com/send-device-to-device-push-notifications-without-server-side-code-238611c143
I want to push notification to specific device without server, is there another way to do this?
Sending a message to a device with Firebase Cloud Messaging requires that you specify the FCM server key. As its name implies, this key is supposed to only be used in trusted environments, as knowing it allows one to send any message they want to all users of the app. For this reason it is not possible to secure send messages directly from one device to another device with FCM.
Instead you will have to run code in a trusted environment, such as your development machine, a server you control, or Cloud Functions. Your client-side application code invokes the server-side code, that ensures the call is authorized, and then calls the FCM API.
For more on this see:
How to send one to one message using Firebase Messaging
How to send device to device messages using Firebase Cloud Messaging?

How do I send notifications from Google Cloud(GCP) via Firebase to an Android app

I know how to send messages from Firebase cloud messaging portal to an android device. But my server runs on Google Cloud, I do gcloud app deploy from my local machine and the app logic gets deployed on Google Cloud. Now, I want to send notifications, based on the data stored as Entities in GCP Datastore, to an Android App.
Notification messages can be sent from Firestore-Cloud Messaging portal to an Android device, if I could harness this Firestore Cloud Messaging API in my GCP logic, then my problem will be solved.
I'm trying to look for any examples or POC's.
you can use Cloud Functions to add such custom functionality...
for example: https://android.jlelse.eu/serverless-notifications-with-cloud-functions-for-firebase-685d7c327cd4
and there are Cloud Datastore Callbacks, which can be used as event triggers. most relevant for Cloud Functions might be the Google Cloud Datastore Node.js Client - in order to connect to the Datastore. here's one of my examples, it is written in AppScript (similar to Node.js), which also connects to a Cloud Datastore, with the service account JSON loaded from Google Drive.
however, in this case the Datastore would need to subsequently trigger an HTTP Trigger or Pub/Sub Trigger and the code behind that trigger could get more data from the Datastore or directly send the Firebase notification.
in Cloud Function there are just triggers for Cloud Storage, while the Pub/Sub (publish/subscribe) triggers can be used for just anything. The Datastore would need to publish whatever event (add/edit/update/delete) - while a Cloud Functions script would need to subscribe these events.
using Firebase as backend might be less effort, because data-change events/triggers are being supported out-of-the-box, without any HTTP interaction or Pub/Sub communication involved.

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.

Resources