Created
May 1, 2020 12:46
-
-
Save saminadhikari/fdb688cd8bf8244ebf552fd8ccc29c42 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const http2 = require('http2'); | |
| const fs = require('fs'); | |
| /* | |
| Use 'https://api.push.apple.com' for production build | |
| */ | |
| host = 'https://api.sandbox.push.apple.com' | |
| path = '/3/device/{you device token}' | |
| /* | |
| Using certificate converted from p12. | |
| The code assumes that your certificate file is in same directory. | |
| Replace/rename as you please | |
| */ | |
| const client = http2.connect(host, { | |
| key: fs.readFileSync(__dirname + '/newfile.key.pem'), | |
| cert: fs.readFileSync(__dirname + '/newfile.crt.pem') | |
| }); | |
| client.on('error', (err) => console.error(err)); | |
| body = { | |
| "aps": { | |
| "alert": "hello", | |
| "content-available": 1 | |
| } | |
| } | |
| headers = { | |
| ':method': 'POST', | |
| 'apns-topic': 'com.xxxxxx.xxxxxxx', //you application bundle ID | |
| ':scheme': 'https', | |
| ':path': path | |
| } | |
| const request = client.request(headers); | |
| request.on('response', (headers, flags) => { | |
| for (const name in headers) { | |
| console.log(`${name}: ${headers[name]}`); | |
| } | |
| }); | |
| request.setEncoding('utf8'); | |
| let data = '' | |
| request.on('data', (chunk) => { data += chunk; }); | |
| request.write(JSON.stringify(body)) | |
| request.on('end', () => { | |
| console.log(`\n${data}`); | |
| client.close(); | |
| }); | |
| request.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment