php apns notification source code

Solutions on MaxInterview for php apns notification source code by the best coders in the world

showing results for - "php apns notification source code"
Nicole
01 Sep 2016
1 <?php
2        /* We are using the sandbox version of the APNS for development. For production
3        environments, change this to ssl://gateway.push.apple.com:2195 */
4        $apnsServer = 'ssl://gateway.sandbox.push.apple.com:2195';
5        /* Make sure this is set to the password that you set for your private key
6        when you exported it to the .pem file using openssl on your OS X */
7        $privateKeyPassword = '1234';
8        /* Put your own message here if you want to */
9        $message = 'Welcome to iOS 7 Push Notifications';
10        /* Pur your device token here */
11        $deviceToken =
12        '05924634A8EB6B84437A1E8CE02E6BE6683DEC83FB38680A7DFD6A04C6CC586E';
13        /* Replace this with the name of the file that you have placed by your PHP
14        script file, containing your private key and certificate that you generated
15        earlier */
16        $pushCertAndKeyPemFile = 'PushCertificateAndKey.pem';
17        $stream = stream_context_create();
18        stream_context_set_option($stream,
19        'ssl',
20        'passphrase',
21        $privateKeyPassword);
22        stream_context_set_option($stream,
23        'ssl',
24        'local_cert',
25        $pushCertAndKeyPemFile);
26
27        $connectionTimeout = 20;
28        $connectionType = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
29        $connection = stream_socket_client($apnsServer,
30        $errorNumber,
31        $errorString,
32        $connectionTimeout,
33        $connectionType,
34        $stream);
35        if (!$connection){
36        echo "Failed to connect to the APNS server. Error no = $errorNumber<br/>";
37        exit;
38        } else {
39        echo "Successfully connected to the APNS. Processing...</br>";
40        }
41        $messageBody['aps'] = array('alert' => $message,
42        'sound' => 'default',
43        'badge' => 2,
44        );
45        $payload = json_encode($messageBody);
46        $notification = chr(0) .
47        pack('n', 32) .
48        pack('H*', $deviceToken) .
49        pack('n', strlen($payload)) .
50        $payload;
51        $wroteSuccessfully = fwrite($connection, $notification, strlen($notification));
52        if (!$wroteSuccessfully){
53        echo "Could not send the message<br/>";
54        }
55        else {
56        echo "Successfully sent the message<br/>";
57        }
58        fclose($connection);
59
60  ?>