0

I already have an app and I want to start sending notification to the users. I already set up everything in the app(using react native) and I checked manually that I can send notification to the devices and it works. Now I want to run a job in the server who will push the message (with the device token) to the cloud messaging in firebase. I can't find a lot of details about how to do it. I would like if someone can give me any guide I can use with. my server is in Kotlin(java can be good too) and I m working with gradle.

Thank you so much for the help

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    I have exaplained in one of my tutorials step by step, how you can send **[notifications](https://www.youtube.com/watch?v=6RzB4HXzQyA&t=3s&list=PLn2n4GESV0AmXOWOam729bC47v0d0Ohee&index=17)** to specific users using `Cloud Firestore` and `Node.js`. You can also take a look at my answer from this **[post](https://stackoverflow.com/questions/48298993/push-notifications-on-content-change/48299840)**. – Alex Mamo Sep 05 '18 at 12:32
  • @AlexMamo its also for specific device?(if I have the device token) –  Sep 05 '18 at 12:33
  • Check my answer from that post. – Alex Mamo Sep 05 '18 at 12:36

1 Answers1

2

From a Java server you can use the Firebase Admin SDK to send messages. From that documentation comes this minimal example:

// This registration token comes from the client FCM SDKs.
String registrationToken = "YOUR_REGISTRATION_TOKEN";

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

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

Note that this sends a data message, so that will always be delivered to your code, where you can decide to display a notification or not. To send a notification message, which is what the Firebase console does, you'd use:

Message message = Message.builder()
    .setNotification(new Notification("This is the title", "This is the body"))
    .setToken(registrationToken)
    .build();

Both of these send the message to a specific registration token, so only to a single device/app instance. This means you will need to maintain a list of these tokens, in a way that allows you to send the messages to fit your needs. E.g. a common way is to store the tokens per user. For an example of that, see the functions-samples repo. While this example is in Node.js, the same logic could be applied to a Java server.

Finally: you can also send message to topics. For an example of that (again: using a Node.js server), have a look at this blog post Sending notifications between Android devices with Firebase Database and Cloud Messaging.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • btw do you know where I can see all the notification I sent? I mean did the notification saves somewhere in firebase website? I tried what you told me and it works!! but when I go to the project->cloud messaging->notification I don't see what I sent –  Sep 06 '18 at 08:17
  • 1
    Notification messages sent through the API are not tracked in the Firebase console. – Frank van Puffelen Sep 06 '18 at 14:06