node-apn / node-apn

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

How to use a key string instead of a p8 file in config? #687

Open lamlejobchat opened 3 years ago

lamlejobchat commented 3 years ago

Currently. I'm using config bellow with a p8 file (APNsAuthKey_XXXXXXXXXX.p8). I found that in the p8 file, content is a string of a private key. How to use a key string instead of a p8 file?

const options = { token: { key: 'path/to/APNsAuthKey_XXXXXXXXXX.p8', keyId: process.env.APPLE_KEY_ID, teamId: process.env.APPLE_TEAM_ID, }, production: process.env.APP_ENV === 'prod', };

se02951 commented 3 years ago

Currently. I'm using config bellow with a p8 file (APNsAuthKey_XXXXXXXXXX.p8). I found that in the p8 file, content is a string of a private key. How to use a key string instead of a p8 file?

const options = { token: { key: 'path/to/APNsAuthKey_XXXXXXXXXX.p8', keyId: process.env.APPLE_KEY_ID, teamId: process.env.APPLE_TEAM_ID, }, production: process.env.APP_ENV === 'prod', };

@lamlejobchat As i know just have 2 option: string (path to your key file) and Buffer and if you dont want to use p8 file, you can use option below. let bufferKey = fs.readFileSync('path/to/APNsAuthKey_XXXXXXXXXX.p8'); const options = { token: { key: bufferKey , keyId: process.env.APPLE_KEY_ID, teamId: process.env.APPLE_TEAM_ID, },

alienproX commented 2 years ago

you could do it like below.

const key = `-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCxxxxxxxxxxxxxxx
-----END PRIVATE KEY-----`

const options = {
  token: {
    key: key,
    keyId: "xxxx",
    teamId: "xxxx"
  }
}