Firebase chat Push Notifications

10,603

Solution 1

Hope someone finds this useful, it helped in my case.

Currently sending push notifications from device to device isn't possible with solution Firebase offers. If you want that type of thing, you should try OneSignal which is free and very easy to implement.

Solution 2

There are two features in the new Firebase to send notifications to users:

  • Firebase Cloud Messaging - the next generation of Google Cloud Messaging
  • Firebase Notifications - built on top of Cloud Messaging, which allow you to easily send notifications to users through the Firebase Console

To send messages with Cloud Messaging, you need to specify an Authorization Key in a request. Since knowing this key allows sending of messages on your behalf, you'll want to limit access to it to trusted processes. This indeed can be a server, but any trusted process is fine.

Solution 3

(iOS) A little late; but perhaps can help anyone in the future with this.

I recently had this issue, and my solution was to utilize the device group method.

Ergo:

  1. Would create a device group with the registration_ids (or array of device ids for the push)

  2. Would then send the push to said device group

  3. Delete device group

See: https://firebase.google.com/docs/cloud-messaging/ios/device-group

Share:
10,603
ZassX
Author by

ZassX

Mobile developer

Updated on June 26, 2022

Comments

  • ZassX
    ZassX almost 2 years

    I am developing a chat app using Firebase as backend. It is required that on every new message receiver gets a push notification saying that he got a new message. This is first time I am doing something like this so I have a question:

    Since we do not use our own server for this, do we need a 3rd party provider (like Batch for example) for handling push notifications? If I am not mistaken I think Firebase does not support sending push notifications like this but only ones you send from console.

    Any help would be great.

  • ZassX
    ZassX almost 8 years
    So this means that without server (which we don't have) we have to use third party service like Batch?
  • Suyash
    Suyash almost 8 years
    The FCM_KEY is mean't to be a secret. Never embed it directly into your app.
  • MJQZ1347
    MJQZ1347 almost 8 years
    @ZassX: May I ask if and how solved the problem? Thanks in advance.
  • ZassX
    ZassX almost 8 years
    @MJQZ1347 just posted solution that worked for me, please see my answer.
  • MJQZ1347
    MJQZ1347 almost 8 years
    Thanks, will look into it!
  • Erik
    Erik almost 8 years
    What do you mean by it can be a server? Is there any other way around instead of getting a server?
  • Frank van Puffelen
    Frank van Puffelen almost 8 years
    You can run trusted processes on any trusted hardware. I often run them on my laptop, since many of my trusted processes don't need to be constantly available. Others I run on tiny always-on systems, like a Raspberry Pi at home, because they need better online availability than my laptop. Yet other processes I run on (micro) GCE/EC2 instances. All of these have one thing in common: they are running on hardware that is not publicly accessible.
  • Erik
    Erik almost 8 years
    Oh I see. As for the push notifications, how much hardware resources are required to run such a process to delegate push notifications to Firebase from a client app? Can this be done by said Raspberry Pi at home, or should I find a suitable web hotel? If the latter one is preferred, do you have any recommendations as to which provider would be appropriate?
  • Frank van Puffelen
    Frank van Puffelen almost 8 years
    You'll be listening to some endpoints (either something you draft or for example the Firebase Database) and calling a HTTP endpoint to send each message. I'd definitely start with something small, unless you expect a significant message volume straight away.
  • n6xej
    n6xej almost 8 years
    This should be the accepted answer for device to device push messages, like you need in a messaging app.
  • Jason
    Jason almost 8 years
    This is EXACTLY what i needed. Been searching for days. Thanks.
  • Sanjith Bravo Dastan
    Sanjith Bravo Dastan over 7 years
    How it is possible please explain. I don't find anything with OneSignal.
  • Rajan Maharjan
    Rajan Maharjan over 7 years
    I also don't find any related documentations for device to device to push with firebase Realtime database. Please let us know reference to such. Thanks in advance.
  • ZassX
    ZassX over 7 years
    @RajanMaharjan it is not related to Firebase. When you are executing an action in firebase (saving, updating) and want to send notification about it to another user, you must add some OneSignal code to handle notifications. Please check OneSignal documentation. documentation.onesignal.com/docs/…
  • Mohammad Zaid Pathan
    Mohammad Zaid Pathan over 7 years
    @FrankvanPuffelen Any updates on this thread after Berlin event?
  • user2875289
    user2875289 over 7 years
    @ZassX How do you send a device to device notification using OneSignal? Say Bob send a chat message to Mary. We want to send this notification to Mary programmatically. We do not want to send to other users, just Mary. We can do this by specifying Mary's "player_id" in OneSignal API. But how do Bob get Mary's "player_id"? Does Bob get it from Firebase Database where Mary saved its "player_id"? Thanks.
  • ZassX
    ZassX over 7 years
    @user2875289 You can specify extra data / info when posting notification. It is simple JSON so you could add a property to JSON like "senderID" : MARY_PLAYER_ID and then catch it on Bobs side. For more you can check OneSignal postNotification function documentation documentation.onesignal.com/docs/…
  • user2875289
    user2875289 over 7 years
    @ZassX Thanks for your replay. Sorry but I have faced a strange problem implementing OneSignal, if you can take a look stackoverflow.com/questions/41755921/…
  • veeresh kumbar
    veeresh kumbar about 7 years
    @FrankvanPuffelen i need a help on push notification services by firebase, my question is i am updating the firebase database whenever my app user is online and is authenticated with fb , so whenever my fb friends come online to my app, i want to send the notification to all other fb friends that "user" is online