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 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器