I am using Firebase for my android studio project and I want to have a notification button so that whenever a user clicks a send button, the notification button indicates there is a new notification and that notification contains the data that the user sent. Is it possible to do so using firebase ? and how ?
2 Answers
Sure. You want to use Topic Messaging in FCM.
Here's the setup guide.
Once implemented, subscribe a user with:
FirebaseMessaging.getInstance().subscribeToTopic("dogs")
And send a notification via a POST request on the app (insecure, not recommended) or via a server (recommended):
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":{
"condition": "'dogs' in topics || 'cats' in topics",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message",
}
}
}

- 7,549
- 8
- 45
- 86
-
so at first I should implement how to send a notification to users and then I put the code you suggested where exactly ? – user6465283 Jan 07 '19 at 23:30
-
The subscribe code goes whenever you want them to subscribe to the topic, maybe on first start. How are you intending to send the notification? Via your server, manually, from another user, etc? – Jake Lee Jan 07 '19 at 23:31
-
Regardless, you'll want to read this whole article, specifically the `POST` request section linked to: https://firebase.google.com/docs/cloud-messaging/android/topic-messaging#topic-http-post-request – Jake Lee Jan 07 '19 at 23:33
-
I think there is something not suitable with all this because I want a notfication button to indicate a new notification and not a push notifcation and when the user opens it, it shows the data that the other user has sent. Should I use another way or this is still the way ? I've been looking for hours and all I found is how to send push notifications. – user6465283 Jan 07 '19 at 23:43
-
Ah, I see, it's for user to group messaging, I thought it was for alerting users of certain news topics etc. I'm not sure I'm afraid, maybe edit your question to make it more clear it's for user to group messaging? – Jake Lee Jan 07 '19 at 23:45
I don't know, whether you figured it out or not (because you asked the question months ago), but I may have an idea. Actually I was also searching for what you asked to do, and I read your question. I wanted to do the same thing as you. Then I thought, if firebase cloud messaging service can receive http POST request not just from firebase console but also from any other service like ARC (Advanced REST client).
So the solution : Send a JSON HTTP post from your app when the users click on the app with the required data received from user. Now when the JSON response is received by the firebase cloud messaging server, it should you the notification to every user with the data sent by the user who clicked the button.
I haven't tried it yet but I am going to try it now. I will let you know in the comments if it works or not. It should work theoretically. If you have already figured out a way to send such notifications, please don't forget to share your method.
If you want to know how to send JSON HTTP post, then here is a stack overflow answer : SENDING JSON OBJECT

- 23
- 5