2

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

}

?>```
Matthew Foster
  • 167
  • 1
  • 10
  • I am facing same issue, have you found any solution for this? Please share if you found anything. – nadim Oct 25 '19 at 10:44

1 Answers1

0

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;
Stefano Boeri
  • 61
  • 1
  • 2
  • I have tried this approach but it is not working for me. No error no notification. Even Apple docs ask 3 required fields. In you answer I found only "apns-push-type". – nadim Oct 25 '19 at 10:47
  • Did you modify also client side notification code? The way now Apple return device token is different in iOS 13. Read this: https://onesignal.com/blog/ios-13-introduces-4-breaking-changes-to-notifications/ – Stefano Boeri Oct 26 '19 at 11:27
  • Yes, I have modified. I have asked question as well. see this. https://stackoverflow.com/questions/58543104/ios-13-php-script-payload-for-push-notification – nadim Oct 26 '19 at 13:04