Ashif Shereef
Ashif Shereef

Reputation: 494

APNS push notifications not being sent

Actually I am new to APNS and I have been using some help from the online forums and blogs. I am using PHP to implement the server side. The following is my PHP code

<?php

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

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

// Put your alert message here:
$message = 'My first push notification!';

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

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $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);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
    'alert' => $message,
    '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));

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

' . PHP_EOL;

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

I have converted the .p12 file to a PEM file and named it as ck.pem and has hosted it on the same location as the php file resides. When I execute the PHP file, the following gets printed. Is there something I am missing. I am doubtful about the certificate part.

Connected to APNS
Message successfully delivered

Upvotes: 0

Views: 744

Answers (2)

Ashif Shereef
Ashif Shereef

Reputation: 494

I actually replaced the UDID of the device with the Device Token and it works like a charm. You cannot use UDID anymore.

Upvotes: 0

MDP77
MDP77

Reputation: 69

I had the exact same problem, using the exact same example code - works perfect for android but fails for IOS with no returning error.

To fix my problem I had to create the SSL cert again against the app and then run

openssl x509 -in aps_development.cer -inform der -out  aps_development.pem

openssl pkcs12 -nocerts -out  aps_developmentKey.pem -in ios_development.p12

cat aps_development.pem aps_developmentKey.pem > ck.pem

Finially tested against: openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert aps_development.pem -key aps_developmentKey.pem -CAfile entrust_2048_ca.cer

After this everything worked perfect, that was on the 29th Sept - Its now stopped working so I think its the Certs again but at least it may help you with your solution.

Upvotes: 1

Related Questions