iphone - 为什么 iPhone 收不到推送通知?

标签 iphone ios push-notification apple-push-notifications apns-php

我真的不知道问题出在哪里。 我正在使用库 ApnsPHP 发送推送通知。我也尝试过其他脚本,但也不起作用。

我使用本教程(http://code.google.com/p/apns-php/wiki/CertificateCreation)生成推送证书,并将它们放到苹果开发者网站上。 我从 iPhone 获得了正确的 token ,并将其放入sample_push.php

我有 2010 年中生产的 13 英寸 Macbook Pro,配备 Mac OS Lion。

vojta:~/dev/www/application$ php sample_push.php 
Fri, 13 Apr 2012 16:23:24 +0200 ApnsPHP[6478]: INFO: Trying ssl://gateway.sandbox.push.apple.com:2195...
Fri, 13 Apr 2012 16:23:32 +0200 ApnsPHP[6478]: INFO: Connected to ssl://gateway.sandbox.push.apple.com:2195.
Fri, 13 Apr 2012 16:23:32 +0200 ApnsPHP[6478]: INFO: Sending messages queue, run #1: 1 message(s) left in queue.
Fri, 13 Apr 2012 16:23:32 +0200 ApnsPHP[6478]: STATUS: Sending message ID 1 [custom identifier: Message-Badge-3] (1/3): 109 bytes.
Fri, 13 Apr 2012 16:23:33 +0200 ApnsPHP[6478]: INFO: Disconnected.

如果我执行“phpsample_push.php”,我不会收到任何错误,但我的 iPhone 上也没有收到任何推送通知。

来源sample_push.php:

// Using Autoload all classes are loaded on-demand
require_once 'ApnsPHP/Autoload.php';

// Instanciate a new ApnsPHP_Push object
$push = new ApnsPHP_Push(
    ApnsPHP_Abstract::ENVIRONMENT_SANDBOX,
    'server_certificates_bundle_sandbox.pem'
);

// Set the Root Certificate Autority to verify the Apple remote peer
$push->setRootCertificationAuthority('entrust_root_certification_authority.pem');

// Connect to the Apple Push Notification Service
$push->connect();

// Instantiate a new Message with a single recipient

$message = new ApnsPHP_Message('xxxx'); // i put my token here

// Set a custom identifier. To get back this identifier use the getCustomIdentifier() method
// over a ApnsPHP_Message object retrieved with the getErrors() message.
$message->setCustomIdentifier("Message-Badge-3");

// Set badge icon to "3"
//$message->setBadge(3);

// Set a simple welcome text
$message->setText('Hello APNs-enabled device!');

// Play the default sound
$message->setSound();

// Set the expiry value to 30 seconds
$message->setExpiry(30);

// Add the message to the message queue
$push->add($message);

// Send all messages in the message queue
$push->send();

// Disconnect from the Apple Push Notification Service
$push->disconnect();

// Examine the error message container
$aErrorQueue = $push->getErrors();
if (!empty($aErrorQueue)) {
    var_dump($aErrorQueue);
}

我还尝试了更简单的脚本(也不起作用)

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

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

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

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

我真的不知道该怎么办。请帮助我。

最佳答案

我解决了。 这是因为 XCode 使用 iOS Team Provisioning Profile 签署了应用程序。删除此配置文件后,推送通知开始起作用。

关于iphone - 为什么 iPhone 收不到推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10143527/

相关文章:

ios - CoreML 模型 : Convert imageType model input to multiArray

ios - 从 HealthKit (HKStatistics) 获取最新(最近)的 Step 值

iphone - 在 iPad 上打开 HTML 页面上指向 PDF 特定页面的链接

iphone - "Operation couldn' t 完成。连接由对等方重置。”在 iOS 上使用 NSURLConnection

ios - 名称更改后项目将无法运行

android - 如何使 GCM 与应用程序和服务器保持同步?

ios - 使用APNS发送远程推送通知的送达报告-IOS

css - 如何调整背景图像以适应 iPad 屏幕

iphone - Three20 构建错误

ios - 在后台接收推送通知