Broadcast Receiver with Firebase

Asked

Viewed 214 times

0

I’m in need and I’ve searched a lot, but not yet figure out how to do, I have a base in google’s firebase-database that is updated every 10 minutes, when a new information arrives in firebase the Altime modifies the forms in the app in milliseconds, but when the app is closed I do not know that there has been modification. I need that when there is a modification in firebase the app receive this data that increases the notification for users. I searched several sites and videos and nobody work with Broadcast and firebase, but I believe there is something, I also checked the possibility of using the function Services android, but the same should be triggered by the application and this would be impossible since the same is closed.

Someone could help me

1 answer

0

You can use the Firebase Cloud Messaging in conjunction with Cloud Functions.

Add FCM to Android as shown in the article and create a function that will send notifications. It would look something like this:

'use strict';

const functions = require('firebase-functions'); //Constante para invocar o Firebase Cloud Functions
const admin = require('firebase-admin'); //Constante para invocar o Firebase Admin SDK
admin.initializeApp(functions.config().firebase); //Inicializar o Firebase com configurações do functions

//Função quer detecta alterações na database
exports.enviarNotificacao = functions.database.ref('/caminho/da/informacao/{idInformacao}').onUpdate(event => {

    var instanceId = //Use a sua lógica para obter o(s) token(s) do(s) utilizador(es) que deve(m) ser notificado(s)

    const payload = { //Criar a notificação
          notification: {
              title: "Novos Dados!",
              body: "Os dados do fromulário foram atualizados"
          }
    };

    admin.messaging().sendToDevice(instanceId, payload) //enviar a notificação
            .then(function (response) {
              console.log("Mensagem enviada com sucesso:", response);
            })
            .catch(function (error) {
                console.log("Erro ao enviar a mensagem:", error);
            });

});
  • Ola Rosario, I was looking at the article that I believe meets my need, but I still have some doubts, this method works even with the closed application ? Do I need to send a notification if a value exceeds the preset limit that function will be within firebase or application? need to use token?

  • @Renansilveira you found the solution, I’m looking for it too. I just need to create this broadcast to know if there have been changes and activate the notification.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.