面对ios推送通知php代码中的问题 [英] Facing problems in ios push notification php code
问题描述
我在php中使用此代码...
I use this code in 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;
.pem file and their password is correct. but when i hit this function then only on production mode it give me return false;
$result = pushnotificationios( $deviceToken, $message, $badges);
echo "<pre>"; print_r($result);
回复需要太多时间。
目前我找不到任何解决方案..我的api会去苹果,我的通知无效。有趣的是它是一个聊天应用程序,整个应用程序基于通知。对我来说这是糟糕的一天。
Currently i am not find any solution.. my api is going to apple and my notifications is not working. The interesting thing is its a chating app and whole app is based on notifications. Its a bad day for me.
如果有什么事情对我有用,请帮忙。
Please help if something happen good for me for app.
推荐答案
生产:
ssl://gateway.push.apple.com :2195
ssl://gateway.push.apple.com:2195
。
开发:
ssl://gateway.sandbox.push.apple.com:2195
ssl://gateway.sandbox.push.apple.com:2195
在我的应用中。我直接得到 deviceToken
在 NSData
In my app. I am directly getting deviceToken
that's in NSData
- (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
这是我服务器中的内容。
Here's what i have in my server.
$from_database = "<xxxxxxx 9b0f527f xxxxxxxx 2727ed28 xxxxxxxx 4e693a61 xxxxxxx ac2f7dbb>";
// this removes the '<' and '>' to make the device token valid
//
$token_string = substr($from_database, 1, -1);
$token_array[] = $token_string;
// you can also add multiple 'token_string' to the 'token_array' for push notification broadcasting, i am currently doing that and it is working properly.
...
public function __push_notification($message_input, $token_array)
{
$message = stripslashes($message_input);
$passphrase = 'xxxxxx';
$cert = realpath('xxx.pem');
$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', $passphrase);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $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)
{
$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);
}
如果您仍然无法发送通知,请检查此排查推送通知
If you still cant send notification check this Troubleshooting Push Notifications
编辑
我一直在看你的推送通知推送,不久前,我注意到你的'徽章'=> +1
你不能增加这样的徽章。
I've been looking at your push notification push, a while ago, then i've noticed that your 'badge' => "+1"
You cannot increment badge like that.
唯一的选择是在您的应用中管理它并使用数据库保持正常运行..
The only option is to manage it in your app and using database to keep on track..
和 可能 导致问题.. 徽章
必须是数字,此处:表3 -1
and maybe it is causing the problem.. and badge
must be a Number, here: Table 3-1
还要检查讨论也可能对您有所帮助..
And also check this discussion might help you as well..
这篇关于面对ios推送通知php代码中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!