Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const functions = require('firebase-functions');
- const admin = require('firebase-admin');
- admin.initializeApp();
- /**
- * Triggers when a user gets a new follower and sends a notification.
- *
- * Followers add a flag to `/followers/{followedUid}/{followerUid}`.
- * Users save their device notification tokens to `/users/{followedUid}/notificationTokens/{notificationToken}`.
- */
- exports.sendFollowerNotification = functions.database.ref('/followers/{followedUid}/{followerUid}')
- .onWrite((change, context) => {
- const followerUid = context.params.followerUid;
- const followedUid = context.params.followedUid;
- // If un-follow we exit the function.
- if (!change.after.val()) {
- return console.log('User ', followerUid, 'un-followed user', followedUid);
- }
- var convertToUppercase = functions.database.ref('/followers/{followedUid}/device_token').onWrite((changes, context) => {
- const beforeData = changes.before.val(); // data before the write
- const afterData = changes.after.val(); // data after the write
- });
- // var tokenNew= admin.database().ref('Users/{followedUid}/device_token').once('value', (snap) => {
- // const device_token = snap.val()
- // console.log(device_token+" this is token"); // Null
- // return device_token;
- // });
- var device_token;
- // Get the list of device notification tokens.
- const getDeviceTokensPromise = admin.database().ref('Users/'+followedUid+'/device_token').once('value', (snap) => {
- device_token = snap.val()
- console.log(device_token+" this is token in getdevtok"); // Null
- return device_token;
- });
- var name;
- const getFollowerName = admin.database().ref('Users/'+followerUid+'/name').once('value', (snap) => {
- name = snap.val()
- console.log(name+" this is follower name"); // Null
- return name;
- });
- var thumb_image;
- var getFollowerThumb = admin.database().ref('Users/'+followerUid+'/thumb_image').once('value', (snap) => {
- thumb_image =String(snap.val())
- console.log(thumb_image+" this is follower dp photoURL"); // Null
- return thumb_image;
- });
- // var favs = snap.val();
- // const getFollowerNumb = admin.database().ref('Users/'+followerUid+'/favs').once('value', (snap) => {
- // favs = snap.val()
- // console.log(favs+" this is number"); // Null
- // return favs;
- // });
- // Get the follower profile.
- const getFollowerProfilePromise = admin.auth().getUser(followerUid);
- // The snapshot to the user's tokens.
- let tokensSnapshot;
- // The array containing all the user's tokens.
- let tokens;
- return Promise.all([getDeviceTokensPromise, getFollowerProfilePromise]).then(results => {
- tokensSnapshot = results[0];
- const follower = results[1];
- // Check if there are any device tokens.
- // if (!tokensSnapshot.hasChildren()) {
- // return console.log('There are no notification tokens to send to.'+getDeviceTokensPromise);
- // }
- // Notification details.
- const payload = {
- data: {
- title: 'New follower',
- channel_url: 'followers',
- body: name+' is now following you on LittleGig.',
- message: name+' is now following you.',
- name: 'New follower',
- uid:followerUid,
- channel:'Followers',
- profiledps: String(thumb_image+''),
- uiduser:followerUid,
- titles: 'New follower',
- channel_urls: 'followers',
- bodys: name+' is now following you on LittleGig.',
- icon:String(thumb_image+''),
- }
- // ,
- // data: {
- // profiledps: String(thumb_image+''),
- // uiduser:followerUid,
- // titles: 'New follower',
- // channel_urls: 'followers',
- // bodys: name+' is now following you on LittleGig.',
- // messages: name+' is now following you.'
- // }
- // ,"data":{
- // title: 'New follower',
- // channel_url: 'followers',
- // body: name+' is now following you on LittleGig.',
- // message: name+' is now following you.',
- // name: 'New follower',
- // uid:followerUid,
- // channel:'Followers',
- // icon:String(thumb_image),
- // }
- };
- console.log('sending notification to '+ device_token+ ' name follower' +name+ ' pic follower '+thumb_image +' getFollowerThumb '+getFollowerThumb );
- console.log('Fetched follower profile', follower);
- // Listing all tokens as an array.
- // tokens = Object.keys(tokensSnapshot.val());
- // Send notifications to all tokens.
- return admin.messaging().sendToDevice(device_token, payload);
- }).then((response) => {
- // For each message check if there was an error.
- const tokensToRemove = [];
- response.results.forEach((result, index) => {
- const error = result.error;
- if (error) {
- console.error('Failure sending notification to', device_token, error);
- // Cleanup the tokens who are not registered anymore.
- if (error.code === 'messaging/invalid-registration-token' ||
- error.code === 'messaging/registration-token-not-registered') {
- tokensToRemove.push(admin.database().ref('followers/'+followedUid+'/'+followerUid).remove());
- }
- }
- });
- return Promise.all(tokensToRemove);
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement