-
Notifications
You must be signed in to change notification settings - Fork 30.4k
Expand file tree
/
Copy pathnode-pushnotifications-tests.ts
More file actions
121 lines (106 loc) · 2.91 KB
/
Copy pathnode-pushnotifications-tests.ts
File metadata and controls
121 lines (106 loc) · 2.91 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
import PushNotifications = require("node-pushnotifications");
import { supportedContentEncodings } from "web-push";
const settings: PushNotifications.Settings = {
gcm: {
id: "null",
},
fcm: {
appName: "null",
serviceAccountKey: require("path/to/serviceAccountKey.json"),
credential: null,
},
apn: {
token: {
key: "./certs/key.p8",
keyId: "ABCD",
teamId: "EFGH",
},
},
adm: {
client_id: "null",
client_secret: "null",
},
wns: {
client_id: "null",
client_secret: "null",
notificationMethod: "sendTileSquareBlock",
},
web: {
vapidDetails: {
subject: "< 'mailto' Address or URL >",
publicKey: "< URL Safe Base64 Encoded Public Key >",
privateKey: "< URL Safe Base64 Encoded Private Key >",
},
gcmAPIKey: "gcmkey",
TTL: 2419200,
contentEncoding: supportedContentEncodings.AES_128_GCM,
headers: {},
},
};
const push = new PushNotifications(settings);
const registrationIds = [];
registrationIds.push("INSERT_YOUR_DEVICE_ID");
registrationIds.push("INSERT_OTHER_DEVICE_ID");
registrationIds.push({
endpoint: "https://fcm.googleapis.com/fcm/send/...",
keys: {
auth: "...",
p256dh: "...",
},
});
const data = {
title: "New push notification",
body: "Powered by AppFeel",
};
function assertResultTypes(result: PushNotifications.Result) {
// $ExpectType MethodValue
result.method;
result.method === "apn";
result.method === "gcm";
result.method === "fcm";
result.method === "adm";
result.method === "wns";
result.method === "webPush";
result.method === "unknown";
result.method === "none";
// @ts-expect-error
result.method === "any_other_string";
// $ExpectType number
result.success;
// $ExpectType number
result.failure;
// $ExpectType Message[]
result.message;
// $ExpectType string
result.message[0].regId;
// $ExpectType string | undefined
result.message[0].originalRegId;
// $ExpectType string | undefined
result.message[0].messageId;
// $ExpectType Error | null | undefined
result.message[0].error;
// $ExpectType string | undefined
result.message[0].errorMsg;
}
// You can use it in node callback style
push.send(registrationIds, data, (err, results) => {
if (err) {
console.log(err);
} else {
// $ExpectType Result[]
results;
results.forEach(assertResultTypes);
console.log(results);
}
});
// Or you could use it as a promise and send only a single notifications:
push.send(registrationIds[0], data)
.then((results) => {
// $ExpectType Result[]
results;
results.forEach(assertResultTypes);
console.log(results);
})
.catch((err) => {
console.log(err);
});