forked from phonegap/phonegap-plugin-push
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpushADM.js
158 lines (106 loc) · 4.58 KB
/
pushADM.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
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
148
149
150
151
152
153
154
155
156
// Client ID and Client Secret received from ADM
// For more info, see: https://developer.amazon.com/public/apis/engage/device-messaging/tech-docs/02-obtaining-adm-credentials
var CLIENT_ID = "amzn1.application-oa2-client.8e838f6629554e26ae3f43a6c663cd60";
var CLIENT_SECRET = "0af96083320f5d70dc4f358cc783ac65a22e78b297ba257df34d5f723f24543f";
// Registration ID, received on device after it registers with ADM server
var REGISTRATION_IDS = ["amzn1.adm-registration.v2.Y29tLmFtYXpvbi5EZXZpY2VNZXNzYWdpbmcuUmVnaXN0cmF0aW9uSWRFbmNyeXB0aW9uS2V5ITEhOE9rZ2h5TXlhVEFFczg2ejNWL3JMcmhTa255Uk5BclhBbE1XMFZzcnU1aFF6cTlvdU5FbVEwclZmdk5oTFBVRXVDN1luQlRSNnRVRUViREdQSlBvSzRNaXVRRUlyUy9NYWZCYS9VWTJUaGZwb3ZVTHhlRTM0MGhvampBK01hVktsMEhxakdmQStOSXRjUXBTQUhNU1NlVVVUVkFreVRhRTBCYktaQ2ZkUFdqSmIwcHgzRDhMQnllVXdxQ2EwdHNXRmFVNklYL0U4UXovcHg0K3Jjb25VbVFLRUVVOFVabnh4RDhjYmtIcHd1ZThiekorbGtzR2taMG95cC92Y3NtZytrcTRPNjhXUUpiZEk3QzFvQThBRTFWWXM2NHkyMjdYVGV5RlhhMWNHS0k9IW5GNEJMSXNleC9xbWpHSU52NnczY0E9PQ"];
// Message payload to be sent to client
var payload = {
data: {
message: "PushPlugin works!!",
sound: "beep.wav",
url: "http://www.amazon.com",
timeStamp: new Date().toISOString(),
foo: "baz"
},
consolidationKey: "my app",
expiresAfter: 3600
};
//*********************************
var https = require("https");
var querystring = require("querystring");
if(CLIENT_ID == "" || CLIENT_SECRET == "" || REGISTRATION_IDS.length == 0){
console.log("******************\nSetup Error: \nYou need to edit the pushADM.js file and enter your ADM credentials and device registration ID(s).\n******************");
process.exit(1);
}
// Get access token from server, and use it to post message to device
getAccessToken(function(accessToken){
for(var i = 0; i < REGISTRATION_IDS.length; i++){
var registrationID = REGISTRATION_IDS[i];
postMessage(accessToken, registrationID, payload);
}
});
// Query OAuth server for access token
// For more info, see: https://developer.amazon.com/public/apis/engage/device-messaging/tech-docs/05-requesting-an-access-token
function getAccessToken(callback){
console.log("Requesting access token from server...");
var credentials = {
scope: "messaging:push",
grant_type: "client_credentials",
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET
}
var post_data = querystring.stringify(credentials);
var post_options = {
host: "api.amazon.com",
port: "443",
path: "/auth/O2/token",
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
}
};
var req = https.request(post_options, function(res) {
var data = "";
res.on("data", function (chunk) {
data += chunk;
});
res.on("end", function() {
console.log("\nAccess token response:", data);
var accessToken = JSON.parse(data).access_token;
callback(accessToken);
});
});
req.on("error", function(e) {
console.log("\nProblem with access token request: ", e.message);
});
req.write(post_data);
req.end();
}
// Post message payload to ADM server
// For more info, see: https://developer.amazon.com/public/apis/engage/device-messaging/tech-docs/06-sending-a-message
function postMessage(accessToken, registrationID, payload){
if(accessToken == undefined || registrationID == undefined || payload == undefined){
return;
}
console.log("\nSending message...");
var post_data = JSON.stringify(payload);
var api_path = "/messaging/registrations/" + registrationID + "/messages";
var post_options = {
host: "api.amazon.com",
port: "443",
path: api_path,
method: "POST",
headers: {
"Authorization": "Bearer " + accessToken,
"X-Amzn-Type-Version": "[email protected]",
"X-Amzn-Accept-Type" : "[email protected]",
"Content-Type": "application/json",
"Accept": "application/json",
}
};
var req = https.request(post_options, function(res) {
var data = "";
res.on("data", function (chunk) {
data += chunk;
});
res.on("end", function() {
console.log("\nSend message response: ", data);
});
});
req.on("error", function(e) {
console.log("\nProblem with send message request: ", e.message);
});
req.write(post_data);
req.end();
}