Search code examples
androidfirebasegoogle-cloud-functionsfirebase-cloud-messagingfirebase-notifications

Script for making firebase user to user notifications


(Sorry for bad English) I'm looking for a way to make user to user notifications in Android. The system have to catch a new child event in the Database, read the data and send the notification to destination.

The struct of the DB is this:

notificationRequests
    $pushid
      message: "You have a new request! Open the app"
      userId: "sadbijasuobru112u4124u21b"      //user destination id

By doing some researches in the web I've found the possibility to use topic messages. So, i've added this in my LoginActivity befor calling the MainActivity:

    FirebaseMessaging.getInstance().subscribeToTopic("users_topic$uid")
                    .addOnCompleteListener {
                        Log.d("LoginActivity", "User registered")
                    }

The code works. I can send notifications from the console

Like I said before, i need automatic messages. I've found this code on the web, but it doesn't work.

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

var API_KEY = "Firebase Cloud Messaging Server API key"

// Fetch the service account key JSON file contents
var serviceAccount = require("./serviceAccountKey.json");

// Initialize the app with a service account, granting admin privileges
firebase.initializeApp({
  credential: firebase.credential.cert(serviceAccount),
  databaseURL: "https://appname.firebaseio.com/"
});
ref = firebase.database().ref();

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

function sendNotificationToUser(userID, 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/users_topic'+userID
    })
  }, (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();

I've implemented this code in the index.js for deployng. I've also write the entire file instead of requiring it, but it's always the same.

const functions = require('firebase-functions');

exports.sendNotifications = functions.https.onRequest((request, response) => {
  require('./sendNotifications.js')
})

Any suggestions? Thanks for the kelp!

EDIT

Does this need a billing account configurated? If yes, how can i make it with the free plan?


Solution

  • The problem was that I was calling an external site. I solved it doing this:

    const functions = require('firebase-functions');
    
    var firebase = require('firebase-admin');
    
    // Fetch the service account key JSON file contents
    var serviceAccount = require("./serviceAccountKey.json");
    
    // Initialize the app with a service account, granting admin privileges
    firebase.initializeApp({
      credential: firebase.credential.cert(serviceAccount),
      databaseURL: "https://appname.firebaseio.com/"
    });
    
    exports.sendNotifications = functions.database.ref('/notificationRequest/{pushId}')
      .onCreate( (change, context) => {
        var uid = change.child('userId').val()
        var notificationMessage = change.child('message').val()
    
        var userTopic = 'users_topic'+uid
    
        var payload = {
          data: {
            message: notificationMessage
          }
        };
    
        var options = {
          priority: 'high',
          timeToLive: 60 * 60 * 24,
          collapseKey: 'notification'
        };
    
        firebase.messaging().sendToTopic(userTopic, payload, options)
          // eslint-disable-next-line promise/always-return
          .then((response) => {
            console.log('Done', response);
          })
          .catch((error) => {
            console.log('Error: ', error);
          });
    
        return change.ref.remove();
      });