Cloud Functions for Firebase — Device to Device Push Notification

Sinan Yılmaz
AndroidPub
Published in
3 min readApr 8, 2017

--

Hello everyone! Google has organized the Cloud Next event on March 8–10. And Cloud Functions for Firebase was announced!

Cloud Functions for Firebase lets you create functions that are triggered by Firebase products, such as changes to data in the Realtime Database, uploads to Cloud Storage, new user sign ups via Authentication, and conversion events in Analytics.

Cloud Functions is a hosted, private, and scalable Node.js environment where you can run JavaScript code.

In this blog, let’s look at push notification from device to device using Cloud Functions for Firebase on Android Application.

Get Started

First, create an Android project and connect to firebase account. Notifications will get the subscribed devices.We will do this using Firebase Topic Messages. We need to Firebase Messaging SDK.

//Add the SDK
dependencies {
compile 'com.google.firebase:firebase-messaging:10.0.1'
}

We will use a service for our notifications.Edit the app manifest for this.

<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

And java file of Service.

We added the FCM to the project. Now, how do we enable subscribing? It’s here.

//Add to Activity
FirebaseMessaging.getInstance().subscribeToTopic("pushNotifications");

And, unsubscribing?

//Add to Activity
FirebaseMessaging.getInstance().unsubscribeFromTopic("pushNotifications");

Okay now, let’s look at the cloud functions.

The Firebase CLI requires Node.js and npm, which you can install by following the instructions on https://nodejs.org/. Installing Node.js also installs npm.

The Firebase CLI requires Node.js version 6.3.1 or greater.

Is everything ready? Lets start!

//Install Firebase CLI
npm install -g firebase-tools

If the command fails, you may need to change npm permissions.

To initialize your project:

  1. Run firebase login to log in via the browser and authenticate the firebase tool.
  2. Go to your Firebase project directory.
  3. Run firebase init functions.

After completed succesfully, open “index.js” in the folder of functions.

//import firebase functions modules
const functions = require('firebase-functions');
//import admin module
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

Next one, Realtime Database Triggers!

You create a new function for Realtime Database events with functions.database. To control when your function were trigger, call ref(path). This method directs your function to handle writes at a certain path within your database:

functions.database.ref('/messages/{pushId}').onWrite( event => {
...
}

The Realtime Database supports the onWrite() event, which triggers anytime data is created, destroyed, or changed in a specified database location.

The notification configuration is as follows

/* Grab the current value of what was written to the Realtime
Database.*/
var valueObject = event.data.val();


const payload = {
notification: {
title:valueObject.name,
body: valueObject.text || valueObject.photoUrl,
sound: "default"
},
};

The title of the notification here is the name key in the path specified in the database.Notification body, text key (if send a photo,photoUrl key) in same path. Because my database structure is as follows

And, my index.js file completed.

The functions are triggered when the specified path changes.Notifications are sent to subscribed devices.

Now, you can completing cloud functions command with firebase deploy . After deploy, we can see the function and logs in the console.

We can now test notifications!

Github repo of full project

Thank you for reading! If you like this blog, you can recommend.

Resource: https://firebase.google.com

--

--