Do i run functions in c# or google cloud platform?

I'm looking to send a notification to the user when something changes in my firebase realtime database. I've set up FCM in my Xamarin.Android application. I'm pretty new to Firebase though so I'm a bit confused as to how Database Triggers work. (https://firebase.google.com/docs/functions/database-events)

I'm confused as to whether the functionality for this, ie "OnCreate" or "OnWrite" is written on the Google Cloud Platform or my C# code?

To put it another way, do i either:

  1. Write the code in TypeScript to detect changes in my database then push a notification through FCM, or
  2. Write the code in C# to detect changes, like this: How to get notification in xamarin forms on firebase data change?

Thanks.

Answers 1

  • To send messages to devices through FCM, you must specify the FCM server* key in your code. As its name implies, this key should only be used in server-side code, or in an otherwise trusted environment. The reason for this is that anyone who has the FCM server key can send whatever message they want to all of your users. If you were to include this key in your client-side C# code, a malicious user can find it and you're putting your users at risk.

    Cloud Functions allow you to run small snippets of Node.js code on Google Cloud's servers that run in response to events within your Cloud/Firebase project, such as when a document is written to Cloud Firestore. This code runs on Google's servers, so are the perfect spot to call the FCM API to send notifications.

    That's also why you'll see many examples of this approach. But if you have another server-side platform already, it is also totally fine to call the FCM API to send notifications from there.


Related Articles