A, certificate

1. Obtaining certificates

1. Create a project and configure bundleId

2. Bind bundleId to the AppleDeveloper website, enable the push service, and generate App IDs

3. Generate a CSR(Certificate Signing Request) file from the key string

4. Create aps_development.cer and aps_producation.cer certificates and bind App IDs and CSR files

2. Export of certificates

1. Download the certificate and double-click the. Cer file to import it to the keystring

2. Open the keychain and find the Apple Development/Producation Mac/iOS Push Services certificate in the Login options

3. Export the certificate and certificate private key as p12 files and save them as apns_dev_cert.p12 and apns_dev_privateKey.p12 respectively

3. Conversion and synthesis of certificate format

1. Run the openssl pkcs12-clcerts -nokeys -out apns_dev_cert.pem -in apns_dev_cert.p12 command to generate the apns_dev_cert.pem file

openssl pkcs12 -nocerts -out apns_dev_privateKey.pem -in apns_dev_privateKey.p12














Iv. Certificate verification

Gateway.sandbox.push.apple.com: 1. Implement openssl s_client – connect 2195 – cert apns_dev. Pem command, if there is a large character the verification is successful, Unable to load client certificate private key file: unable to load client certificate private key file: Unable to load client certificate private key file: Unable to load client certificate private key file

Obtain deviceToken

1. Registered in applicationDidFinishLaunching remote notifications

Third, the node. Js

1. Installation node. Js

1. Nodejs.org

2. Install the apn

1. Run sudo NPM install apn 2. Possible errors and solutions:

  1. No such file or directory, open ‘~/package.json’ Run the NPM init command
  2. NPM WARN [email protected] No description NPM WARN [email protected] No repository field. Open package.json and add two keys

3. Configure the node_modules environment variable

1. Access the current user directory and run $touch ~/. Bash_profile to create the. Bash_profile file

2. Open the. Bash_profile file, add the following content, and save export NODE_PATH=/usr/local/lib/node_modules/

4. Create the edit JS file

     "use strict";
     const apn = require("apn"); / / token arraylet tokens = ["d4e356af1b3b4624fb0cde4bf6c6ff5264b4a0506161df1b95c6825ea17b4150"]; // The obtained tokenlet service = new apn.Provider({
          cert: "/ The absolute path on your computer /apns-dev-cert.pem",
          key: "/ Absolute path on your computer /apns-dev-key.pem",
          gateway: "gateway.sandbox.push.apple.com",
          // gateway: "gateway.push.apple.com"; Online address port: 2195, port passphrase:"123456"Pem certificate password});let note = new apn.Notification({
        alert:  "Breaking News: I just sent my first Push Notification"}); Note. Topic ="testApns.com"
     console.log(`Sending: ${note.compile()} to ${tokens}`);
     service.send(note, tokens).then( result => {
     console.log("sent:", result.sent.length);
     console.log("failed:", result.failed.length);
        console.log(result.failed);
     });
     service.shutdown();
Copy the code

5. Run the js file

1. Execute Node apns.js on the terminal, wait a few seconds, and the notification will be sent to your application