在 php yii2.0 中集成 android firebase 通知

Integrating android firebase notification in php yii2.0

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

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 和注册令牌,只需要知道如何执行它。

是的,我做到了!!这是任何尝试此操作的人的方法。

假设您有一个后端,您要在其中提交推送通知的 "title" 和 "body"。 现在,当我提交表单时,它会执行读取提交数据的操作。像这样。

use backend\helpers\FirebaseNotifications; 

定义在top

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->sendNotification($tokens, $message);

它在 helpers class 中定义在一个单独的文件中。在 helpers 文件夹下,例如 FirebaseNotifications.php。它的内容是这样的。

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

    }