• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

php - iOS推送通知无法使用PHP

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:21:04 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试使用 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap