Tyson
Tyson

Reputation: 747

Create and send cloud messages to Topic in Firebase

I am creating an Android application which allows a single user to form a group with all or some of the other users in the app. I am using Firebase for my database. I want to send a push notification to the user when he/she is being added into a group. I can understand how to receive the messages and handle it.

I read the docs over and over again but I have no idea how to actually initiate the group to begin with and how to send push notifications to the users. I have till now only used the Firebase console to send push notifications to all the users in my app.

How can I create a topic such that the users who are in that topic are sent a push notification automatically without using the Firebase console?

Upvotes: 0

Views: 696

Answers (2)

Afjalur Rahman Rana
Afjalur Rahman Rana

Reputation: 813

From Firebase console: Firstly you need to subscribe the topic using your android app... Use this code to subscribe the topic

    FirebaseMessaging.getInstance().subscribeToTopic("your_topic_name_here");

Now while sending notification using firebase console set the "Target" to "Topic" and you should find out subscribed topic (you given earlier at the place of "your_topic_name_here") there :)

Then send notification and only those specific group of people who subscribed that topic will get the notification

Using own server: For sending notification to specific user using own server you have to follow those posts

How to add push notification in android application from android studio – Android developer (part – 1 Connect with firebase ) => https://androidrace.com/2016/12/08/how-to-add-push-notification-in-android-application-from-android-studio-android-developer-part-1-connect-with-firebase/

How to add push notification in android application from android studio – Android developer (part – 2 Working with server) =>https://androidrace.com/2017/01/05/how-to-add-push-notification-in-android-application-from-android-studio-android-developer-part-2-working-with-server/

Have a nice day :)

Upvotes: 1

John O'Reilly
John O'Reilly

Reputation: 10330

One option, if using Node.js, is to use node-gcm (https://github.com/ToothlessGear/node-gcm).

For example:

  var gcm = require('node-gcm');

  var sender = new gcm.Sender(senderKey);

  var message = new gcm.Message();
  message.addNotification('title', title);
  message.addNotification('body', body);

  sender.send(message, { topic: "/topics/" + topic }, function (err, response) {
    if (err) console.error(err);
    else console.log(response);
  });

Upvotes: 1

Related Questions