doumi9618 2015-08-27 11:16
浏览 53

使用php的IOS推送通知

I have create pushnotification for ios. Following coding i got from website. And the device token is from Our Apple device that i have changed.

<?php
$deviceToken = '0f744707bebcf74f9b7c25d48e3358945fxxx01da5ddb387462c7eaf61bbad78';
$msg_push="hi there";
$deviceToken = str_replace(" ", "", $deviceToken);
// Put your private key's passphrase here:
$passphrase = 'pushchat';
// Put your alert message here:
$message = $msg_push;
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'pushcert.pem');//'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 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
?>

The pushcert.pem file i have included in the same folder. Know when i run this page in server i get the msg"Connected to APNS Message successfully delivered". But i don't get the notification message in my apple device. So is any error in this code. What is wrong here?

  • 写回答

1条回答 默认 最新

  • douman6245 2015-08-27 11:33
    关注

    It can be because your app is in production environment then you have to remove the sandbox from the 'ssl://gateway.sandbox.push.apple.com:2195'

    or

    It may be because push notifications are not enabled in the apple account for the particular bundle identifier.It can also be because methods for push notification are not defined in the app.

    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler