lizhen_gitee 1 год назад
Родитель
Сommit
a09a2e299a

+ 4 - 8
application/api/controller/Demo.php

@@ -4,6 +4,7 @@ namespace app\api\controller;
 
 use app\common\controller\Api;
 use app\common\library\Transaction;
+use app\common\library\Easemob;
 /**
  * 示例接口
  */
@@ -40,14 +41,9 @@ class Demo extends Api
      */
     public function test()
     {
-        $config = [
-            'appkey' => '1137221110163975#tkenim',
-            'client_id' => 'YXA6DEehGBpFTb-UOcgmWtveZQ',
-            'client_secret' => 'YXA6KhfJfqyJYBn2I20DdgTEzWpx5Cs',
-            'org_name' => '1137221110163975',
-            'app_name' => 'tkenim',
-        ];
-        $url = '';
+
+        $easemob = new Easemob();
+        $easemob->push_text(1009,'标题','内容');
 
         //header
         //Authorization:Bearer ${YourAppToken}

+ 66 - 0
application/common/library/Easemob.php

@@ -0,0 +1,66 @@
+<?php
+namespace app\common\library;
+
+use Easemob\Auth;
+use Easemob\Push;
+
+class Easemob {
+
+    protected $auth;
+
+
+    public function __construct() {
+
+        //初始化配置
+        $easemob_config               = config('easemob');
+        $appKey                       = $easemob_config['appkey'];
+        $clientIdOrAppID              = $easemob_config['client_id'];
+        $clientSecretOrAppCertificate = $easemob_config['client_secret'];
+
+        $this->auth = new Auth($appKey,$clientIdOrAppID,$clientSecretOrAppCertificate);
+    }
+
+    //////////////////消息推送//////////////////////////
+    public function push_text($uid,$title,$msg){
+        $message = new Push($this->auth);
+
+        $pushMessage =
+        [
+            'title'=> $title,
+            'subTitle'=> '',
+            'content'=> $msg,
+            'ext'=> [],
+            'config'=> [
+                'clickAction'=> [
+                    'url'=>'',
+                    'action'=>'',
+                    'activity'=>'',
+                ],
+                'badge'=> [
+                    'addNum'=> 0,
+                    'setNum'=> 0,
+                ],
+            ],
+
+            'easemob'=>[],
+            'apns'=> [],
+            'fcm'=> [],
+            'fcmV1'=>[],
+            'huawei'=> [],
+            'meizu'=> [],
+            'oppo'=> [],
+            'vivo'=> [],
+            'xiaomi'=> [],
+            'honor'=>[]
+        ];
+
+        $rs = $message->push_sync($uid,json_encode($pushMessage));
+        //dump($rs);
+    }
+
+
+
+
+
+
+}

+ 23 - 0
vendor/maniac/easemob-php/src/Push.php

@@ -25,6 +25,29 @@ final class Push
     }
     /// @endcond
 
+    //以同步方式发送推送通知
+    //https://docs-im-beta.easemob.com/push/push_send_notification.html
+    public function push_sync($uid, $pushMessage)
+    {
+
+        $uri = $this->auth->getBaseUri() . '/push/sync/' . $uid;
+
+        $strategy = 0;
+        $body = compact('strategy','pushMessage');
+
+        $header = $this->auth->headers();
+        $header['Content-Type'] =  'application/json';
+
+        $resp = Http::post($uri, $body, $header);
+        dump($resp);
+        if (!$resp->ok()) {
+            return \Easemob\error($resp);
+        }
+        $data = $resp->data();
+        return $data['data'];
+        //return $data['data']['successKeys'];
+    }
+
     /**
      * \~chinese
      * \brief