node-apn / node-apn

:calling: Apple Push Notification module for Node.js
MIT License
4.37k stars 681 forks source link

How to use P12 files to send pushes #725

Open mikemilla opened 11 months ago

mikemilla commented 11 months ago

How do I fix this? What is going on here? What am I missing?


I am using a P12 generated the same way as this tutorial: https://youtu.be/jq42brrSg28

Here is what my code looks like:

const apn = require('apn');

async function test() {

  // ⚠️ This token is valid and from an actual iOS device with the same bundle id
  var deviceToken = 'f6209a7b84f17e4fa601e2a37b53937ff2843c24cc86eee2b7f1a0393555fbe3';

  var apnProvider = new apn.Provider({
    pfx: 'Certificates.p12',
    passphrase: 'password',
    production: false
  });

  let notification = new apn.Notification();

  notification.alert = {
    title: 'Hey',
    body: 'Whats up'
  };

  const res = await apnProvider.send(notification, deviceToken)
  console.log(res)

}

test()

I get the following error in the console when running:

{
  sent: [],
  failed: [
    {
      device: 'f6209a7b84f17e4fa601e2a37b53937ff2843c24cc86eee2b7f1a0393555fbe3',
      error: Error: unsupported
          at configSecureContext (node:internal/tls/secure-context:277:15)
          at Object.createSecureContext (node:_tls_common:117:3)
          at Object.connect (node:_tls_wrap:1629:48)
          at Endpoint.connect [as _connect] (/Users/xxx/Desktop/Folders/test/node_modules/apn/lib/protocol/endpoint.js:87:37)
          at new Endpoint (/Users/xxx/Desktop/Folders/test/node_modules/apn/lib/protocol/endpoint.js:44:10)
          at EndpointManager.createEndpoint (/Users/xxx/Desktop/Folders/test/node_modules/apn/lib/protocol/endpointManager.js:51:22)
          at EndpointManager.getStream (/Users/xxx/Desktop/Folders/test/node_modules/apn/lib/protocol/endpointManager.js:32:12)
          at /Users/xxx/Desktop/Folders/test/node_modules/apn/lib/client.js:121:43
          at new Promise (<anonymous>)
          at Client.getStream (/Users/xxx/Desktop/Folders/test/node_modules/apn/lib/client.js:120:12)
    }
  ]
}

Why is this error happening?

mikemilla commented 11 months ago

Anybody know what's going on here?

mtrezza commented 11 months ago

I can confirm that I've also observed the issue, on Node 18, but not on Node 16 or 14. It presumably exists since Node 17 due to the security deprecation of RC2, see https://github.com/nodejs/node/issues/40672 for more details.

mikemilla commented 11 months ago

This is a node 18 issue. Thanks @mtrezza. Downgrading to node 16 works.

Would still be nice to find a working solution for node 18

mtrezza commented 11 months ago

To be exact, it's more a certificate cipher issue than a Node issue. The certificate needs a more secure cipher because by default Node >=17 rejects RC2.

ayushforcebolt commented 7 months ago

"An 'error: unsupported' message may occur in Node.js 17 RC2 because the legacy OpenSSL provider is disabled by default. If you enable the --openssl-legacy-provider flag, it should work as expected."

Ref:- https://github.com/nodejs/node/issues/40672

How to enable flag:- node --openssl-legacy-provider index.js(Root file)

ayushforcebolt commented 7 months ago

Anybody know what's going on here?

You have to enable --openssl-legacy-provider

dchahla commented 7 months ago

bad cert handling try easy-apn (npm) it should fix your problem. no deps! 7.7kb unpacked.