-
Notifications
You must be signed in to change notification settings - Fork 424
/
index.js
140 lines (129 loc) · 3.8 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Firebase Cloud Messaging (FCM) can be used to send messages to clients on iOS, Android and Web.
*
* This sample uses FCM to send two types of messages to clients that are subscribed to the `news`
* topic. One type of message is a simple notification message (display message). The other is
* a notification message (display notification) with platform specific customizations. For example,
* a badge is added to messages that are sent to iOS devices.
*/
const https = require('https');
var fs = require('fs');
var {google} = require('googleapis');
var PROJECT_ID = '<YOUR-PROJECT-ID>';
var HOST = 'fcm.googleapis.com';
var PATH = '/v1/projects/' + PROJECT_ID + '/messages:send';
var MESSAGING_SCOPE = 'https://www.googleapis.com/auth/firebase.messaging';
var SCOPES = [MESSAGING_SCOPE];
/**
* Get a valid access token.
*/
// [START retrieve_access_token]
function getAccessToken() {
return new Promise(function(resolve, reject) {
var key = require('./service-account.json');
var jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
SCOPES,
null
);
jwtClient.authorize(function(err, tokens) {
if (err) {
reject(err);
return;
}
resolve(tokens.access_token);
});
});
}
// [END retrieve_access_token]
/**
* Send HTTP request to FCM with given message.
*
* @param {JSON} fcmMessage will make up the body of the request.
*/
function sendFcmMessage(fcmMessage) {
getAccessToken().then(function(accessToken) {
var options = {
hostname: HOST,
path: PATH,
method: 'POST',
// [START use_access_token]
headers: {
'Authorization': 'Bearer ' + accessToken
}
// [END use_access_token]
};
var request = https.request(options, function(resp) {
resp.setEncoding('utf8');
resp.on('data', function(data) {
console.log('Message sent to Firebase for delivery, response:');
console.log(data);
});
});
request.on('error', function(err) {
console.log('Unable to send message to Firebase');
console.log(err);
});
request.write(JSON.stringify(fcmMessage));
request.end();
});
}
/**
* Construct a JSON object that will be used to customize
* the messages sent to iOS and Android devices.
*/
function buildOverrideMessage() {
var fcmMessage = buildCommonMessage();
var apnsOverride = {
'payload': {
'aps': {
'badge': 1
}
},
'headers': {
'apns-priority': '10'
}
};
var androidOverride = {
'notification': {
'click_action': 'android.intent.action.MAIN'
}
};
fcmMessage['message']['android'] = androidOverride;
fcmMessage['message']['apns'] = apnsOverride;
return fcmMessage;
}
/**
* Construct a JSON object that will be used to define the
* common parts of a notification message that will be sent
* to any app instance subscribed to the news topic.
*/
function buildCommonMessage() {
return {
'message': {
'topic': 'news',
'notification': {
'title': 'FCM Notification',
'body': 'Notification from FCM'
}
}
};
}
var message = process.argv[2];
if (message && message == 'common-message') {
var commonMessage = buildCommonMessage();
console.log('FCM request body for message using common notification object:');
console.log(JSON.stringify(commonMessage, null, 2));
sendFcmMessage(buildCommonMessage());
} else if (message && message == 'override-message') {
var overrideMessage = buildOverrideMessage();
console.log('FCM request body for override message:');
console.log(JSON.stringify(overrideMessage, null, 2));
sendFcmMessage(buildOverrideMessage());
} else {
console.log('Invalid command. Please use one of the following:\n'
+ 'node index.js common-message\n'
+ 'node index.js override-message');
}