在发送警予推送通知 [英] Sending a push notification in yii

查看:131
本文介绍了在发送警予推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过让设备令牌推送通知发送到iOS设备,到目前为止,我已经在Yii中做到了这一点:

I want to send a push notification to an ios device by getting the device token, so far i have done this in yii:

public function actionPushtest(){

    $token=$_REQUEST['token'];

        $message = 'Hello';
        $badge = 1;
        $sound = 'default';
        $development = true;
        $passphrase='pass';

        $payload = array();
        $payload['aps'] = array('alert' => $message, 'badge' => intval($badge), 'sound' => $sound);
        $payload = json_encode($payload);

        $apns_url = NULL;
        $apns_cert = NULL;
        $apns_port = 2195;

        if($development)
        {
            $apns_url = 'gateway.sandbox.push.apple.com';
            $apns_cert = dirname(Yii::app()->request->scriptFile).'/file.pem';
        }
        else
        {
            $apns_url = 'gateway.push.apple.com';
            $apns_cert = dirname(Yii::app()->request->scriptFile).'/file.pem';
        }
        $stream_context = stream_context_create();
        stream_context_set_option($stream_context, 'ssl', 'local_cert', $apns_cert);
        stream_context_set_option($stream_context, 'ssl', 'passphrase', $passphrase);

        $apns = stream_socket_client('ssl://' . $apns_url . ':' . $apns_port, $error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context);



        $device_tokens=  str_replace("<","",$token);
        $device_tokens1=  str_replace(">","",$device_tokens);
        $device_tokens2= str_replace(' ', '', $device_tokens1);


            $apns_message = chr(0) . chr(0) . chr(32) . pack('H*', $device_tokens2 /*str_replace(' ', '', $device_tokens1)*/) . chr(0) . chr(strlen($payload)) . $payload;

            $msg=fwrite($apns, $apns_message);
            if (!$msg){
                echo 'Message not delivered' . PHP_EOL;
            }else{
                echo 'Message successfully delivered' . PHP_EOL;
            }


        @socket_close($apns);
        @fclose($apns);
}

..我没有得到任何错误,但没有被收到通知。我究竟做错了什么?

..i am not getting any errors but the notification is not being received. what am i doing wrong?

推荐答案

我发现了我的错误终于,这个问题是与 PEM文件,虽然没有误差显示此,也不会产生推送通知而应用程序时,您需要最小化或东西。对于其他人谁遇到这个问题,请确保您的 PEM文件 100%确定。并感谢其他人谁幸免一段时间,帮我解决这个问题。
这里是code:

I discovered what my error was finally, the issue was with the pem file, though no error was being displayed for this and also no push notification is generated while the app is on, you need to minimize it or something. For others who run into this problem, make sure that your pem file is 100% OK. And thanks to others who spared some time and helped me solve it. Here is the code:

public function actionPushtest(){

    $token=$_REQUEST['token'];

        $message = 'Hello';
        $badge = 1;
        $sound = 'default';
        $development = true;//make it false if it is not in development mode
        $passphrase='pass';//your passphrase

        $payload = array();
        $payload['aps'] = array('alert' => $message, 'badge' => intval($badge), 'sound' => $sound);
        $payload = json_encode($payload);

        $apns_url = NULL;
        $apns_cert = NULL;
        $apns_port = 2195;

        if($development)
        {
            $apns_url = 'gateway.sandbox.push.apple.com';
            $apns_cert = dirname(Yii::app()->request->scriptFile).'/file.pem';
        }
        else
        {
            $apns_url = 'gateway.push.apple.com';
            $apns_cert = dirname(Yii::app()->request->scriptFile).'/file.pem';
        }
        $stream_context = stream_context_create();
        stream_context_set_option($stream_context, 'ssl', 'local_cert', $apns_cert);
        stream_context_set_option($stream_context, 'ssl', 'passphrase', $passphrase);

        $apns = stream_socket_client('ssl://' . $apns_url . ':' . $apns_port, $error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context);



        $device_tokens=  str_replace("<","",$token);
        $device_tokens1=  str_replace(">","",$device_tokens);
        $device_tokens2= str_replace(' ', '', $device_tokens1);


            $apns_message = chr(0) . chr(0) . chr(32) . pack('H*', $device_tokens2) . chr(0) . chr(strlen($payload)) . $payload;

            $msg=fwrite($apns, $apns_message);
            if (!$msg){
                echo 'Message not delivered' . PHP_EOL;
            }else{
                echo 'Message successfully delivered' . PHP_EOL;
            }


        @socket_close($apns);
        @fclose($apns);
}

这篇关于在发送警予推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆