2
votes

iOS 13 now requires you to add the header of apns-push-type for either alert or background. How do I add this to my current code that I use to send notifications?

I've been sending push notifications using this solution for years but I don't know how to add the headers they're requiring.

function sendPushNotification($uid,$title,$message,$action,$type,$id){

$deviceToken = "*************";
$passphrase = '***********';

$ctx = stream_context_create();

stream_context_set_option($ctx, 'ssl', 'local_cert', 'pem/Certificates.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

// Open a connection to the APNS server

if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);

//echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
    'alert' => array(
        'title' => $title,
        'body' => $message,
        'action-loc-key' => 'Bango App',
    ),
    'mutable-content'=> 1,
    'badge' => 1,
    'content-available'=>1,
    'sound' => 'oven.caf',
    'action' => $action,
    'type' => $type,
    'id' => $id,

    );



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


fclose($fp);

// Close the connection to the server

}

?>```
1
I am facing same issue, have you found any solution for this? Please share if you found anything.nadim

1 Answers

0
votes

You can pass header along the $msg, in my code i have:

$msg = pack("C", 1) . pack("N",$row['id_device_token']) . pack("N", $apple_expiry) . pack("n", 32) . pack('H*', str_replace(' ', '', $row['device_token'])) . pack("n", strlen($payload1)) . $payload1;

where $row['id_device_token'] is "apns-id" and $apple_expiry is "apns-expiration". According to Apple (Apple Docs) the "apns-push-type" header must be placed before "apns-id", so:

$msg = pack("C", 1) . pack("N","alert") . pack("N",$row['id_device_token']) . pack("N", $apple_expiry) . pack("n", 32) . pack('H*', str_replace(' ', '', $row['device_token'])) . pack("n", strlen($payload1)) . $payload1;