iOS 推送通知无法使用 PHP [英] iOS push notification not working using PHP

查看:25
本文介绍了iOS 推送通知无法使用 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to implement push notification in iOS in PHP using APNS library. Here is my code :

<?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 中它不显示通知.

It show message sending successfully but in iOS it does not show notification.

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

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

Why this is happening? Am I missing anything ?

推荐答案

他的错误是在 ios 9 上,如果是这样,请停止使用 Objective C 中的代码并在 swift 中使用,因为它在生成 objectiv token - C for ios9 时有一个错误.我建议紧急更改您的代码.同样通过这个想法是当预定的 ios 更改为 swift 时服务器一切都解决了,我的 php 几乎相等.护理:

his mistake is with the ios 9 if so stop using the code in objective C and use in swift as it has a bug in generating objectiv token - C for ios9. I recommend urgent change your code. Also through this thought was the server when the scheduled ios changed for swift everything was resolved and my php was almost equal. Care:

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

这篇关于iOS 推送通知无法使用 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆