OGeek|极客世界-中国程序员成长平台

标题: php - APNS PHP 不发送通知 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:41
标题: php - APNS PHP 不发送通知

我已经使用 PHP 设置了 APNS Provider Server。我没有错误地连接到 Apple 的 APNS 服务器,并且脚本似乎运行良好,循环通过所有设备并发送有效负载。但是,订阅通知的设备不会收到通知。有人可以帮我找出代码中的错误吗?

$message = $_POST['message'];
echo "osted Message: $message<br />";

$message = str_replace("'", "\'", $message);
echo "Formatted message: $message<br />";

$title = $_POST['title'];
$title = str_replace("'", "\'", $title);
echo "Formatted Title: $title<br />";

$category = $_POST['category'];
echo "Category: $category<br />";

$alert = array('title' => "$title", 'body' => "$message");
foreach ($alert as $key => $value){
    echo "Key: $key Value: $value<br />";
}

ini_set("display_errors",1);

$apnsHost = 'gateway.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apns.pem';

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
if ($error!="" || $errorString!=""){
    echo "Error Connecting: $error : $errorString<br />";
}
else{
    echo "No Error Connecting<br />";
}

$query = "INSERT INTO notifications (`title`, `message`, `category`) VALUES (\"$title\", \"$message\", \"$category\")";
echo "Insert Query: $query<br />";

$result1 = mysqli_query($connection, $query);

$query = "SELECT * FROM notifications ORDER BY notification_id DESC";
$result931 = mysqli_query($connection, $query);
$row = $result931->fetch_assoc();
$notification_id = $row[notification_id];

$query = "SELECT * FROM category_subscriptions WHERE category_id='$category' ORDER BY device_id";
$result1 = mysqli_query($connection, $query);
echo "Subscriber Query: $query<br />";
while($row = $result1->fetch_assoc()) {
    $device_id = $row[device_id];
    $query = "SELECT * FROM devices WHERE device_id='$device_id' ORDER BY device_id";
    $result = mysqli_query($connection,$query);

    while($r = $result->fetch_assoc()) {
        $deviceToken = $r[device_token];
        $badge = $r[badge_value] + 1;
        if ($r[accepts_alerts]==1) {
            if ($r[accepts_badges]==1) {
                if ($r[accepts_sounds]==1) {
                    $payload['aps'] = array('alert' => $alert, 'badge' => $badge, 'sound' => 'default');
                } else {
                    $payload['aps'] = array('alert' => $alert, 'badge' => $badge);
                }
            }
            else if ($r[accepts_sounds]==1) {
                    $payload['aps'] = array('alert' => $alert, 'sound' => 'default');
            } 
            else {
                $payload['aps'] = array('alert' => $alert);
            }
            $payload1 = json_encode($payload);
            echo "ayload: $payload1<br />";

            echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;Sending to Device Token: $deviceToken<br />";
            $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload1)) . $payload1;
            echo "APNS Message: $apnsMessage<br />";
            fwrite($apns, $apnsMessage);
            $query6 = "UPDATE devices SET badge_value = $badge WHERE device_id='$device_id'";
            $result6 = mysqli_query($connection,$query6);
            $query7 = "INSERT INTO notification_map (`device_id`, `notification_id`, `viewed`) VALUES ($device_id, $notification_id, \"false\")";
            $result7 = mysqli_query($connection,$query7);
        }
    }
}
fclose($apns);

截图: enter image description here



Best Answer-推荐答案


执行以下步骤:
1.检查pem文件是否有效
2. 检查你的服务器是否打开了2195端口。

3. 如果您的 pem 文件是开发模式,则使用“ssl://gateway.sandbox.push.apple.com:2195”连接到 apns,否则如果您的 pem 文件是生产模式,则使用“ssl://gateway.push.apple.com:2195".

  1. 检查密码是否正确。

检查此示例代码:

<?php

// Put your device token here (without spaces):
$deviceToken = 'fefb03ba6adcea310cf3f455dae16fec4f63b4ba4d96103c20d594a04efd7c2a';
// Put your private key's passphrase here:
$passphrase = 'Welcome@1';

// Put your alert message here:
$message = 'My first push notification!';

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

$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.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',
'title' => 'testing title'
);

// 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);

关于php - APNS PHP 不发送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41303313/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4