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