iPhone 上收不到推送通知

发布于 2024-12-15 10:27:15 字数 2111 浏览 8 评论 0原文

我正在使用本教程< /strong> 学习推送通知。

<?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 是否有连接。

I am using this tutorial to learn push notification.

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

I also configure app for push notification. After configuring for push i also recreate provisioning profiles, old delete one, install new profile.
I run app it gives me device id and then i connect both server sandbox and production to send push notification with their relative push profiles but still i am not able to receive push notification on my device.

I also install ipusher on my device and check push notification. they are coming from that application.

One strange thing i notice is that i change my application identifier and use any other app id then device token remain same

Now my problem is I am not receiving the push notification on my device.


The problem is not in my profiles. May be the error is php code that i am using because when i use the easy apns on remote server then it sends push notifications.
The notifications received time was 6 to 7 hours. I think this is due to network problem on my device side.
But now it is working fine after 2 days on production profile. Now notification take no time for delivering on my device but it is taking 30 sec to 5 minutes on some devices.


There can be one more problem if you are not receiving push notifications on your device from other apps too, then you should check your DNS for the connection.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忆梦 2024-12-22 10:27:15

首先确保您正在使用:

  • 应用程序是使用调试/发布配置进行编译的,
  • 您的钥匙串具有开发/生产推送通知证书,

然后使用以下代码(已测试开发和生产)

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

?>

First make sure that you're using:

  • The application is compiled with debug/release provision
  • your keychain has the devlopment/production push notification certificate

then use the following code (been tested both dev & production)

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

?>
仲春光 2024-12-22 10:27:15

在服务器端实现反馈服务,并在服务器端检查所有设备令牌在多长时间内发送到 APNS。
从反馈服务中,您至少会知道有多少设备收到了您的通知。
如果所有设备令牌都一一发送到 APN,并且 APN 不通过反馈服务发送任何列表,则您无法处理在设备上接收通知的持续时间。

Implement the feedback service on server side and also check on server side that within how much duration all the device token are send to APNS.
From feedback service atleast u will come to know that how much devices had received ur notification.
If all the device token are send one by one to the APNs and the APNs does'nt send any list through feedback service then u cannot handle the duration to receive notification on devices.

神魇的王 2024-12-22 10:27:15

好吧,我终于解决了我的问题。问题不在代码中,实际上问题出在我的 iPhone 中设置的错误 DNS 值。 Iphone 会自动将路由器的 IP 地址放入 DNS 字段。现在我给出了我的服务提供商的 DNS 值,然后它就可以正常工作了。现在我一发送推送消息就收到了。

我希望它对其他人有帮助。

Okay i finally got my Problem. The problem is not in code actually the problem is in Wrong DNS Value set in my iphone. Iphone automatically place ip of my router for DNS field. Now i give the DNS value of my service provider then it works fine. Now I am receiving Push messages As soon as i sent them.

I Hope it helps others.

吾性傲以野 2024-12-22 10:27:15

检查您的推送通知证书。证书是否与任何私钥相关联?

如果否,请使用从您的钥匙链生成的适当私钥重新创建推送通知证书。

请查看以下教程:

Apple 推送通知教程:

谢谢,

米努大师

Check your push notification certificate. is certificate was associate with any private key?

If no then please recreate push notification certificate with appropriate private key which are generated from your key chain.

Please take a look at the below Tutorial:

Apple Push Notification Tutorial:

thanks,

MinuMaster

节枝 2024-12-22 10:27:15

使用城市飞艇。在我看来,它是最好的服务器端解决方案,因为它还包括适用于 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);
?>

Use UrbanAirShip. In my opinion it's the best server side solution since it includes Push-alike notifications for Android (C2DM) and Blakberry too.

Try finding differences between these to files and understand them. Might be a solution to your problem. Here's my code:

<?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);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文