drxzpo70788179614 2014-06-07 08:05
浏览 55
已采纳

苹果推送通知 - PHP [关闭]

I have a problem when i use push Notification, It works when i use develop, but when i want to use it for real, it won't work.

The ck is made for production.

When i use it, the result is: Message successfully delivered But my phone do not receive the message

<?php

pushNotification('wow2','DeviceToken');

function pushNotification($theMessage, $theDeviceToken)
{

        // Put your device token here (without spaces):
    $deviceToken = $theDeviceToken;     

    // Put your private key's passphrase here:
    $passphrase = 'Code';

    // Put your alert message here:
    $message = $theMessage;

    ////////////////////////////////////////////////////////////////////////////////

    $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.push.apple.com:2195', $err,
        $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

    if (!$fp)
        //exit("Failed to connect: $err $errstr" . PHP_EOL);
    exit("" . 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;

    fclose($fp);


}
?>      

Can anyone see what's wrong? :)

  • 写回答

3条回答 默认 最新

  • dongluo8439 2014-06-07 10:48
    关注

    there are 3 types of problem faced at here on APNS

    1. your Device token does not match
    2. your .pem does not a valid one, the reason is .p12 and .cer files are corrupted some times , so delete the .p12 file and .cer and create again once.
    3. is not a worth able answer but by chance it will be occur -- check your device notification is ON/OFF
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?