forked from firebase/functions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
124 lines (114 loc) · 3.78 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* 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(functions.config().firebase);
// [START all]
/**
* After a user has completed a purchase, send them a coupon via FCM valid on their next purchase.
*/
// [START trigger]
exports.sendCouponOnPurchase = functions.analytics.event('in_app_purchase').onLog(event => {
// [END trigger]
// [START attributes]
const user = event.data.user;
const uid = user.userId; // The user ID set via the setUserId API.
const purchaseValue = event.data.valueInUSD; // Amount of the purchase in USD.
const userLanguage = user.deviceInfo.userDefaultLanguage; // The user language in language-country format.
// [END attributes]
// For purchases above 500 USD, we send a coupon of higher value.
if (purchaseValue > 500) {
return sendHighValueCouponViaFCM(uid, userLanguage);
}
return sendCouponViaFCM(uid, userLanguage);
});
// [END all]
/**
* Sends a coupon code via FCM to the given user.
*
* @param {string} uid The UID of the user.
* @param {string} userLanguage The user language in language-country format.
*/
function sendCouponViaFCM(uid, userLanguage) {
// Fetching all the user's device tokens.
return getDeviceTokens(uid).then(tokens => {
if (tokens.length > 0) {
// Notification details.
let payload = {
notification: {
title: 'Thanks for your Purchase!',
body: 'Get 10% off your next purchase with "COMEBACK10".'
}
};
// Notification in French.
if (userLanguage.split('-')[0] === 'fr') {
payload = {
notification: {
title: 'Merci pour votre achat!',
body: 'Obtenez 10% de réduction sur votre prochain achat avec "COMEBACK10".'
}
};
}
// Send notifications to all tokens.
return admin.messaging().sendToDevice(tokens, payload);
}
});
}
/**
* Sends a high value coupon vode via FCM to the given user.
*
* @param {string} uid The UID of the user.
* @param {string} userLanguage The user language in language-country format.
*/
function sendHighValueCouponViaFCM(uid, userLanguage) {
// Fetching all the user's device tokens.
return getDeviceTokens(uid).then(tokens => {
if (tokens.length > 0) {
// Notification details.
let payload = {
notification: {
title: 'Thanks for your Purchase!',
body: 'Get 30% off your next purchase with "COMEBACK30".'
}
};
// Notification in French.
if (userLanguage.split('-')[0] === 'fr') {
payload = {
notification: {
title: 'Merci pour votre achat!',
body: 'Obtenez 30% de réduction sur votre prochain achat avec "COMEBACK30".'
}
};
}
// Send notifications to all tokens.
return admin.messaging().sendToDevice(tokens, payload);
}
});
}
/**
* Get the Device Tokens for the given user.
*
* @param {string} uid The UID of the user.
*/
function getDeviceTokens(uid) {
return admin.database().ref(`/users/${uid}/tokens`).once('value').then(snap => {
if (snap.exists()) {
return Object.keys(snap.val());
}
return [];
});
}