首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在php yii2.0中集成android防火墙通知

在php yii2.0中集成android防火墙通知
EN

Stack Overflow用户
提问于 2018-01-09 07:03:25
回答 1查看 2.1K关注 0票数 0

我知道这是一个非常愚蠢的问题,我正在yii2后端集成android通知。我了解了许多yii2扩展,但这是行不通的,我发现这个很简单,所以尝试使用这个。但是我不知道如何使用它,我必须为此发送HTTP请求。这是密码。

代码语言:javascript
复制
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=Your_Authorization_Key
 {
   "registration_ids": ["registration_token"],
   "data": {
    "message": "This is a Firebase Message!",
 }
}

我有auth_key和注册令牌,只需要知道如何执行。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-18 05:17:03

是的我做到了!这是任何人尝试这个的方法。

假设您有一个后端,在其中提交推送通知的“标题”和“主体”。现在,当我提交我的表格,它有一个动作,我读取提交的数据。就像这样。

代码语言:javascript
复制
use backend\helpers\FirebaseNotifications; 

在顶部定义

代码语言:javascript
复制
if ($model->load(Yii::$app->request->post())) {
        $model->save(false);
        $title = $model->title;
        $body = $model->content;
        $service = new FirebaseNotifications(['authKey' => 
      'YOUR_AUTH_KEY']);

        $all_users = User::find()->where(['!=','device_id','Null'])->andwhere(['!=','device_id',' '])->all();
        $tokens = [];    
            foreach ($all_users as $users) { 
                 $tokens[] = $users['device_id'];
            } 
        $message = array('title' => $title, 'body' => $body);
        $service->sendNotification($tokens, $message);

        return $this->redirect(['index']);
 }

我在打电话

$service->发送通知($tokens,$message);

它是在另一个文件中的helpers类中定义的。在像FirebaseNotifications.php这样的helpers文件夹下。它的内容是这样的。

代码语言:javascript
复制
<?php
    namespace backend\helpers;

    use yii\base\Object;
    use Yii;
    use yii\helpers\ArrayHelper;

    class FirebaseNotifications extends Object
    { 
        public $authKey;
        public $timeout = 50;
        public $sslVerifyHost = false;
        public $sslVerifyPeer = false;
        public $apiUrl = 'https://fcm.googleapis.com/fcm/send';

        public function init()
        {
            if (!$this->authKey) throw new \Exception("Empty authKey");
        }

        public function send($body)
        {
            $headers = [
                "Authorization:key={$this->authKey}",
                'Content-Type: application/json',
                'Expect: ',
            ];
            $ch = curl_init($this->apiUrl);
            curl_setopt_array($ch, [
                CURLOPT_POST           => true,
                CURLOPT_SSL_VERIFYHOST => $this->sslVerifyHost,
                CURLOPT_SSL_VERIFYPEER => $this->sslVerifyPeer,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_BINARYTRANSFER => true,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_HEADER         => false,
                CURLOPT_FRESH_CONNECT  => false,
                CURLOPT_FORBID_REUSE   => false,
                CURLOPT_HTTPHEADER     => $headers,
                CURLOPT_TIMEOUT        => $this->timeout,
                CURLOPT_POSTFIELDS     => json_encode($body),
            ]);
            $result = curl_exec($ch);

            if ($result === false) {
                Yii::error('Curl failed: '.curl_error($ch).", with result=$result");
                throw new \Exception("Could not send notification..");
            }
            $code = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if ($code<200 || $code>=300) {
                Yii::error("got unexpected response code $code with result=$result");
                throw new \Exception("Could not send notification");
            }
            curl_close($ch);
            $result = json_decode($result , true);
            return $result;
        }

        public function sendNotification($tokens = [], $notification, $options = [])
        {   
            $body = array(
                'registration_ids' => $tokens,
                'notification' => $notification,
                    //array('title' => 'Time of Sports', 'body' => 'Salman Notification'),
                //'data' => array('message' => $notification)
            );
            $body = ArrayHelper::merge($body, $options);
            return $this->send($body);
        }

    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48162901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档