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

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.

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 notifications

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.

Can I create triggers with Admin SDK in my own backend?

I am new to Firebase and learning it. With Cloud Functions you can create trigger listeners to events that happen in Firestore (firestore events) or Authentication (auth events). Can I create listeners similarly with the Admin SDK of Firestore or Authentication in my own nodejs environment?
No, Cloud Functions triggers only work on the Cloud Functions backend, as they depend heavily on Google Cloud infrastructure to work efficiently. There is no exact equivalent for other environments. You can certainly use the provided nodejs Firestore SDK to set up a document or query listener, just like web and mobile clients, but it won't behave like a Cloud Functions trigger.

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.

Integrate Firebase Database with FCM

Is there a way to integrate the Firebase real time database with the newly introduced Firebase Cloud Messaging services ?
I am building an app which uses the the Firebase realtime database for storing data. I want to notify my users using push notifications for every new insert in the database. I came across tutorials which teach you how to use FCM, but most of them require you to set up your own server for storing tokens.
Is there a serverless way of integrating Firebase DB with FCM ?
Sending messages to devices based on inserts into the Firebase Database will require you to run a trusted process, typically on an app server that you control. This trusted process listens to the database changes and then calls Firebase Cloud Messaging to send the messages.
For an example of how to send messaging from a node.js script, see my answer here: How to send Firebase Cloud Messaging from a node server?

Resources