-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrover-push-test.js
More file actions
executable file
·147 lines (127 loc) · 4.79 KB
/
rover-push-test.js
File metadata and controls
executable file
·147 lines (127 loc) · 4.79 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
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
141
142
143
144
145
146
147
#!/usr/bin/env node
const apn = require("apn")
const commander = require('commander')
const fs = require("fs")
const path = require("path")
const filePath = path.join(__dirname, "login.json")
const fileExists = fs.existsSync(filePath)
if (!fileExists) {
console.log("not logged in")
process.exit()
}
let program, provider, notification
Promise.resolve()
.then(buildProgram)
.then(validateOptions)
.then(configureProvider)
.then(buildNotification)
.then(sendNotification)
.then(validateResponse)
.then(() => {
console.info("Notification sent successfully!")
process.exit(0)
})
.catch(err => {
console.error(err)
process.exit(1)
})
function buildProgram() {
program = commander
.option("--certificate <file>", "Path to a valid APNS certificate")
.option("--passphrase <string>", "APNS certificate passphrase - required unless passphrase is blank")
.option("--bundle-id <string>", "The app's bundle identifier")
.option("--device-token <string>", "The device's push token")
.option("-D --development", "Use the development APNS environment")
.option("--title <string>", "A short string describing the purpose of the notification")
.option("--body <string>", "The text of the alert message", "Lorem ipsum sit dolor amet...")
.option("--badge <integer>", "The number displayed in badge of your app icon")
.option("--sound <string>", "The name of a sound file in your app's main bundle or in the Library/Sounds folder of your app's data container")
.option("-C --content-available", "Set this flag to send a silent notification")
.option("--category <string>", "A string value that represents the notification's type")
.option("--thread-id <string>", "A string value that represents the app-specific identifier for grouping notifications")
.option("-M --mutable-content", "Indicates this notification can be modified by a service extension")
.parse(process.argv)
}
function validateOptions() {
const requiredKeys = ["certificate", "bundleId", "deviceToken", "body"]
for (let i = 0; i < requiredKeys.length; i++) {
const key = requiredKeys[i]
if (program[key] === undefined) {
return Promise.reject(key + " is required")
}
}
let fileExists = fs.existsSync(program.certificate)
if (!fileExists) {
return Promise.reject("Certificate at path " + program.certificate + " does not exist")
}
let fileExtension = path.extname(program.certificate)
if (fileExtension !== ".p12") {
return Promise.reject("Certificate at path " + program.certificate + " is not a valid p12 file")
}
return Promise.resolve()
}
function configureProvider() {
provider = new apn.Provider({
pfx: fs.readFileSync(program.certificate),
production: program.development ? false : true,
passphrase: program.passphrase
})
}
function buildNotification() {
if (program.contentAvailable) {
notification = new apn.Notification({
"aps": {
"content-available": 1
},
"topic": program.bundleId,
"priority": 10
})
return
}
notification = new apn.Notification({
"aps": {
"alert": {
"title": program.title,
"body": program.body
},
"badge": program.badge,
"sound": program.sound,
"category": program.category,
"thread-id": program.threadId,
"rover": {
"notification": {
"id": "foo",
"campaignId": "bar",
"title": program.title,
"body": program.body,
"action": {
"__typename": "PresentExperienceAction",
"experienceId": "5873dee6d5bf3e002de4d70e"
},
"deliveredAt": "2017-11-22T13:01:48-05:00",
"expiresAt": "2017-12-24T00:00:00-05:00",
"isRead": false,
"isNotificationCenterEnabled": true
}
}
},
"topic": program.bundleId,
"priority": 10
})
}
function sendNotification() {
return provider.send(notification, program.deviceToken)
}
function validateResponse(response) {
if (response.failed.length >= 1) {
const failed = response.failed[0]
if (failed.error) {
return Promise.reject(failed.error)
}
if (failed.response && failed.response.reason) {
return Promise.reject(failed.response.reason)
}
return Promise.reject("Failed to send notification for an unknown reason")
}
return Promise.resolve()
}