2

我真的不知道問題在哪裏。 我正在使用庫ApnsPHP發送推送通知。我也嘗試過其他腳本,但那也行不通。爲什麼iPhone不會收到推送通知?

我使用本教程生成推送證書(http://code.google.com/p/apns-php/wiki/CertificateCreation)並將它們放到Apple開發人員網站。 我得到正確的令牌從iphone我把它放進sample_push.php

我有Macbook Pro 13英寸,2010年年中與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. 

如果我執行「PHP sample_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); 

我真的不知道該怎麼辦。請幫助我一個人。

回答

2

我解決了它。這是因爲XCode使用iOS Team Provision Profile簽署了應用程序。在我刪除此配置文件後,推送通知開始工作。

+1

我刪除了所有的配置文件,但仍然沒有迴應。任何想法 ? – 2013-09-06 11:55:30

+0

@SyedOsama,我遇到了同樣的問題。你有沒有找到解決這個問題的辦法? – jacobian 2013-12-02 09:06:26

+0

@Jacobian,是的,我解決了這個問題。 1)檢查你的.pem文件是否有RSA私鑰? – 2013-12-02 12:06:53

0

向蘋果發送推送通知時,會返回狀態碼。你收到的狀態碼是什麼?你回來了嗎?

Status codes 
0 No errors encountered 
1 Processing error 
2 Missing device token 
3 Missing topic 
4 Missing payload 
5 Invalid token size 
6 Invalid topic size 
7 Invalid payload size 
8 Invalid token 
255 None (unknown) 
+0

謝謝你的回答,但PHP腳本使用函數stream_socket_client,它不給我任何答案。你在服務器端使用什麼?我真的在找東西,我也可以調試.... – 2012-04-13 20:47:38

+0

Fri,06 Sep 2013 13:50:26 +0200 ApnsPHP [4632]:信息:嘗試ssl://gateway.push.apple.com:2195。 .. Fri,2013年9月6日13:50:27 +0200 ApnsPHP [4632]:信息:連接到ssl://gateway.push.apple.com:2195。 Fri,2013年9月6日13:50:27 +0200 ApnsPHP [4632]:信息:發送消息隊列,運行#1:留在隊列中的消息。 週五,2013年9月6日13:50:27 +0200 ApnsPHP [4632]:狀態:發送消息ID 1 [自定義標識符:未設置](1/3):55個字節。 Fri,2013年9月6日13:50:28 +0200 ApnsPHP [4632]:信息:斷開連接。我收到了這條消息,而且我有同樣的問題。 – 2013-09-06 11:52:49