1

Hello to all programmers. I get a system (php/MySQL) for making some changes. One of the problem I find is Ios push notifications. Code is here

function send_notification_ios($cert,$passphrase) {
var $error;
var $errorString;
$keyPath = dirname(__FILE__) . '/' .$cert.'.pem';
$keyPassword = $passphrase;
$gateway = 'gateway.push.apple.com:2195';
$context = stream_context_create();
if (!stream_context_set_option($context, 'ssl', 'local_cert', $certificate)){
  return json_encode(array('success' => 0, 'failure' => 1, 'reason'=>'stream_context local_cert'));
}
if(!stream_context_set_option($context, 'ssl', 'passphrase', $passphrase)){
  return json_encode(array('success' => 0, 'failure' => 1, 'reason'=>'stream_context passphrase'));
}
$socket = stream_socket_client($gateway, $error, $errorString, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $context);
if (!$socket) {
  return json_encode(array('success' => 0, 'failure' => 1, 'reason'=>'soket: '.$error.' '.$errorString));
}
$payload = json_encode([
  'aps' => [
    'alert' => 'Hello World',
    'sound' => 'default',
    'badge' => 1
  ]
]);
$payPack = chr(2).pack('n', strlen($payload)).$payload;
$expDate = chr(3).pack('n', 4).pack('N', time() + 86400);
$priority = chr(4).pack('n', 1).chr(10);
$inner = 
  chr(1).pack('n', 32).pack('H*', $apikey)
  .$payPack
  .$expDate
  .$priority;
$notification = 
  chr(2)
  .pack('N',strlen($inner))
  .$inner;
stream_set_blocking($socket, 0);
$socketResult = fwrite($socket,$notification,strlen($notification));
if($socketResult){
  $success++;
}
else{
  $failure++;
}
fclose($socket);
return json_encode(array('success' => $success, 'failure' => $failure);
}

The result of this function is {success:1,failure:0). But ios application doesn't receive notification. (for example android app receives android notification, sent from this system without any problem). All advises will be appreciated.

Aravind A R
  • 2,674
  • 1
  • 15
  • 25
  • did you perform all setup process for iOS device & certificate setting steps etc – Gagan_iOS Jun 13 '17 at 11:51
  • To @Gagan_iOS I think yes. There are 2 certificate files (*.pem) in the system and 2 passphrase in database. Ios app works without problem - log in, getting messages, etc.(except push notification) – Adeptus Mechanicus Jun 13 '17 at 11:55
  • https://stackoverflow.com/questions/14563097/sending-multiple-iphone-push-notifications-apns-php-tutorial this link helping you – Vinod Kumar Jun 13 '17 at 11:56
  • there is another certificate for push support. Did you generated and install in your key chain? You can open Keychain & see into that. – Gagan_iOS Jun 13 '17 at 11:56
  • To @Gagan_iOS I changed old certificate with new one. But this doesn't resolve the problem. – Adeptus Mechanicus Jun 13 '17 at 13:29
  • let me explain you. there must be two certificates 1. for development 2. for Push notification & an associated provisioning profile with these certificates. Another point your appid must be enable with APNS – Gagan_iOS Jun 13 '17 at 13:33
  • To @Gagan_iOS. Frankly speaking I didn't understand. I create a push notification certificate. Placed it on server. What one more certificate I need? For what development? – Adeptus Mechanicus Jun 13 '17 at 13:46
  • To @Gagan_iOS P.S. I use this manual to create push certificate https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/AddingCapabilities/AddingCapabilities.html#//apple_ref/doc/uid/TP40012582-CH26-SW6 – Adeptus Mechanicus Jun 13 '17 at 13:52
  • ok, are you also working on iOS code & settings – Gagan_iOS Jun 13 '17 at 13:55
  • To @Gagan_iOS. Nope. I work only on the server side. – Adeptus Mechanicus Jun 13 '17 at 14:04
  • ok just ask iOS developer, whether e/he has done all the settings – Gagan_iOS Jun 13 '17 at 14:05
  • As I say he use this manual https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/AddingCapabilities/AddingCapabilities.html#//apple_ref/doc/uid/TP40012582-CH26-SW6 – Adeptus Mechanicus Jun 13 '17 at 14:40

0 Answers0