How to send device to device messages using Firebase Cloud Messaging?

后端 未结 13 1904
Happy的楠姐
Happy的楠姐 2020-11-22 01:47

After searching the docs I could not find any info on how to send device to device messages using FCM without the use of an external server.

For example, if I was cr

13条回答
  •  迷失自我
    2020-11-22 02:17

    Google Cloud Functions make it now possible send push notifications from device-to-device without an app server. I have made cloud function which is trigger when new message is added in database

    It is node.js code

    'use strict';
    
    const functions = require('firebase-functions');
    const admin = require('firebase-admin'); admin.initializeApp();
    
    exports.sendNotification = functions.database.ref('/conversations/{chatLocation}/{messageLocation}')
      .onCreate((snapshot, context) => {
          // Grab the current value of what was written to the Realtime Database.
          const original = snapshot.val();
    
           const toIDUser = original.toID;
           const isGroupChat = original.isGroupChat;
    
           if (isGroupChat) {
           const tokenss =  admin.database().ref(`/users/${toIDUser}/tokens`).once('value').then(function(snapshot) {
    
    // Handle Promise
           const tokenOfGroup = snapshot.val()
    
          // get tokens from the database  at particular location get values 
           const valuess = Object.keys(tokenOfGroup).map(k => tokenOfGroup[k]);
    
         //console.log(' ____________ffffd((999999ffffd_________________ ' +  valuess );
        const payload = {
           notification: {
                     title:   original.senderName + " :- ",
                     body:    original.content
        }
      };
    
      return admin.messaging().sendToDevice(valuess, payload);
    
    
    
    }, function(error) {
    
      console.error(error);
    });
    
           return ;
              } else {
              // get token from the database  at particular location
                    const tokenss =  admin.database().ref(`/users/${toIDUser}/credentials`).once('value').then(function(snapshot) {
                    // Handle Promise
      // The Promise was "fulfilled" (it succeeded).
    
         const credentials = snapshot.val()
    
    
    
        // console.log('snapshot ......snapshot.val().name****^^^^^^^^^^^^kensPromise****** :- ', credentials.name);
         //console.log('snapshot.....****snapshot.val().token****^^^^^^^^^^^^kensPromise****** :- ', credentials.token);
    
    
         const deviceToken = credentials.token;
    
        const payload = {
           notification: {
                     title:   original.senderName + " :- ",
                     body:    original.content
        }
      };
    
      return admin.messaging().sendToDevice(deviceToken, payload);
    
    
    }, function(error) {
    
      console.error(error);
    });
    
    
              }
    
    
    
    
    
      return ;
    
    
        });
    

提交回复
热议问题