1

Can i send push notification to topic using FirebaseMessaging.getInstance().send ? There are a lot of answers describing how to do this using https request. It's clear, but I don't want to use large block of code, building JSON request body, adding custom headers with my key, etc.

So, what I have tried:

At first - FirebaseMessaging.getInstance().subscribeToTopic("test")

then

RemoteMessage rm = new RemoteMessage.Builder("test") .addData("message", "Hello") .build(); FirebaseMessaging.getInstance().send(rm);

where test is my topic.

Result: no message received. But when I send this JSON via postman:

{"to": "/topics/test", "data": { "message": "Hello", } }

everything is OK and i receive notification on my phone. So, do FirebaseMessaging.getInstance().send support sending topic messages and how do i need to configure RemoteMessage?

aydinugur
  • 1,208
  • 2
  • 14
  • 21
MatWay
  • 66
  • 9
  • 1
    Sending messages to devices requires that you specify the so-called FCM server key. As its name applies this key should only be used on a server, or in an otherwise trusted environment. There is no secure way to send a cloud message to other devices directly **from** a device. See https://stackoverflow.com/a/39279716/209103 – Frank van Puffelen Nov 15 '17 at 15:02
  • @FrankvanPuffelen I understand how push notification works. But in my case I do not need any security. See [link](https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessaging) There is a method `send`, but I can't find how to build RometeMessage correctly for sending notification to the topic. I found that single parameter in `RemoteMessage.Builder()` constructor - this is a "to" parameter. So i tried different options for this: "test", "/topics/test", etc but no success (( – MatWay Nov 15 '17 at 15:44
  • 1
    The link you give is for sending messages **from** a client to a server. From the link: "Client apps can send upstream messages back to the app server using the XMPP-based Cloud Connection Server." Unless you have a server running, these will not be sent out to client devices. Since there is no secure way to send device-to-device messages, the Firebase SDKs intentionally have no method to support that scenario. If you want to send device-to-device messages anyway, you'll have to do it the hard (and insecure) way and call the HTTP API. But I recommend looking at Cloud Functions. – Frank van Puffelen Nov 15 '17 at 16:06
  • Thanks @FrankvanPuffelen! This is exactly what i was looking for! Now understood that FirebaseMessaging instance can send remote messages only to server. Can you post this comment as answer to make me able to apply it as a correct answer? – MatWay Nov 15 '17 at 16:57

0 Answers0