Skip to content

Commit 077952f

Browse files
Upgrade fcm-notifications to ES2017 syntax
1 parent fb08eaf commit 077952f

File tree

3 files changed

+38
-37
lines changed

3 files changed

+38
-37
lines changed

fcm-notifications/functions/.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"parserOptions": {
33
// Required for certain syntax usages
4-
"ecmaVersion": 6
4+
"ecmaVersion": 2017
55
},
66
"plugins": [
77
"promise"

fcm-notifications/functions/index.js

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ admin.initializeApp();
2626
* Users save their device notification tokens to `/users/{followedUid}/notificationTokens/{notificationToken}`.
2727
*/
2828
exports.sendFollowerNotification = functions.database.ref('/followers/{followedUid}/{followerUid}')
29-
.onWrite((change, context) => {
29+
.onWrite(async (change, context) => {
3030
const followerUid = context.params.followerUid;
3131
const followedUid = context.params.followedUid;
3232
// If un-follow we exit the function.
@@ -48,44 +48,42 @@ exports.sendFollowerNotification = functions.database.ref('/followers/{followedU
4848
// The array containing all the user's tokens.
4949
let tokens;
5050

51-
return Promise.all([getDeviceTokensPromise, getFollowerProfilePromise]).then(results => {
52-
tokensSnapshot = results[0];
53-
const follower = results[1];
51+
const results = await Promise.all([getDeviceTokensPromise, getFollowerProfilePromise]);
52+
tokensSnapshot = results[0];
53+
const follower = results[1];
5454

55-
// Check if there are any device tokens.
56-
if (!tokensSnapshot.hasChildren()) {
57-
return console.log('There are no notification tokens to send to.');
58-
}
59-
console.log('There are', tokensSnapshot.numChildren(), 'tokens to send notifications to.');
60-
console.log('Fetched follower profile', follower);
55+
// Check if there are any device tokens.
56+
if (!tokensSnapshot.hasChildren()) {
57+
return console.log('There are no notification tokens to send to.');
58+
}
59+
console.log('There are', tokensSnapshot.numChildren(), 'tokens to send notifications to.');
60+
console.log('Fetched follower profile', follower);
6161

62-
// Notification details.
63-
const payload = {
64-
notification: {
65-
title: 'You have a new follower!',
66-
body: `${follower.displayName} is now following you.`,
67-
icon: follower.photoURL
68-
}
69-
};
62+
// Notification details.
63+
const payload = {
64+
notification: {
65+
title: 'You have a new follower!',
66+
body: `${follower.displayName} is now following you.`,
67+
icon: follower.photoURL
68+
}
69+
};
7070

71-
// Listing all tokens as an array.
72-
tokens = Object.keys(tokensSnapshot.val());
73-
// Send notifications to all tokens.
74-
return admin.messaging().sendToDevice(tokens, payload);
75-
}).then((response) => {
76-
// For each message check if there was an error.
77-
const tokensToRemove = [];
78-
response.results.forEach((result, index) => {
79-
const error = result.error;
80-
if (error) {
81-
console.error('Failure sending notification to', tokens[index], error);
82-
// Cleanup the tokens who are not registered anymore.
83-
if (error.code === 'messaging/invalid-registration-token' ||
84-
error.code === 'messaging/registration-token-not-registered') {
85-
tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
86-
}
71+
// Listing all tokens as an array.
72+
tokens = Object.keys(tokensSnapshot.val());
73+
// Send notifications to all tokens.
74+
const response = await admin.messaging().sendToDevice(tokens, payload);
75+
// For each message check if there was an error.
76+
const tokensToRemove = [];
77+
response.results.forEach((result, index) => {
78+
const error = result.error;
79+
if (error) {
80+
console.error('Failure sending notification to', tokens[index], error);
81+
// Cleanup the tokens who are not registered anymore.
82+
if (error.code === 'messaging/invalid-registration-token' ||
83+
error.code === 'messaging/registration-token-not-registered') {
84+
tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
8785
}
88-
});
89-
return Promise.all(tokensToRemove);
86+
}
9087
});
88+
return Promise.all(tokensToRemove);
9189
});

fcm-notifications/functions/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@
1717
"deploy": "firebase deploy --only functions",
1818
"logs": "firebase functions:log"
1919
},
20+
"engines": {
21+
"node": "8"
22+
},
2023
"private": true
2124
}

0 commit comments

Comments
 (0)