0

I would like to push some notification with a android app. So i did some researchs, but i'm little lost.

It's possible that I'm wrong but i think my only possibility is to use FCM with HTTP Request like that :

 POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
  "message":{
    "topic" : "foo-bar",
    "notification" : {
      "body" : "This is a Firebase Cloud Messaging Topic Message!",
      "title" : "FCM Message",
      }
   }
}

But i see everyone use their own database and i don't know why they need to use it. I would like not to have to use my own database, so if you have a better solution please help me.

Thank you for your all responses

(sorry for my english)

JustinMartinDev
  • 559
  • 2
  • 7
  • 23

2 Answers2

1

Sending a message to a device with Firebase Cloud Messaging requires that you specify the FCM server key. It is the value in the Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA header in your sample.

But a key concern here is that anyone with your FCM server key can send messages to all users of your app without restrictions. And if you embed that key into your app, it's only a matter of time before someone discovers is and uses it to send unwanted messages to your users. That's why, as its name implies, the FCM server key should only be used in trusted environments (such as a server/device you control, or Cloud Functions).

So while it is technically possible to send a messages from one Android device to another, doing so exposes users of your app to risks that you should not want.

Some more links to good previous answers and articles:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

I had this same problem a while back... To cope with sending notifications via FCM without a database, I used Firebase Cloud Functions and the Firebase Admin SDK to do this. I needed to send a notification when the user received a chat from the group or from another person. I used one of the 4 triggers in Firebase Cloud Functions. The one I used was the onUpdate() trigger. There are also onWrite(), onDelete(), and onCreate. All of these triggers make it possible to send a notification whenever there is a such change.. I used the Admin SDK then to get the token(if it was a single user, which I had previously uploaded to the database) or the topic(which for group chats, I named after the group chat so it would be easy to get). From this, I also named the type so it would know whether to send to a single user or a group. This allowed me to have a listener to send notifications without needing a database of my own. All through firebase. And another fact to add is that this is done in Node.JS, but it is fairly easy to grasp the concept with a knowledge of Firebase.

Some useful links:

Getting Started with Cloud Functuons

Realtime Database Triggers

Admin SDK Setup

Admin Database Getting Started, next pages for read and write(remember to use Node.JS)

Hope this helps!

Epic Gamer_1
  • 104
  • 3
  • 12