-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
android.js
91 lines (83 loc) · 4.04 KB
/
android.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
module.exports = {
createPayload: function createPayload(req) {
let payload = {
android: {},
data: {},
fcm_options: {
analytics_label: "androidV1Notification"
}
};
let updateRateLimits = true;
if (req.body.data){
// Handle the web actions by changing them into a format the app can handle
// https://www.home-assistant.io/integrations/html5/#actions
if (req.body.data.actions) {
for (let i = 0; i < req.body.data.actions.length; i++) {
const action = req.body.data.actions[i];
if (action.action){
payload.data["action_"+(i+1)+"_key"] = action.action;
}
if (action.title) {
payload.data["action_"+(i+1)+"_title"] = action.title;
}
if (action.uri){
payload.data["action_"+(i+1)+"_uri"] = action.uri;
}
if (action.behavior){
payload.data["action_"+(i+1)+"_behavior"] = action.behavior;
}
}
}
// Allow setting of ttl
// https://firebase.google.com/docs/reference/admin/node/admin.messaging.AndroidConfig.html#optional-ttl
if (req.body.data.ttl){
payload.android.ttl = req.body.data.ttl;
}
// https://firebase.google.com/docs/reference/admin/node/admin.messaging.AndroidConfig.html#optional-priority
if (req.body.data.priority){
payload.android.priority = req.body.data.priority;
}
// https://firebase.google.com/docs/reference/admin/node/admin.messaging.AndroidNotification.html
for (const key of [
'icon', 'color', 'sound', 'tag', 'clickAction',
'bodyLocKey', 'bodyLocArgs', 'titleLocKey', 'titleLocArgs', 'channel',
'ticker', 'sticky', 'eventTime', 'localOnly', 'notificationPriority',
'defaultSound', 'defaultVibrateTimings', 'defaultLightSettings', 'vibrateTimings',
'visibility', 'notificationCount', 'lightSettings', 'image', 'timeout', 'importance',
'subject', 'group', 'icon_url', 'ledColor', 'vibrationPattern', 'persistent',
'chronometer', 'when', 'when_relative', 'alert_once', 'intent_class_name', 'notification_icon',
'ble_advertise', 'ble_transmit', 'video', 'high_accuracy_update_interval',
'package_name', 'tts_text', 'media_stream', 'command', 'intent_package_name',
'intent_action', 'intent_extras', 'media_command', 'media_package_name', 'intent_uri',
'intent_type', 'ble_uuid', 'ble_major', 'ble_minor', 'confirmation',
'app_lock_enabled', 'app_lock_timeout', 'home_bypass_enabled', 'car_ui', 'ble_measured_power',
'progress', 'progress_max', 'progress_indeterminate'
]) {
if (req.body.data[key]){
payload.data[key] = String(req.body.data[key]);
}
}
}
// Always put message, title, and image in data so that the application can handle creating
// the notifications. This allows us to safely create actionable/imaged notifications.
if (req.body.message) {
payload.data.message = req.body.message;
const messages_to_ignore = ['request_location_update', 'clear_notification', 'remove_channel', 'command_dnd',
'command_ringer_mode', 'command_broadcast_intent','command_volume_level', 'command_screen_on',
'command_bluetooth', 'command_high_accuracy_mode', 'command_activity', 'command_app_lock', 'command_webview', 'command_media',
'command_update_sensors', 'command_ble_transmitter', 'command_persistent_connection', 'command_stop_tts',
'command_auto_screen_brightness', 'command_screen_brightness_level', 'command_screen_off_timeout', 'command_flashlight'];
if (messages_to_ignore.includes(req.body.message)) {
updateRateLimits = false;
}
}
if (req.body.title) {
payload.data.title = req.body.title;
}
// Include webhook ID to allow distinguishing which notify service sent this.
if (req.body.registration_info.webhook_id) {
payload.data.webhook_id = req.body.registration_info.webhook_id;
}
return { updateRateLimits: updateRateLimits, payload: payload };
}
};