This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (sendmsg)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to D:\exampleProject\sendMsg\package.json:
function getAccessToken() {
return new Promise(function(resolve, reject) {
const 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);
});
});
}
/**
* Send HTTP request to FCM with given message.
*
* @param {object} fcmMessage will make up the body of the request.
*/
function sendFcmMessage (fcmMessage) {
getAccessToken().then(function(accessToken) {
const options = {
hostname: HOST,
path: PATH,
method: 'POST',
// [START use_access_token]
headers: {
'Authorization': 'Bearer ' + accessToken
}
// [END use_access_token]
};
const 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);
});
/**
* 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': 'all',
'notification': {
'title': 'FCM Notification',
'body': 'Notification from FCM',
}
}
};
}
const commonMessage = buildCommonMessage();
console.log('FCM request body for message using common notification object:');
console.log(JSON.stringify(commonMessage, null, 2));
sendFcmMessage(buildCommonMessage());
FCM request body for message using common notification object:
{
"message": {
"topic": "all",
"notification": {
"title": "FCM Notification",
"body": "Notification from FCM",
}
}
}
Message sent to Firebase for delivery, response:
{
"name": "projects/test-18a9a/messages/2560468080093694458"
}