我在PHP中使用此代码…
function pushnotificationios( $deviceToken, $message, $badges){
$passphrase = "12345";
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $_SERVER['DOCUMENT_ROOT'].'/include/ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client(
"ssl://gateway.push.apple.com:2195", $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
$body['aps'] = array(
//'badge' => $badges,
'badge' => "+1",
'alert' => $message['message'],
'sound' => 'default',
'content-available' => '1'
);
$body['msg'] =$message;
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
//echo "<pre>"; print_r($result);
fclose($fp);
return $result;
}
.pem文件及其密码是否正确.但是当我点击这个功能然后只在生产模式下,它会让我返回false;
$result = pushnotificationios( $deviceToken, $message, $badges);
echo "<pre>"; print_r($result);
这需要太多时间来回应.
目前我找不到任何解决方案..我的api将去苹果,我的通知无效.有趣的是它是一个聊天应用程序,整个应用程序基于通知.对我来说这是糟糕的一天.
如果有什么事情对我有用,请帮忙. 解决方法:
Production:
ssl://gateway.push.apple.com:2195
.
Development:
ssl://gateway.sandBox.push.apple.com:2195
在我的应用程序中我直接得到了NSData中的deviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *deviceTokenString = [NSString stringWithFormat:@"%@", deviceToken];
// this is producing something like:
// <xxxxxxx 9b0f527f xxxxxxxx 2727ed28 xxxxxxxx 4e693a61 xxxxxxx ac2f7dbb>
//
// and i am directly saving that to my servers database
这是我在服务器中的内容.
/***
* Function: template_user_info
*
* Parameter
* ? $message_input
* - String message
* ? $token_array
* - Array contains: `token` ex. "<xxxxxxx 9b0f527f xxxxxxxx 2727ed28 xxxxxxxx 4e693a61 xxxxxxx ac2f7dbb>"
* Note:
* this removes the '<' and '>' to make the device token valid
* `$device_token = str_replace(">","",str_replace("<","",$device_token));`
* ---
*/
public function __push_notification($message_input, $token_array)
{
$push_config = array(
"development" => array(
"status" => true,
"cert" => realpath('xxx.pem'),
"pass" => 'xxxxxx',
"server" => 'ssl://gateway.sandBox.push.apple.com:2195'
),
"production" => array(
"status" => true,
"cert" => realpath('xxx.pem'),
"pass" => 'xxxxxx',
"server" => 'ssl://gateway.push.apple.com:2195'
)
);
$message = stripslashes($message_input);
foreach ($push_config as $key => $value)
{
if ($value['status'] === true)
$this->__exe_push_notification($message, $value, $token_array);
}
}
private function __exe_push_notification($message, $config, $token_array)
{
$cert = $config['cert'];
$pass = $config['pass'];
$server = $config['server'];
$payload = '{
"aps" :
{
"alert" : "'.$message.'",
"badge" : 1,
"sound" : "bingbong.aiff"
}
}';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $cert);
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
$fp = stream_socket_client($server, $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
{
// echo "Failed to connect $err $errstr <br>";
return;
}
else
// echo "Post notification sent<br>";
$dev_array = array();
$dev_array = $token_array;
foreach ($dev_array as $device_token)
{
$device_token = str_replace(">","",str_replace("<","",$device_token));
$msg = chr(0) .
pack("n", 32) .
pack('H*', str_replace(' ', '', $device_token)) .
pack("n", strlen($payload)) .
$payload;
// echo "sending message :" . $payload . "n";
fwrite($fp, $msg);
}
fclose($fp);
}
如果您仍然无法发送通知,请查看此Troubleshooting Push Notifications
编辑
我一直在看你的推送通知推,不久前,我注意到你的’徽章’=> “1” 你无法增加这样的徽章.
唯一的选择是在您的应用程序中管理它并使用数据库保持正常运行.
也许它导致问题..徽章必须是数字,这里:Table 3-1
并检查这discussion也可能对你有所帮助..
编辑02/12/19 我已经更新了推送通知的PHP代码,以支持开发和生产. (编辑:北几岛)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|