Advertisement
CarlosWGama

Cloud Functions Exemplos

Dec 5th, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as functions from 'firebase-functions';
  2. import * as admin from 'firebase-admin';
  3. admin.initializeApp();
  4.  
  5. exports.salvandoBanco = functions.https.onRequest((request, response) => {
  6.     return admin.database().ref('id_salvo').set(request.query.id).then(() => {
  7.         response.send("ID salvo: " + request.query.id);
  8.     })
  9. });
  10.  
  11. exports.notificacao = functions.https.onRequest((request, response) => {
  12.     let not = {
  13.         notification: {
  14.           title: 'Sua mensagem!',
  15.           body: 'Conteúdo da notificação',
  16.         }
  17.     };
  18.     //let token = 'crH8dfHlGRM:APA91bEWoeBwUauSQWFszcCuwAoeVwBD1wsvIMaQomyjsiLFLr2hDRqMddq_dGMUjYIakd-ECsFFy0qdXeInc2ZYK-5ErNm7VTF5HwXjYgBZC3WYjxICXT1w6V-wYuJ1Y8Fi9dVMTeiw';
  19.     //return admin.messaging().sendToDevice(token, not);
  20.  
  21.     return admin.messaging().sendToTopic('topico', not);
  22. })
  23.  
  24. //Caminho referência que será observado caso surge novo valor
  25. exports.bancoExemplo = functions.database.ref('/usuarios/{usuarioID}')
  26.                                 .onCreate((snapshot, context) => {
  27.     //Recupera o ultimo conteúdo criado neste caminho, onde o ultimo trecho pode variar
  28.     let dados = {
  29.         usuarioID: context.params.usuarioID,
  30.         valor: snapshot.val()
  31.     }
  32.     return admin.database().ref('ultimo_usuario').set(dados);
  33. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement