0

I'm following the below link to implement Firebase Messaging to send messages to multiple devices

https://firebase.google.com/docs/cloud-messaging/android/send-multiple#build_send_requests

I'm almost done with the implementation but just stuck at the last stage (Build send requests)

In the below code

Message message = Message.builder()
    .putData("score", "850")
    .putData("time", "2:45")
    .setTopic(topic)
    .build();

I'm getting the error Cannot resolve symbol 'Message'

Also in the line

String response = FirebaseMessaging.getInstance().send(message);

When I Ctrl+click the send() method, the argument message is being shown as instance of RemoteMessage and not Message with return type of void and not String

Am I missing any dependencies or is there any change in implementation in the latest library of firebase messaging?

I'm using the following (latest) Firebase version in my app level build.gradle

implementation 'com.google.firebase:firebase-messaging:19.0.1'
implementation 'com.google.firebase:firebase-core:17.0.1'
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Vishal Afre
  • 1,013
  • 3
  • 12
  • 39
  • Kindly Check if you have imported proper Message Import, which Belongs to Firebase Messaging. Else paste your imports. – Nizamudeen Sherif Jul 16 '19 at 14:15
  • The Message class requires import com.google.firebase.messaging.Message but I'm not able to import this as I cannot find it (though I'm able to find com ggogle.firebase.messaging) – Vishal Afre Jul 16 '19 at 15:16

1 Answers1

2

The Message class is inside the Firebase admin sdk but you cannot use that in your android project, you can only use firebase admin sdk in the server side and then you will be able to use the Message class. Check the docs for reference:-

https://firebase.google.com/docs/cloud-messaging/manage-topics

To be able to use the admin sdk, you need a server like Tomcat, you should also use java version 8, then you can follow the following tutorial:-

https://firebase.google.com/docs/admin/setup/#prerequisites


Original Answer

You need to use RemoteMessage class, example:

RemoteMessage message = RemoteMessage.builder()
    .addData("score", "850")
    .addData("time", "2:45")
    .build();

Check here for more information:

https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/RemoteMessage

https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/RemoteMessage.Builder.html

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • I tried it but I want to send message to users subscribed to a particular Topic, and this method has no arguments for topic – Vishal Afre Jul 16 '19 at 19:20
  • 1
    The `Message` class is inside the Firebase admin sdk but you cannot use that in your android project, you can only use firebase admin sdk in the server side and then you will be able to use the `Message` class https://firebase.google.com/docs/cloud-messaging/manage-topics – Peter Haddad Jul 16 '19 at 20:38
  • Ohhh... Thanks Peter! Now I got it. Actually I'm new to FCM and not sure where do I put this code on my server and how do I call it from Android. Can you please guide me on this. – Vishal Afre Jul 17 '19 at 02:49
  • 2
    U need a server like tomcat that will have java 8 and then u can follow this https://firebase.google.com/docs/admin/setup/#prerequisites – Peter Haddad Jul 17 '19 at 04:11
  • Okay... Thanks for the help... Just worried about 1 thing. I want to achieve functionality of sending notifications from one user to multiple users from my android app. So am I on the right track? – Vishal Afre Jul 17 '19 at 08:28
  • yes let all the users subscribe to the topic then in the backend set the topic and using `send()` everyone subscribed to that topic will recieve the notification. – Peter Haddad Jul 17 '19 at 08:30