Sending Notifications in Firebase Chat app

1,166 views
Skip to first unread message

Maaz Contractor

unread,
Jul 2, 2016, 3:36:35 AM7/2/16
to Firebase Google Group
Hello Guys,

I was looking at the Google IO Firebase "Zero to App" example: https://www.youtube.com/watch?v=xAsvwy1-oxE where they make a chat app using the Realtime Firebase Database.

I am looking to extend this example app and add Notifications so that if a user has the app in the background, he gets a notification for a new message.

This is what I am thinking and would love to get your opinion on this, to see if there is a better way.

1. For Each chat session, we make a unique Id and users in the chat session subscribe to a topic of the form : /topic/chat/<uniqueId>
2. When a user sends a message, it gets added to the Firebase realtime database. 
3. On the server, we listen for messages to the Firebase realtime database and use Firebase Cloud Messaging to send topic message. The message is created with both a 'data' and 'notification' payload. If the app is in the foreground, we essentially want to give control to the app, and in that case, we want it to be a no-op as the Firebase realtime database would have taken care of this. If the app is not in the foreground, we want Firebase to show a display notification.

Is there a better way to achieve this ? Your feedback is highly appreciated.

Thanks,
Maaz

Kato Richardson

unread,
Jul 5, 2016, 1:32:11 PM7/5/16
to Firebase Google Group
Hello Maaz,

Seems like a pretty decent approach. I'd probably try this first. 

However, if the message volume or the noops turn out to be non-performant and you need more scale, you could try something like this as an alternative:
  • Rig up some presence management using the database onDisconnect() or a heartbeat.
  • Create a node server to monitor presence.
  • When users go offline, subscribe them to the topic.
  • When users come back online, unsubscribe them from the topic.
☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/91e4863c-83d5-40f5-9e21-14644e1dffcd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Frank van Puffelen

unread,
Jul 8, 2016, 12:33:12 AM7/8/16
to Firebase Google Group
Hey Maaz,

That's pretty much the first thing I did when we came off stage from our "Zero to app" talk at I/O. ;-)

Slight difference is the specific use-case: I send the notification when the user is @ mentioned in a chat message, so my topics match user nicknames: e.g. /topics/user_puf. 

My notification listener service simply ignores the message when the app is active. In hindsight I probably didn't even need a notification listener, but I didn't know better at the time. :-)

Let us know what you build!

    puf
Reply all
Reply to author
Forward
0 new messages