Broadcast Receiver with Firebase

0

I have a need and I have already searched a lot, but I still have not figured out how to do it, I have a base in google firebase-database that is updated every 10 minutes when new information arrives in firebase realtime modifies the forms in app in milliseconds, but when the app is closed I do not know that there was modification. I need when there is a modification in the firebase the app receives this data that uploads the notification to the users. I searched for several sites and videos and no one worked with Broadcast and firebase, but I believe there is something, I also checked the possibility of using the services function of android, but the same should be triggered by the application and this would be impossible since it is closed.

Someone could help me

    
asked by anonymous 14.06.2018 / 19:31

1 answer

0

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

Add FCM to Android as the article shows 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);
            });

});
    
14.06.2018 / 20:47