0

I am trying to make push notifications using FCM in which any user subscribed to the topic "Alerts" can send a notification to others. I can recieve notifications sent using the FCM testing system. But when I was reffering to the documentation for sending push notifications, it gave me this code

String topic = "highScores";

// See documentation on defining a message payload.
Message message = Message.builder()
    .putData("score", "850")
    .putData("time", "2:45")
    .setTopic(topic)
    .build();

// Send a message to the devices subscribed to the provided topic.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);

I had 2 problems in this First, the class named message does not exist. After much research, I found out the message class was deprecated and RemoteMessage had to be used but the RemoteMessage documentation had no proper instructions on how to do this.I need help in that
Second, a very small problem but does the topic name have to be the channel name or the topic to which everyone is subscribed to? Even sending the message to a list of tokens will be perfect

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 1
    You can't send messages to other devices directly from within your Android app, as that would be a big abuse vector. You'll need to do this from a trusted environment, such as your development machine, a server you control, or Cloud Functions. The code you found in the documentation is pretty much a way to send a message from your Android code to such a server. – Frank van Puffelen Jul 03 '20 at 15:54
  • I was just typing an answer, then it got closed. :(. you can actually send using a API. – Gouse Mohiddin Jul 03 '20 at 16:02
  • Hey @GouseMohiddin Sorry for closing the question while you were working on an answer. If the question was wrongly closed, it can be reopened. Can you point to the documentation for how you would **send** a message from device to device as OP is asking? – Frank van Puffelen Jul 04 '20 at 14:04
  • @GouseMohiddin could you please send the API code as it would be a great help for me. I have been trying to do this for around a week now –  Jul 05 '20 at 08:20
  • @SiddharthAgrawal I am unable to open the question, to post my answer – Gouse Mohiddin Jul 05 '20 at 18:32
  • @GouseMohiddin Can you email it to me at siddharthagrawal2006@gmail.com?@FrankVanPuffelen, please open the question. –  Jul 07 '20 at 03:42

0 Answers0