This article complements my other article on “Firebase Notifications”. To test firebase notifications, one can simply use the Firebase Cloud Messaging console on Firebase. But in actual you would notify users when there’s a new email or a new tweet or trigger a reminder or give away free tokens in gaming apps; all-in-all to engage users with your app, to lure them to open or use your app. To be able to achieve this, mostly notifications would be sent using cloud functions or server-side code.

USE CASE

How to write a cloud function to send firebase notifications. Such cloud functions can be set to listen to changes on your Firebase table or can be triggered using HTTP. In this article, I will share with you how to write a cloud function and execute it as an HTTP function.

IMPLEMENTATION

Below are the steps to set up, write, deploy, and test your cloud function.

Step 1: Setup Firebase Cloud Messaging API

  1. A good starting point is of course the firebase documentation.
  2. Make sure you have added your project on firebase. (step 1 in the above documentation)
  3. Install node.js.

_> _You would need node NVM (Node Version Manager) only if you want to use multiple versions of node, else you don’t. (I did not install NVM on Windows but was needed on Mac. Refer #7 below)

>_ NPM (Node Package Manager) is installed by default with Node.js._

4. Add the below line to the PATH variable in “bash. profile” file in your home directory.

export PATH=”$PATH:/user/local/bin”

5. You can check the version of node installed using the below command in command prompt

node — version (You need version 6 and above)

npm -v (you need 5 or later)

You need node version 6 and above, npm version 5 or above.

6. I could not install Firebase CLI because of some permission issues on Mac. The same has been mentioned in this firebase google tutorial and the solution was to reinstall Node.js using NVM.

7. From the terminal install NVM. The link has a curl command to download and install.

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash

8. Close & reopen the terminal to verify the installation. Use the command below, the output should be “nvm”

command -v nvm

9. Re-install Node.js using the below command,

nvm install node

10. Then Use the below command to install Firebase CLI (Command Line Interface)

npm install -g firebase -tools

11. Type the below command to check the version of firebase

firebase — version

12. Login into firebase from the command. Use the below command

firebase login

Type”y” to the question “Allow Firebase to collect anonymous CLI usage and error reporting information? (Y/n)”

Chrome browser will open up asking you to pick the account associated on Google FireBase.

Click “Allow” on the disclaimer screen that follows.

You should see a “Firebase Login Successful” screen on the browser as well as on the command prompt.

13. Initialize your project on Firebase by using the below command. Make sure you are in the directory where you want your firebase app will be initialized. Eg: …/sandbox/firebase.

#firebasenotifications #firebase-cloud-functions #flutter-app-development #flutter #firebase

Notify Users, Part 2 — Firebase Cloud Functions
1.40 GEEK