immobiliare / ApnsPHP

ApnsPHP: Apple Push Notification & Feedback Provider
BSD 3-Clause "New" or "Revised" License
1.43k stars 452 forks source link

Support for p8 files? #148

Open jacksonkr opened 7 years ago

jacksonkr commented 7 years ago

Apple offers a never expiring p8 file you can use for signing to send push notifications.

Any chance this is in the works? I have a working shell script and a python script for this if that's helpful at all. I've attached the shell script.

#!/bin/bash

# Get curl with HTTP/2 and openssl with ECDSA: 'brew install curl openssl'
curl=/usr/local/opt/curl/bin/curl
openssl=/usr/local/opt/openssl/bin/openssl

# --------------------------------------------------------------------------

deviceToken=55555555555555555555555555555555555555555555555555

authKey="./APNsAuthKey.p8"
authKeyId=ABC123456789
teamId=ABC123456789
bundleId=com.domain.project
endpoint=https://api.development.push.apple.com

read -r -d '' payload <<-'EOF'
{
   "aps": {
      "badge": 2,
      "category": "mycategory",
      "alert": {
         "title": "my title",
         "subtitle": "my subtitle",
         "body": "my body text message"
      }
   },
   "custom": {
      "mykey": "myvalue"
   }
}
EOF

# --------------------------------------------------------------------------

base64() {
   $openssl base64 -e -A | tr -- '+/' '-_' | tr -d =
}

sign() {
   printf "$1"| $openssl dgst -binary -sha256 -sign "$authKey" | base64
}

time=$(date +%s)
header=$(printf '{ "alg": "ES256", "kid": "%s" }' "$authKeyId" | base64)
claims=$(printf '{ "iss": "%s", "iat": %d }' "$teamId" "$time" | base64)
signed=$(sign $header.$claims)
jwt="$header.$claims.$signed"

$curl --verbose \
   --header "content-type: application/json" \
   --header "authorization: bearer $jwt" \
   --header "apns-topic: $bundleId" \
   --data "$payload" \
   $endpoint/3/device/$deviceToken
justinkumpe commented 3 years ago

Any updates on this? using p8 files (APNS Key) is very useful as they never expire and one key can be used for all apps under your account instead of having a separate certificate for each app you wish to send notifications to.

SMillerDev commented 3 years ago

Since the company I work for wants to keep using ApnsPHP we added p8 support: https://github.com/M2mobi/ApnsPHP/releases/tag/2.0.0beta1