ios推送通知php代码中面临的问题
我在php中使用此代码ios推送通知php代码中面临的问题,php,ios,Php,Ios,我在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
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;
}
函数pushnotificationios($deviceToken、$message、$badges){
$passphrase=“12345”;
$ctx=stream_context_create();
stream_context_set_选项($ctx,'ssl','local_cert',$_SERVER['DOCUMENT_ROOT']./include/ck.pem');
stream_context_set_选项($ctx,'ssl','passphrase',$passphrase);
$fp=流\u套接字\u客户端(
"ssl://gateway.push.apple.com:2195“,$err,
$errstr,60,流式_客户端_连接|流式_客户端_持久,$ctx);
$body['aps']=数组(
//“徽章”=>美元徽章,
“徽章”=>“+1”,
'警报'=>$message['message'],
“声音”=>“默认值”,
“内容可用”=>“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));
//回显“;打印($result);
fclose($fp);
返回$result;
}
.pem文件及其密码正确。但当我点击这个函数时,它只在生产模式下返回false
$fp = stream_socket_client(
"ssl://gateway.push.apple.com:2195", $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
$result=pushnotificationios($deviceToken、$message、$badges);
回声“;打印(结果);
而且要花太多时间才能做出反应
目前我还没有找到任何解决办法。。我的api将转到苹果,我的通知不起作用。有趣的是,它是一个聊天应用程序,整个应用程序都是基于通知的。这对我来说是糟糕的一天
如果应用程序发生了对我有利的事情,请提供帮助。在生产模式下,不要使用沙盒url
/***
* 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);
}
制作:
ssl://gateway.push.apple.com:2195
发展:
ssl://gateway.sandbox.push.apple.com:2195
在我的应用程序中。我正在直接获取NSData
中的deviceToken
-(void)应用程序:(UIApplication*)应用程序DIDregisterforRemotionTificationswithDeviceToken:(NSData*)deviceToken
{
NSString*deviceTokenString=[NSString stringWithFormat:@“%@”,deviceToken];
//这产生了类似于:
//
//
//我直接将其保存到我的服务器数据库中
这是我服务器中的内容
/***
*功能:模板\用户\信息
*
*参数
*•$message_输入
*-字符串消息
*•$token_数组
*-数组包含:`token`ex.“”
*注:
*这将删除“”以使设备令牌有效
*`$device\u-token=str\u-replace(“>”,“”),str\u-replace(“,”,str\u-replace(“是的..最后我得到了解决方案
问题是端口。我正在使用Bluehost server。Bluehost阻止推送通知使用的端口。现在我更改了我的服务器和为我工作的通知。。
感谢所有budy当我实现此功能并在生产现场时,通知是有效的,但在得到苹果公司的批准后,通知突然失效,,,我不知道怎么做?然后检查pem文件是否是从ios sideno ios团队的开发或分发证书中提取的。”分发证明不是从开发中提取的"他们是不同的。这项工作是在开发模式下进行的,而不是在生产模式下进行的。。ssl://gateway.sandbox.push.apple.com:2195 我付钱给你,它可以工作,但已经在开发模式下使用了,但在生产模式下没有。你试过使用上面的代码吗?不用担心付钱,我只是在开玩笑。。哈哈哈。。如果这不起作用,它就是奇怪,因为我们目前正在使用此..哈哈哈是-----------------------------------------------嗯,不过最后一条评论,可能是防火墙阻止访问端口
。检查此苹果[推送通知疑难解答]()…抱歉没有帮助,您(世界各地的专业人士都在使用此网站;您可能希望整理您的答案以适合目标受众。注意找到了任何解决方案:-(嗨,Suresh,1)问题可能出在pem文件创建中,请检查您是否使用了适用于产品应用程序的正确证书。2)有时,到达后可能无法从Apple服务器发送推送通知,通知的生存时间仅为30分钟。如果未在该时间内发送,则不会再次发送消息通知。添加此代码并检查与APNS服务器的连接是否正常。如果(!$fp){exit(“连接失败:$err$errstr.PHP_EOL);}
您正在使用的托管服务器??首先检查POST 2195是否已打开,如果未打开,则要求hosting Provider打开端口尝试从服务器发送一个强制通知。有时消息或数据包会卡在服务器上。因此,它确实是端口…很高兴您这样做了,巴德,祝贺您:)为此,我在两台或三台不同的服务器上检查相同的功能。IOS PushNotification在共享服务器上不工作。我也面临相同的问题…您能否提供详细信息,说明如何允许BLUEHOST服务器上的某些端口…??嗨,我们正在使用ssl://gateway.sandbox.push.apple.com:2195 ios推送通知和某些共享服务器的url阻止端口2195所以对于apple推送通知,我们从不使用共享服务器。您使用哪个端口?
- (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);
}