面对ios推送通知php代码中的问题

时间:2015-05-27 07:13:59

标签: 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');
        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会去苹果,我的通知无效。有趣的是它是一个聊天应用程序,整个应用程序基于通知。对我来说这是糟糕的一天。

如果app对我有用,请帮忙。

3 个答案:

答案 0 :(得分:5)

在生产模式下,请勿使用沙盒网址

 $fp = stream_socket_client(
        "ssl://gateway.push.apple.com:2195", $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 

答案 1 :(得分:2)

  

<强>生产:

     

ssl://gateway.push.apple.com:2195

  

发展:

     

ssl://gateway.sandbox.push.apple.com:2195

在我的应用中。我直接在deviceToken

中获得了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

这是我服务器中的内容。

/***
* 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

修改

前一段时间我一直在看你的推送通知推送,然后我注意到你的'badge' => "+1" 你无法增加这样的徽章。

唯一的选择是在您的应用中管理它并使用数据库保持正常运行..

可能 导致问题.. badge必须是数字,在此处:Table 3-1

并检查此discussion可能对您有所帮助..

编辑02/12/19 我已经更新了推送通知的PHP代码,以支持开发和生产。

答案 2 :(得分:0)

是的..最终我得到了解决方案。

问题是端口。我使用Bluehost服务器。 Bluehost阻止推送通知使用的端口。现在我改变了我的服务器和通知为我工作.. 感谢所有budy