使用PHP的iOS推送通知不工作 [英] iOS push notification not working using PHP

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

问题描述

我试图用APNS库来实现推送通知iOS中的PHP。这里是我的code:

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若有停止使用code目标C和迅速的使用,因为它在产生objectiv令牌错误 - 下ios9。
我建议紧急变更您的code。
同时通过这个想法是服务器时改变一切迅速在预定的ios被解决,我的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;     

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

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