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 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘