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

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

问题描述

我想通过获取设备令牌向 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% 正常.并感谢其他抽出时间帮助我解决问题的人.代码如下:

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

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

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