php - 无法向 apns 发送消息?

标签 php ios apple-push-notifications

你好,我想给我的 iphone 发消息

我的 php 代码是

<?php

// set time limit to zero in order to avoid timeout
set_time_limit(0);

// charset header for output
header('content-type: text/html; charset: utf-8');

// this is the pass phrase you defined when creating the key
$passphrase = 'Zazimi16';

// you can post a variable to this string or edit the message here
//if (!isset($_POST['msg'])) {
//$_POST['msg'] = "Notification message here!";
//}

// tr_to_utf function needed to fix the Turkish characters
//$message = tr_to_utf($_POST['msg']);

$message = 'test';

// load your device ids to an array
$deviceIds = array(
'f67f045d934cdc1bfa9f41e333aa47d0841c4c833bc38c7252cb137c7067ecda',
);

// this is where you can customize your notification
$payload = '{"aps":{"alert":"' . $message . '","sound":"default"}}';

$result = 'Start' . '<br />';

////////////////////////////////////////////////////////////////////////////////
// start to create connection
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'push1.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

echo count($deviceIds) . ' devices will receive notifications.<br />';

foreach ($deviceIds as $item) {
    // wait for some time
    sleep(1);

    // 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" . '<br />');
    } else {
        echo 'Apple service is online. ' . '<br />';
    }

    // Build the binary notification
    $msg = chr(0) . pack('n', 32) . pack('H*', $item) . pack('n', strlen($payload)) . $payload;

    // Send it to the server
    $result = fwrite($fp, $msg, strlen($msg));

    if (!$result) {
        echo 'Undelivered message count: ' . $item . '<br />';
    } else {
        echo 'Delivered message count: ' . $item . '<br />';
    }

    if ($fp) {
        fclose($fp);
        echo 'The connection has been closed by the client' . '<br />';
    }
}

echo count($deviceIds) . ' devices have received notifications.<br />';

// function for fixing Turkish characters
function tr_to_utf($text) {
    $text = trim($text);
    $search = array('Ü', 'Þ', 'Ð', 'Ç', 'Ý', 'Ö', 'ü', 'þ', 'ð', 'ç', 'ý', 'ö');
    $replace = array('Ãœ', 'Åž', '&#286;ž', 'Ç', 'Ä°', 'Ö', 'ü', 'ÅŸ', 'ÄŸ', 'ç', 'ı', 'ö');
    $new_text = str_replace($search, $replace, $text);
    return $new_text;
}

// set time limit back to a normal value
set_time_limit(30);
?>

但是当我发送时我有它的文本

1 devices will receive notifications.
Apple service is online. 
Delivered message count: f67f045d934cdc1bfa9f41e333aa47d0841c4c833bc38c7252cb137c7067ecda
The connection has been closed by the client
1 devices have received notifications.

最佳答案

如果您使用开发配置文件运行,请确保您使用的是沙箱 url 进行连接,

$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);   

同时检查为应用程序编译的配置文件以及生成的 pem 文件是否正确。

关于php - 无法向 apns 发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37025706/

相关文章:

javascript - 如果使用 jquery/javascript 锁定 iphone,如何提交表单

ios - iOS 中如何查找 UIView 的 subview 的值变化

ios - 将设备唯一 token 发送到推送通知提供商

php - 找不到意外的 T_STRING

php - Mysql将一个表中的主键添加到另外两个表中

php - 有没有办法在 PHP 中测试 MongoDB 连接?

ios - APN设备 token 是否依赖于推送通知权限

php - MySQL 服务器版本,用于在第 6 行 'AND epg_data.start' ' ORDER BY epg_data.start DESC' 附近使用正确的语法

ios - 如何在任何操作系统攻击的情况下保护 iOS 应用程序(在越狱设备上)

ios - 如果我想实现推送通知,我必须多久向服务器发送一次设备 token ?