iOS 13 PHP Script payload for Push notification

3.5k Views Asked by At

I just come to know that iOS 13 have changed Push notification device token and Payload. I have update App and published it. Now, We have server in Php, We send both alert and silent notification to the users. I have gone through "https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns" Apple Documentation, but I am still not able to understand how can I pass new headers in payload.

Here is the my Php Script.

<?php

// Put your device token here (without spaces):
$deviceToken = 'deviceToken';

// Put your private key's passphrase here:
$passphrase = 'dummyPassword';

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'pushcert.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
//stream_context_set_option($ctx, 'ssl', 'cafile', 'entrust_2048_ca.cer');

// 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);

echo 'Connected to APNS' . PHP_EOL;

    $body['aps'] = array(
                         'content-available' => '1',
                         'type' => 'message',
                         'msg' => 'We detected a failure when sending a message.'
                             );

    // Encode the payload as JSON
    $payload = json_encode($body);


    echo "\njson::::\n";
    print_r ($payload);

// 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));

if (!$result)
  echo 'Message not delivered' . PHP_EOL;
else
  echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);

Can anyone have any example, tutorial or PHP Script so I can create payload for Apple Push notification. Any help will be appreciated.

1

There are 1 best solutions below

1
On

I will try to help you out -

This will just work as a 'test' - it is not production but it will send you in the right direction -

So - A long long time ago Apple changed their push notifications to return information about the push and also get rid of their feedback server. That is part of the change you are having to work with.

Here is a basic PHP setup - for using the NEW PUSH AUTH -

What you will need to do is get a push key for your developer account - YOU GET ONE FOR ALL YOUR ACCOUNTS. You get this the same way you register for your other certs on the apple developer page -

Here is some sample PHP code to get you started - THIS REQUIRES ABOVE PHP 5.3 (I dont know the exact version)

<?php
const AUTH_KEY_PATH = '/path/to/AuthKey.p8';
const AUTH_KEY_ID = 'your auth key id here';
const TEAM_ID = 'your team id here';
const BUNDLE_ID = 'com.testApplication.me';
// Setup the payload
$payload = [
   'aps' => [
     'alert' => [
       'title' => 'This is the notification.',
     ],
     'sound'=> 'default',
   ],
];
//// Create The JWT
$header = base64_encode(json_encode([
             'alg' => 'ES256',
             'kid' => AUTH_KEY_ID
        ]));
$claims = base64_encode(json_encode([
             'iss' => TEAM_ID,
             'iat' => time()
        ]));
$pkey = openssl_pkey_get_private('file://' . AUTH_KEY_PATH);
openssl_sign("$header.$claims", $signature, $pkey, 'sha256');
$signed = base64_encode($signature);
$signedHeaderData = "$header.$claims.$signed";
//Setup curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_POSTFIELDS,
json_encode($payload));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
  'apns-topic: ' . BUNDLE_ID,
  'authorization: bearer ' . $signedHeaderData,
  'apns-push-type: alert'
]);
//Setting up URL
$token = $argv[1];
$url = "https://api.development.push.apple.com/3/device/$token";
//Making the call
curl_setopt($ch, CURLOPT_URL, "{$url}");
$response = curl_exec($ch);
// DEAL WITH IT ('it' being errors)
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

To use this you would

php -f push.php TOKEN

This is a 'test' setup - please do not use this for production because if you need to make A LOT of requests you want to reuse the connection.