Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send push notifications FROM an android app with Firebase?

I want to develop a new feature in my app which lets me send notifications to all users with that app installed. I have been searching and found information on how to send upstream messages to Firebase Cloud Messaging, but found nothing explaining how to directly send notifications to other users.

like image 226
Alejandro Bertinelli Avatar asked Oct 28 '22 22:10

Alejandro Bertinelli


2 Answers

There is a way to do that. You could accomplish that using Firebase database, Cloud Messaging and Node.js. You can find how to do that in this article: https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html

But if you see this as too much complicated you can use some another providers like OneSignal or Back4app which is based on parse platform and pretty much simpler then this example above and also has a free plain. How to setup notifications using Cloud code in Back4app take a look on this example: https://docs.back4app.com/docs/android/push-notification/push-cloud-code/

like image 163
Yupi Avatar answered Nov 09 '22 10:11

Yupi


Option 1 - Send messages directly from Firebase

If you are trying to send out messages to all users of your Android app using Firebase, you should look at the documentation on how to "Send Messages with the Firebase Console".

Option 2 - Send messages from the app to a service; then to Firebase

If you are trying to send a message from your Android app to Firebase and then from firebase to other users, you may be missing a key component of your stack/infrastructure, a web service. The service acts as a middle man between Firebase Cloud Messaging and your app, effectively making the decisions on how to handle messages from the app, and how to send commands to firebase, including who (which devices) to target. You have options for this kind of service:

  • An app server (written in whatever environment you know best or is most appropriate)
  • Some other trusted environment such as Cloud Functions for Firebase

Take a look at the documentation on interacting with the Firebase Cloud Messaging Server for more details.

Once you have that set up, your flow looks more like this:

Android App (Message Sender) -> Message Handling Service -> Firebase Cloud Messaging -> Android App (Targeted Device(s))

Option 3 - Use Upstream Messages

Upstream messages still use a separate app server to handle messages sent by the device. In this case, the app sends an upstream message through FCM to the service, the service then does something with that upstream message and sends back an ACK (acknowledgment) message back to the sending device, so it can handle it with a callback. The app service, when handling the upstream message, could then command firebase to send out push notifications to targeted devices.

Overall, it's likely you have some more steps to complete.

You can also find more directly on the Firebase Cloud Messaging page or in the youtube video giving a general background to FCM.

There's also a video demoing upstream and downstream messages using firebase on youtube called Upstream and Downstream messages using FCM - Android.

like image 34
Jack Hughes Avatar answered Nov 09 '22 12:11

Jack Hughes