9

我正在使用this tutorial要學習推送通知。推送通知不在iphone上接收

<?php 

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

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

// 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)); 
echo 'result =' . $result. PHP_EOL; 
if (!$result) 
echo 'Message not delivered' . PHP_EOL; 
else 
echo 'Message successfully delivered' . PHP_EOL; 

// Close the connection to the server 
fclose($fp); 

我也配置推送通知的應用程序。配置推後,我也重新創建配置文件,舊的刪除一,安裝新的配置文件。 我運行應用程序,它給了我設備ID,然後我連接服務器沙箱和生產發送推送通知與他們的相對推送配置文件,但仍然無法接收我的設備上的推送通知。

我也在我的設備上安裝ipusher並檢查推送通知。他們來自該應用程序。我注意到

一個奇怪的是,我改變我的應用程序標識和使用任何其他應用程序ID,然後在設備令牌保持相同

現在我的問題是我沒有收到我的設備上的推送通知。


問題不在我的配置文件中。可能是錯誤是我使用的php代碼,因爲當我在遠程服務器上使用easy apns時,它會發送推送通知。 收到的通知時間爲6至7小時。我認爲這是由於我的設備端的網絡問題。 但現在它在生產配置文件2天后正常工作。現在通知沒有時間交付我的設備,但在某些設備上需要30秒到5分鐘。


可以有更多的一個問題,如果你沒有你的設備從其他應用程序在收到推送通知過,那麼你應該檢查你的DNS進行連接。

+2

這是服務器端實現部分iphone應用程序部分。你可以查看這個教程http://mobiforge.com/developing/story/programming-apple-push-notification-services http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part- 12 – iamsult

+0

我遵循raywenderlich爲iphone實施。獲取設備標記 –

+0

問題不在我的配置文件中。可能是錯誤是我使用的php代碼,因爲當我在遠程服務器上使用easy apns時,它會發送推送通知。收到的通知時間爲6至7小時。我認爲這是由於我的設備端的網絡問題。但現在它在生產配置文件的2天后工作正常。現在通知沒有時間交付我的設備,但在某些設備上需要30秒到5分鐘。 –

回答

2

好吧,我終於得到了我的問題。問題不在代碼中,實際上問題在於我的iphone中設置了錯誤的DNS值。 Iphone自動將我的路由器的IP地址放在DNS字段中。現在我給我的服務提供商的DNS值,然後它工作正常。現在我收到推送消息只要我送他們。

我希望它能幫助別人。

1

檢查您的推送通知證書。證書是否與任何私鑰關聯?

如果沒有,請使用從您的鑰匙鏈生成的相應私鑰重新生成推送通知證書。

請看看下面的教程:

Apple Push Notification Tutorial:

感謝,

MinuMaster

2

實現對服務器端的反饋服務,也可以是內有多少檢查服務器端持續時間將所有設備令牌發送給APNS。 從反饋服務atleast你會知道有多少設備收到你的通知。 如果所有的設備令牌都是一個接一個發送給APN,並且APN不會通過反饋服務發送任何列表,那麼您無法處理在設備上接收通知的持續時間。

1

使用UrbanAirShip。在我看來,它是最好的服務器端解決方案,因爲它包含Android(C2DM)和Blakberry的類似推送通知。

嘗試找到這些文件之間的差異並理解它們。可能是解決您的問題。這裏是我的代碼:

<?php 

$message = 'Hello'; // $_GET or $_POST 
$badge = 3; // int 
$sound = 'default'; // string - sound name 
$development = true; // boolean 

$payload = array(); 
$payload['aps'] = array('alert' => $message, 'badge' => intval($badge), 'sound' => $sound); 
$payload = json_encode($payload); 

$apns_url = NULL; // Set Later 
$apns_cert = NULL; // Set Later 
$apns_port = 2195; 

if($development) 
{ 
    $apns_url = 'gateway.sandbox.push.apple.com'; 
    $apns_cert = '/path/apns.pem'; // relative address to an App Specific Certificate  file 
} 
else 
{ 
    $apns_url = 'gateway.push.apple.com'; 
    $apns_cert = '/path/cert-prod.pem'; 
} 

$stream_context = stream_context_create(); 
stream_context_set_option($stream_context, 'ssl','local_cert',$apns_cert); 

$apns = stream_socket_client('ssl://'.$apns_url.':'.$apns_port,$error,$error_string,2,STREAM_CLIENT _CONNECT,$stream_context); 

// You will need to put your device tokens into the $device_tokens array yourself 
$device_tokens = array(); // tokens!!! 

foreach($device_tokens as $device_token) 
{ 
    $apns_message = chr(0).chr(0).chr(32).pack('H*',str_replace(' ','',$device_token)).chr(0).chr(strlen($payload)).$payload; 
    fwrite($apns, $apns_message); 
} 

@socket_close($apns); 
@fclose($apns); 
?> 
8

首先確保您使用的是:

  • 的應用與調試/發佈規定編制
  • 您的鑰匙串有商發展/生產推送通知證書

然後使用下面的代碼(已經測試了兩個dev &生產)

<?php 
// Comment these lines in production mode 
ini_set('display_errors','on'); 
error_reporting(E_ALL); 


// Apns config 

// true - use apns in production mode 
// false - use apns in dev mode 
define("PRODUCTION_MODE",false); 

$serverId = 1; 
$serverName = 'my-server-domain.com'; 

if(PRODUCTION_MODE) { 
$apnsHost = 'gateway.sandbox.push.apple.com'; 
} else { 
$apnsHost = 'gateway.push.apple.com'; 
} 

$apnsPort = 2195; 
if(PRODUCTION_MODE) { 
// Use a development push certificate 
$apnsCert = $_SERVER['DOCUMENT_ROOT'].'/apns/apns-dominos-development.pem'; 
} else { 
// Use a production push certificate 
$apnsCert = $_SERVER['DOCUMENT_ROOT'].'/apns/apns-dominos-production.pem'; 
} 


// --- Sending push notification --- 

// Insert your device token here 
$device_token = "<dc6127d8 dc6127d8 dc6127d8 dc6127d8 dc6127d8 dc6127d8 dc6127d8 dc6127d8>"; // Some Device Token 


// Notification content 

$payload = array(); 

//Basic message 
$payload['aps'] = array(
'alert' => 'testing 1,2,3..', 
'badge' => 1, 
'sound' => 'default', 
); 
$payload['server'] = array(
'serverId' => $serverId, 
'name' => $serverName 
); 
// Add some custom data to notification 
$payload['data'] = array(
'foo' => "bar" 
); 
$payload = json_encode($payload); 

$streamContext = stream_context_create(); 
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert); 
stream_context_set_option($streamContext, 'ssl', 'passphrase', ""); 


$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error,  $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext); 


$deviceToken = str_replace(" ","",substr($device_token,1,-1)); 
echo $deviceToken; 
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '',  $deviceToken)) . chr(0) . chr(mb_strlen($payload)) . $payload; 
fwrite($apns, $apnsMessage); 


//socket_close($apns); 
fclose($apns); 

?> 
+0

你在做什麼?爲什麼? $ deviceToken = str_replace(「」,「」,substr($ device_token,1,-1)); – RamshaS

+0

刪除空格和「<」前綴和「>」後綴。 設備標記是字母數字,不包含空格或其他字符 – Tamir