0
votes

I'm trying to send notifications to an iOS app by using this php script that I found on the Internet. What value have I to put to $passphrase, and where I can find it ? The same for the values of:

  • stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
  • stream_context_set_option($ctx, 'ssl', 'passphrase', self::$passphrase);

class PushNotifications {
    private static $passphrase = 'joashp';

    public function __construct() {
        exit('Init function is not allowed');
    }

    public static function iOS($data, $devicetoken) {

        $deviceToken = $devicetoken;
        $ctx = stream_context_create();
        // ck.pem is your certificate file
        stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
        stream_context_set_option($ctx, 'ssl', 'passphrase', self::$passphrase);
        // Open a connection to the APNS server
        $fp = stream_socket_client(
            'ssl://gateway.sandbox.push.apple.com:2195', $err,
            $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
        if (!$fp)
            exit("Failed to connect: $err $errstr" . PHP_EOL);
        // Create the payload body
        $body['aps'] = array(
            'alert' => array(
                'title' => $data['mtitle'],
                'body' => $data['mdesc'],
            ),
            'sound' => 'default'
        );
        // Encode the payload as JSON
        $payload = json_encode($body);
        // Build the binary notification
        $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
        // Send it to the server
        $result = fwrite($fp, $msg, strlen($msg));

        // Close the connection to the server
        fclose($fp);
        if (!$result)
            return 'Message not delivered' . PHP_EOL;
        else
            return 'Message successfully delivered' . PHP_EOL;
    }

    // Curl
    private function useCurl(&$model, $url, $headers, $fields = null) {
        // Open connection
        $ch = curl_init();
        if ($url) {
            // Set the url, number of POST vars, POST data
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            // Disabling SSL Certificate support temporarly
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            if ($fields) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            }

            // Execute post
            $result = curl_exec($ch);
            if ($result === FALSE) {
                die('Curl failed: ' . curl_error($ch));
            }

            // Close connection
            curl_close($ch);

            return $result;
        }
    }

}
$msg_payload = array (
    'mtitle' => 'Test push notification title',
    'mdesc' => 'Test push notification body',
);

$deviceToken = 'FE66489F304DC75B8D6E8200DFF8A456E8DAEACEC428B427E9518741C92C6660';
PushNotifications::iOS($msg_payload, $deviceToken);

1
The function iOS is not declared static. You should instantiate the classRotimi
Thanks and then please?Jhon Kafka

1 Answers

0
votes

$passphrase is a password of your certificate file ck.pem file. You should ask that person who generated that pem file for a passphrase.

In many cases, a passphrase can be empty. In this case you probably need to provide an empty string.

If you do not have any knowledge what is that pem file - then you can read some guide, e.g. https://developers.connectycube.com/ios/how-to-create-apns-certificate

This guide tells you how to generate p12 certificate file that can be used to send Apple push notifications. Then you can get a pem file from that p12 using the following commands: https://gist.github.com/shahdhiren/9ca059ac0762f7ef0fcf71a79ed5b022