-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPNTestServerNodeJS.js
More file actions
73 lines (64 loc) · 1.69 KB
/
Copy pathAPNTestServerNodeJS.js
File metadata and controls
73 lines (64 loc) · 1.69 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
const apn = require('apn')
const path = require('path')
const _ = require('lodash')
const deviceToken = ''; // Your current device token that you get from APN
const teamId = ''; // Team id https://developer.apple.com/account/#/membership
const keyId = ''; // APN key id
const appBundleKey = 'com.bundle.js'; // Your App Bundle
const certPath = path.resolve(__dirname) + '/key.p8'
class Notification {
constructor(settings) {
try {
this.engine = new apn.Provider(settings)
} catch (e) {
console.error('Invalid APN params', e)
}
}
sendNotification(deviceToken, message, payload = {}, params = {}) {
const defaultParams = {
badge: 3,
expiry: Math.floor(Date.now() / 1000) + 3600,
topic: params.topic,
}
if (!this.engine) {
throw new Error('No apn connection')
}
const notificationMessage = new apn.Notification(payload)
notificationMessage.alert = message
_.each(Object.assign(defaultParams, params), (v, k) => {
notificationMessage[k] = v
})
return this.engine.send(notificationMessage, deviceToken)
}
}
const [nodePath, script, message] = process.argv
if (!keyId || !teamId || !deviceToken || !appBundleKey || !message) {
console.log('Please add params to script [keyId, teamId, deviceToken, appBundleKey, message]')
process.exit(1)
}
const options = {
token: {
key: certPath,
keyId,
teamId,
},
production: false,
}
const notification = new Notification(options)
notification
.sendNotification(
deviceToken,
message,
{},
{
topic: appBundleKey,
}
)
.then((e, f) => {
console.log(e, f)
process.exit(1)
})
.catch(er => {
console.log(er)
process.exit(1)
})