Serverless notifications with Cloud Functions for Firebase

Mahmoud AlyuDeen
AndroidPub
Published in
4 min readMar 16, 2017

Sending notifications to users is a vital, and in many cases, a key process to any app. And for a long time, we’ve been lucky enough to have the delivery of notifications carried out by services like FCM and APN.

The thing is.. it required a server. You had to host some code on a cloud platform that would allow it to always be running, code that listens and determines what to send, to which users, and then makes the request to send the actual notification to your user.

Of course, that makes it harder for indie developers to consider including notifications in their apps, they might even disregard certain ideas altogether if it’s centered around notifications.

Enters Cloud Functions for Firebase. Google’s latest, and long awaited addition to the Firebase suite. Cloud Functions lets you write javascript functions and host them on Google Cloud Platform, they can be triggered by events on your Google Cloud, HTTP requests, and.. of course.. Firebase events! Be it a change in the Realtime database, storage, a new user or even an analytics event you’ve defined.

Show me the code!

First, let’s consider a basic and very common use case.. Chat! I made a basic sample of a chat app that uses Firebase Realtime database to store messages.

The idea is that the app itself would take care of writing messages in both the sender’s and receiver’s end.

The whole project is available on Github. Both Android and Functions parts.

But.. but.. you said notifications!

Yes, that was what’s missing. Telling someone they have a message when they’re not using the app.

To breakdown what’s happening here, the function is listening on any write operations on the notifications/messages node, when a new message is written there, it fetches the receiver’s instanceId, displayName and photoUrl from the database and authentication. It then constructs the notification and sends it to the receiver’s token. Finally, it deletes the message from the notifications node.

And Voilà..

Things to keep in mind

A couple of potential issues and some advice I thought to be worth mentioning:

Endless loops

Imagine if the sendNotification function above got triggered by the deletion it performs on the same node to which it listens. You might think: “Oh, that would cause an endless loop”.. You would be right!

Hence, the next point:

Editing and deleting are considered write events

When a function listens to database write events on a node, it gets triggered by editing or deleting objects on that node. If you want to only react to what’s left after all write events are complete, you want to call .current on the event.

const message = event.data.current.val();

Log, log.. and then log some more

With your code running somewhere else, your only clue as to how things are going is the logs you make. So make sure to think in advance and log things that might go wrong, so you have enough information to fix it when they do.

It’s beta

As promising, and seemingly complete Cloud Functions is right now, it was just released last week. They have a warning themselves that they will likely introduce breaking changes in the future. So keep that in mind.

But.. I don’t javascript!

Well, as of 2 days ago, neither did I.. Actually it was hard to get excited when I found out about Cloud Functions as I didn’t have a clue how I would use it. But really, it’s not that hard. You just need a crash course on the syntax, and the samples on Github should get you up and running with the basic use-cases.
And of course, the documentation will have you covered on the rest.

Where to start

The Get started guide should get you up and running with your first Cloud project in no time. Happy coding!

In conclusion

It is my belief that: the best time to be a developer, is right now!
Problems that used to be costly or required a lot of time and practice to tackle are getting to the point of non-existence. Giving us the opportunity to concentrate on the core logic of apps instead. Nothing proves that more than Firebase. With it, one developer can make the app, make its back-end, have it hosted, tested and monetized, for free! So what’s stopping you?

--

--

Mahmoud AlyuDeen
AndroidPub

I am a diehard tech enthusiast, from gaming computers to smartphones and everything in between. I code for fun and for a living and I write every now and then.