1

I want to sent a message with the fcm of google but I need a class named "Message" but it doesn't exist with my imports. (I implemented com.google.firebase:firebase-messaging:18.0.0)

I tried using the remote message class but it doesn't fit to my problem.

implementation 'com.google.firebase:firebase-core:16.0.9'
implementation 'com.google.firebase:firebase-messaging:18.0.0'

I want to use this example code:

// The topic name can be optionally prefixed with "/topics/".
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);

The needed class is this: https://firebase.google.com/docs/reference/admin/java/reference/com/google/firebase/messaging/Message but it doesn't exist.

Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
Samscryer
  • 25
  • 2

1 Answers1

0

Sending messages to users/devices with Firebase Cloud Messaging, requires that your code has access to the FCM Server Key. As its name implies, this key should only be used on a server, as having access to this key allows one to send any messages to all users of your app.

You seem to be writing code in an Android app, while the Message class you're referring to is part of the Firebase Admin SDK. The Admin SDK gives its users full access to your Firebase project (including your FCM server key), and for that reason can only be used in a trusted environment, such as your development machine, a server you control, or Cloud Functions.

So to send messages to a user/device, you will always need a server, or otherwise trusted environment, to send them from. For more on this, see:

Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645