@@ -26,7 +26,7 @@ admin.initializeApp();
26
26
* Users save their device notification tokens to `/users/{followedUid}/notificationTokens/{notificationToken}`.
27
27
*/
28
28
exports . sendFollowerNotification = functions . database . ref ( '/followers/{followedUid}/{followerUid}' )
29
- . onWrite ( ( change , context ) => {
29
+ . onWrite ( async ( change , context ) => {
30
30
const followerUid = context . params . followerUid ;
31
31
const followedUid = context . params . followedUid ;
32
32
// If un-follow we exit the function.
@@ -48,44 +48,42 @@ exports.sendFollowerNotification = functions.database.ref('/followers/{followedU
48
48
// The array containing all the user's tokens.
49
49
let tokens ;
50
50
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 ] ;
54
54
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 ) ;
61
61
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
+ } ;
70
70
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 ( ) ) ;
87
85
}
88
- } ) ;
89
- return Promise . all ( tokensToRemove ) ;
86
+ }
90
87
} ) ;
88
+ return Promise . all ( tokensToRemove ) ;
91
89
} ) ;
0 commit comments