Created
December 21, 2020 13:15
-
-
Save egesamichael/0688f86a0c930eb6149aecbe2fc937c3 to your computer and use it in GitHub Desktop.
Failing Firebase Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright 2016 Google Inc. All Rights Reserved. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
"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/{agentUid}/{clientUid}`. | |
* Users save their device notification tokens to `/users/{agentUid}/notificationTokens/{notificationToken}`. | |
*/ | |
exports.sendRequestNotification = exports.createUser = functions.firestore | |
.document("Requests/{agentUid}") | |
.onCreate((snap, context) => { | |
// Get an object representing the document | |
// e.g. {'name': 'Marie', 'age': 66} | |
const newValue = snap.data(); | |
// access a particular field as you would any JS property | |
const name = newValue.agent; | |
// perform desired operations ... | |
// Get the list of device notification tokens. | |
exports.getDeviceTokensPromise = functions.firestore | |
.document("/userTockens/{agentUid}") | |
.then(function (doc) { | |
if (doc.exists) { | |
console.log("Document data:", doc.data()); | |
// The snapshot to the user's tokens. | |
let tokensSnapshot; | |
// The array containing all the user's tokens. | |
let tokens = doc.data().token; | |
const results = Promise.all([getDeviceTokensPromise]); | |
tokensSnapshot = results[0]; | |
// Check if there are any device tokens. | |
if (!tokensSnapshot.hasChildren()) { | |
return console.log("There are no notification tokens to send to."); | |
} | |
console.log( | |
"There are", | |
tokensSnapshot.numChildren(), | |
"tokens to send notifications to." | |
); | |
console.log("Fetched follower profile", follower); | |
// Notification details. | |
const payload = { | |
notification: { | |
title: "New Easy Ride Request", | |
body: `${follower.displayName} needs help.`, | |
// icon: follower.photoURL | |
}, | |
}; | |
// Listing all tokens as an array. | |
tokens = Object.keys(tokensSnapshot.val()); | |
// Send notifications to all tokens. | |
const response = admin.messaging().sendToDevice(tokens, payload); | |
// 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", | |
tokens[index], | |
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( | |
tokensSnapshot.ref.child(tokens[index]).remove() | |
); | |
} | |
} | |
}); | |
return Promise.all(tokensToRemove); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment