Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send user to user notifications with Firebase programmatically?

The Firebase Console allows us to send notifications to single users, groups, users subscribed to topics or to the entire user base. Is there any code that allows us to directly send notifications in the same way, but programmatically?

For example, if I had a list of users (containing their Firebase UIDs), if I click on one of the users, could I send a notification to that user through Firebase the same way the console allows us to send a notification through the console?

  1. This Firebase Blog uses the Google App Engine Flexible Environment to actually send the notifications, but it requires a free trial and costs money.
  2. This Quickstart doesn't really show how to send user to user notifications. It focuses on the subscription-based notifications, but this isn't really what I need.

Is there any good way of doing this with Java/Kotlin and the Firebase API?

like image 905
Darryl Johnson Avatar asked Apr 22 '18 21:04

Darryl Johnson


1 Answers

FCM doesn't support you sending message directly from Android app to Android app. However, this is a workaround to solve your problem:

  1. Create a firebase cloud function. Listen a special path in firebase database
  2. Android app 1 push a data (maybe Android app 2 ID) to this path
  3. Firebase cloud function process data, determine who to send notification (Android app 2).
  4. Firebase cloud function push notification to Android app 2.

ref: https://firebase.google.com/docs/functions/use-cases#notify_users_when_something_interesting_happens

In a basic concept: we write our server to get action from firebase database and decision to send notification to other user by FCM. This our server can write with NodeJS or using Firebase Cloud Function (above) Guide how to using Nodejs is here: https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html

like image 127
phapli Avatar answered Sep 30 '22 18:09

phapli