Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send firebase push notification to specific group of user for android

I Have created push notification service using Firebase and I can send notification to either all or single user having FCM id, but I have no idea how to send to specific user.

Also server panel is not created for handling push notification handling If any suggestion for that is there will help more.

like image 716
Anukool srivastav Avatar asked Mar 12 '23 04:03

Anukool srivastav


2 Answers

Firebase Cloud Messaging (FCM) topic messaging allows you to send a message to multiple devices that have opted in to a particular topic. Based on the publish/subscribe model, topic messaging supports unlimited subscriptions for each app i.e your group is attached to specific topic like news group,sports group etc.

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

To unsubscribe unsubscribeFromTopic("news")

From Server side you need to set up for specif topic i.e a group of user like this:

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{
  "to": "/topics/news",
  "data": {
    "message": "This is a Firebase Cloud Messaging Topic Message!",
   }
}

"/topics/news" This will send notification to group of people who have subsribe the news topic

like image 149
Burhanuddin Rashid Avatar answered Apr 09 '23 14:04

Burhanuddin Rashid


In your android code:

    public static void sendNotificationToUser(String user, final String message) {
            Firebase ref = new Firebase(FIREBASE_URL);
            final Firebase notifications = ref.child("notificationRequests");

            Map notification = new HashMap<>();
            notification.put("us

ername", user);
        notification.put("message", message);

        notifications.push().setValue(notification);
    }

Create a node and put this code inside:

    var firebase = require('firebase');
var request = require('request');

var API_KEY = "..."; // Your Firebase Cloud Server API key

firebase.initializeApp({
  serviceAccount: ".json",
  databaseURL: "https://.firebaseio.com/"
});
ref = firebase.database().ref();

function listenForNotificationRequests() {
  var requests = ref.child('notificationRequests');
  ref.on('child_added', function(requestSnapshot) {
    var request = requestSnapshot.val();
    sendNotificationToUser(
      request.username, 
      request.message,
      function() {
        request.ref().remove();
      }
    );
  }, function(error) {
    console.error(error);
  });
};

function sendNotificationToUser(username, message, onSuccess) {
  request({
    url: 'https://fcm.googleapis.com/fcm/send',
    method: 'POST',
    headers: {
      'Content-Type' :' application/json',
      'Authorization': 'key='+API_KEY
    },
    body: JSON.stringify({
      notification: {
        title: message
      },
      to : '/topics/user_'+username
    })
  }, function(error, response, body) {
    if (error) { console.error(error); }
    else if (response.statusCode >= 400) { 
      console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage); 
    }
    else {
      onSuccess();
    }
  });
}

// start listening
listenForNotificationRequests();

More information with the following link, it is the same thing with a many devices:

Sending notification between android devices with Firebase Database and Cloud Messaging

like image 36
Carlo Avatar answered Apr 09 '23 16:04

Carlo