2
votes

I am using the new plugin "phonegap-plugin-push" which override the old PushPlugin for push notification with cordova Apps.

The notifications are working perfectly on both android and iOS 8 but when I use iOS 9 it registered successfully and return the token and the back-end code return success but the device doesn't receive the notification!

here is front-end code

var push = PushNotification.init({ "android": {"senderID": "12345679"},
     "ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {} } );

push.on('registration', function(data) {
    var token = data.registrationId
});

push.on('notification', function(data) {
    // data.message,
    // data.title,
    // data.count,
    // data.sound,
    // data.image,
    var data =  data.additionalData
});

push.on('error', function(e) {
    // e.message
}); 

and here is my back-end code

$deviceToken = "019451814eff224c5dceca49b34b7b635d0716c21da2a77e7fd0809fd508d6z4";
$passphrase = 'myPassPhrase';

$message = 'You have recieved new 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',
'data' => 'test data'
);

// 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 'Message successfully delivered' . PHP_EOL;

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

}

Any help with that please?! Thanks in advance

1

1 Answers

3
votes

I found the solution of my problem,

You can find detailed answer in this article

Actually it's not certain thing but it's combination of more than one factor 1- be sure to use Xcode 7.1.1 (latest stable version till now) 2- be sure to create APNS Production NOT DEVELOPMENT certificate then download it

enter image description here

3- drag it or open it with keychain access expand it and export the private key as yourAppNameKey.p12

enter image description here

4- then we need to generate the pem file for the certificate,So via terminal write :

 openssl x509 -in aps_production.cer -inform der -out yourAppNameCert.pem

Note : ins the last step we used the certificate which we downloaded in step 2

5- now we will Convert the private key’s .p12 file into a .pem file:

openssl pkcs12 -nocerts -out yourAppNameKey.pem -in yourAppNameKey.p12 

Note : you will be asked to enter the password you used to export the private key and insert a pass phrase and confirm it to use in the server side code

6 - Finally we will combine the certificate and key into a single .pem file:

cat PushChatCert.pem PushChatKey.pem > ck.pem

here is a sample of the server side code

Hope it works with everyone .. Thanks