How to connect to apn with PHP using p8 auth key file
Asked Answered
I

2

10

After Apple changed the APNs Auth Key to p8, the current libraries such as https://github.com/immobiliare/ApnsPHP still use old pem and cert files to connect

$push = new ApnsPHP_Push(
    ApnsPHP_Abstract::ENVIRONMENT_SANDBOX,
    'server_certificates_bundle_sandbox.pem'
);
// Set the Provider Certificate passphrase
// $push->setProviderCertificatePassphrase('test');
// Set the Root Certificate Autority to verify the Apple remote peer
$push->setRootCertificationAuthority('entrust_root_certification_authority.pem');
// Connect to the Apple Push Notification Service
$push->connect()

With Node.js example (https://eladnava.com/send-push-notifications-to-ios-devices-using-xcode-8-and-swift-3/), I could send like this:

var apnProvider = new apn.Provider({
     token: {
        key: 'APNsAuthKey_Q34DLF6Z6J.p8', // Path to the key p8 file
        keyId: 'Q34DLF6Z6J', // The Key ID of the p8 file (available at https://developer.apple.com/account/ios/certificate/key)
        teamId: 'RLAHF6FL89', // The Team ID of your Apple Developer Account (available at https://developer.apple.com/account/#/membership/)
    },
    production: false // Set to true if sending a notification to a production iOS app
});

How can I use PHP to send remote notifications to iOS like I do in node.js?

Inappetence answered 9/11, 2016 at 5:3 Comment(5)
Do you have a solution? :)Responser
@Responser not yet :)Inappetence
I'm looking for a way to do this also, looks like I'm going to have to make a solutionLawerencelawes
Looking into this now (developer.apple.com/library/content/documentation/…)Lawerencelawes
This library supports what you need github.com/edamov/pushokSucrose
U
4

Maybe its too late but I had struggle with this because almost every instruction is for the old way.

So i decided to answer this "old" question with my solution.

private static function sendNotificationAPN($device_token, $title, $body, $category, $sound, $optionals) {
        $alert = array(
            'aps' => array(
                'alert'    => array(
                    'title' => $title,
                    'body'  => $body
                ),
                'badge' => 0,
                'sound'    => $sound,
                'category' => $category,
                'content-available' => true
            )
        );

        foreach ($optionals as $key => $option) {
            $alert[$key] = $option;
        }

        $alert = json_encode($alert);

        $url = 'https://api.development.push.apple.com/3/device/' . $device_token['apn'];

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $alert);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("apns-topic: '/* your app name */'"));
        curl_setopt($ch, CURLOPT_SSLCERT, /* absolute path to cert file */);
        curl_setopt($ch, CURLOPT_SSLCERTPASSWD, /* passphrase for cert */);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        $response = curl_exec($ch);
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);


        $ret = array(
            'body' => $response,
            'httpcode' => $httpcode
        );

        return $ret;
    }
Ulceration answered 27/6, 2018 at 13:53 Comment(0)
L
-2

Sorry to be so late to the game. If i understand your question correctly I believe this is what your looking for. This is what I use to send messages to Apple APNS using PHP. You may have to do some research on the payload as there are a few ways to structure it depending on the way you coded your app. Also, keep in mind you must be able to use port 2195 for this to work. If you running a dedicated or in-house server you should be fine. If its a shared server it will not work.

    $passphrase = 'xxxxx'; // This is the passphrase used for file ck.pem
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); // ck.pem file must be included to sent token to ios devices 
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
    stream_context_set_option($ctx, 'ssl', 'verify_peer', true);
    stream_context_set_option($ctx, 'ssl', 'allow_self_signed', true);
    $fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
    stream_set_blocking ($fp, 0); // Ensure that blocking is disabled

    if (!$fp) {
        $fds = "Failed to connect: $err $errstr" . PHP_EOL;
        return false;
    } else {

        // Create the payload body
        // this example uses a custom data payload.  depending on your app you may need to change this
        $body['aps'] = array('alert' => $message, 'sound' => 'default', 'badge' => 1);
        // Encode the payload as JSON
        $payload = json_encode($body);
        // Build the binary notification
        $msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '',$token)) . pack('n', strlen($payload)) . $payload;
        // Send it to the server
        $result = fwrite($fp, $msg, strlen($msg));
        // Close the connection to the server
        fclose($fp);
    }
Lack answered 9/5, 2017 at 12:31 Comment(1)
Please use new JWT authentication.Muntin

© 2022 - 2024 — McMap. All rights reserved.