ios - 推送通知警报来了两次

标签 ios iphone ios5

我做了一个应用程序,因为我已经实现了推送通知,但是当我通过管理面板发送通知时,通知警报在我的设备中出现了两次。我已经创建了单独的配置文件和证书以及 ck.pem 文件。

请帮帮我。

========

function sendnote_ios($message,$registatoin_ids_arr_ios){



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

// Put your alert message here:
$message = $message;

$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'
 );
 //echo '<pre>';print_r($body);
// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
foreach($registatoin_ids_arr_ios as $token){
 $msg = chr(0) . pack('n', 32) . pack('H*', trim($token)) . pack('n', strlen($payload)) . $payload;
 //$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
 $result = fwrite($fp, $msg, strlen($msg));
}

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

最佳答案

让我们试试:

<?php
//your device token
$deviceToken = '';

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

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

关于ios - 推送通知警报来了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23234822/

相关文章:

ios - 如何使用 UITabBarController 为每个选项卡使用相同的 ViewController?

ios - 获取 iPhone 应用程序用户反馈的方法?

ios - 如何自定义 UITabBarController 的标签栏?

ios - 在后台捕获有关 ipad 的信息并发送

c# - 如何使用 MonoTouch 获取 iPhone/iPad 的宽度和高度?

iphone - (iphone) 在没有警报 View 的情况下获取客户的位置?

iphone - 在哪里可以找到 'Available On The App Store' Logo ?

ios - 获取不到的崩溃

iphone - 操作无法完成。 ( cocoa 错误 1560。)

ios - 在 UIAlertController 中更改 UITextField 颜色后出现白色 "outer border"