在ios推送通知php代码中面临问题


Facing problems in ios push notification php code

我在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将转到苹果,我的通知不起作用。有趣的是,它是一个聊天应用程序,整个应用程序都是基于通知的。今天对我来说很糟糕。

如果应用程序发生了对我有利的事情,请帮忙。

在生产模式中不要使用沙箱url

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

生产:

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

开发:

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

如果您仍然无法发送通知,请检查此故障排除推送通知

编辑

我刚才一直在看你的推送通知推送,然后我注意到你的'badge' => "+1"你不能那样增加徽章。

唯一的选择是在你的应用程序中管理它,并使用数据库保持跟踪。。

可能是导致了问题。。和badge必须是一个数字,这里:表3-1

同时也检查一下这个讨论可能对你也有帮助。。

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

是的。。最后我得到了答案。

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