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

标题: php - iOS推送通知无法使用PHP [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:21
标题: php - iOS推送通知无法使用PHP

我正在尝试使用 APNS 库在 PHP 中的 iOS 中实现推送通知。这是我的代码:

<?php

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

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

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

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

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'Certificates.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));print_r($result);

if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);
?>

它显示消息发送成功,但在 iOS 中它不显示通知。

2015-07-28 08:32:39 Connected to APNS result = 97
2015-07-28 08:32:39 Message successfully delivered 
2015-07-28 08:32:39 Connection closed to APNS

为什么会这样?我错过了什么吗?



Best Answer-推荐答案


他的错误在于 ios 9,如果停止使用 objective-c 中的代码并在 swift 中使用,因为它在生成目标 token - C for ios9 时存在错误。 我建议紧急更改您的代码。 同样通过这个想法,当预定的 ios 更改为 swift 时,服务器也得到了解决,我的 php 几乎相等。 护理:

            $ msg = chr (0). pack ('N', 32). pack (H * ','. '$deviceToken [0].'). pack ('N', strlen ($ payload)). $ payload;     

关于php - iOS推送通知无法使用PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31668984/






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