-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.js
More file actions
87 lines (75 loc) · 2.8 KB
/
client.js
File metadata and controls
87 lines (75 loc) · 2.8 KB
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
/**
* Drive firebase HTTP v1 node server messaging app, either local or remote.
* @example node client [--local]
*/
const useLocal = process.argv.includes("--local");
sendMsg(useLocal)
.then((msgId) => {
console.log("client: msgId: " + msgId);
});
/**
* Send sample message to node.js server to send Firebase push notification via HTTP v1 interface
* See https://firebase.google.com/docs/cloud-messaging/migrate-v1 for message format.
*
* @async
* @param Boolean useLocal - true -> use local url
* @returns {String} message ID (successful) | error message (error)
*/
async function sendMsg(useLocal) {
let msgId = null;
const localUrl = ""; //TODO: run `npm run serve` from /functions folder and get url from `http function initialized` message; append "/msg"
const remoteUrl = ""; //TODO: get trigger URL from Firebase Console `Functions` tab; append "/msg"
const url = useLocal ? localUrl : remoteUrl;
let fetchOptions = {
"method": 'POST',
"headers": {
'Content-Type': 'application/json'
},
"body": JSON.stringify({
"app": "festivelo",
"msg": {
"topic": "<topic>", //TODO: set topic or replace with `token: <tokenvalue>`
"notification": {
"title": "Sample Notification",
"body": "Test Notification from Client",
},
"data": { //TODO: add/remove data properties to match the needs of your app
"action": "notify",
"name": "Aaron Rider",
"nfytext": "Test Notification from Client",
"timestamp": new Date().getTime().toString(), // note: all values must be strings
"title": "Sample Notification",
},
"android": {
"priority": 'high',
"notification": {
"notification_priority": "4",
"visibility": "1",
"sound": 'default',
}
},
"apns": {
"payload": {
"aps": {
"contentAvailable": "1",
"sound": 'default',
}
}
}
}
})
}
try {
let response = await fetch(url, fetchOptions);
msgId = await response.text(); // response text is a message ID string or error message
if (response.ok) {
;
}
else {
console.error("sendMsg: fetch error: " + response.status + ": " + msgId);
}
} catch (e) {
console.error("sendMsg: fetch catch error: " + e);
}
return msgId;
} // end sendMsg