php - 苹果推送通知 - PHP

标签 php ios push-notification

<分区>

我在使用推送通知时遇到问题,当我使用开发时它可以工作,但是当我想真正使用它时,它不会工作。

ck 是为生产而制造的。

当我使用它时,结果是:消息成功发送但是我的手机收不到消息

<?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\n' . 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);


}
?>      

谁能看出哪里出了问题? :)

最佳答案

APNS 上这里有 3 种类型的问题

  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

关于php - 苹果推送通知 - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24095177/

相关文章:

objective-c - 关闭 AlertView 时显示新 View

iphone - 在我的 iPhone 应用程序内读取 "Settings"应用程序中的通知标志

android - 如何将通知从我的服务器推送到我的 Android 应用程序?

ios - WatchKit 2.0 检测何时收到 APNS

php - mod_rewrite 问题

php - 将旧的 mysql_ 代码转换为 mysqli_ 查询

iOS 应用内购买 (IAP) 通过 Apple 托管内容

IOS - 应用内购买恢复完成的交易

php - 尽管 z-index 换行?

PHP + MySQL : Time as a string into numerics