google fcm 推送的流程

總結:
1.給一我的推,能成功,
2.給多我的推,有兩種,一種是給組推,一種是給主題推,以前用的是組推,可是不成功,這裏換成主題推;

<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use paragraph1\phpFCM\Client;
use paragraph1\phpFCM\Message;
use paragraph1\phpFCM\Recipient\Device;
use paragraph1\phpFCM\Notification;

class FcmController extends Controller{

    private $apikey;
    public function __construct()
    {
        $this->apikey='本身應用的apikey';

    }

    //用Fcm推送消息
    public function push($content='',$fcmtoken=''){
        $apiKey = $this->apikey;
        $client = new Client();
        $client->setApiKey($apiKey);
        $client->injectHttpClient(new \GuzzleHttp\Client());

//        $note = new Notification('test title', '如今時間是 '.date('Y-m-d H:i:s'));
        $note = new Notification('', $content);
        $note->setIcon('smile')
            ->setColor('#ffffff')
            ->setBadge(1);

        $message = new Message();
        $message->addRecipient(new Device($fcmtoken));
        $message->setNotification($note)
            ->setData(array('someId' =>11112));
        $message->setNotification($note);

        $response = $client->send($message);
        var_dump($response->getStatusCode());
    }

    /**
     * 這裏沒用了
     * @param array $requestData
     * @return bool
     */
    public function old_insertIntoGroup($requestData=[]){
        if(!$requestData['tuid'] || !$requestData['version'] || !$requestData['fcmtoken'] ){
            return false;
        }

        //建立和添加設備組的 url
        $url = 'https://android.googleapis.com/gcm/notification';

        $header = [
            'Content-Type:application/json',
            'Authorization:key='.$this->apikey,
            'project_id:本身項目id',

        ];
        $groupkey='';
        //先查找表中最近新的設備組的key,若key裏面的子帳號已經達到必定數目,就新建一個組
        $gdata=DB::select('select count(id) num,groupkey from zeai_andro_push group by groupkey order by id desc limit 1;');
//        $gdata=DB::select('select groupkey from zeai_andro_push order by id desc limit 1;');

        //確保能有推送fcmtoken
        if( !DB::table('zeai_andro_push')->where(['tuid'=>$requestData['tuid']])->count(['id'])) {
            $requestData['groupkey'] = $gdata[0]->groupkey;
            DB::table('zeai_andro_push')->insertGetId($requestData);
        }
        $groupkey=$gdata[0]->groupkey;
        $data = [
            "operation"=>"add",
            "notification_key_name"=> "appUser-Chris",//設備組名
            "notification_key"=>$groupkey,
            "registration_ids"=>[$requestData['fcmtoken']]
        ];

        $res =  json_decode( $this->http($url,$data,'post',$header) ,true);
        if (isset($res['notification_key'])){
            $res=DB::table('zeai_andro_push')
                ->where(['tuid'=>$requestData['tuid']])
                ->update([
                    'groupkey'=>$res['notification_key']
                ]);
            return  $res ? true : false;
        }else{
            //一個設備組是否滿員未能測試 若是運行到這裏 能夠根據返回的提示
            return false;
        }

    }

    /**
     * 添加用戶的主題,保存用戶的fcmtoken
     * @param array $requestData
     * @return bool
     */
    public function insertIntoGroup($requestData=[]){
        if(!$requestData['tuid'] || !$requestData['version'] || !$requestData['fcmtoken'] ){
            return false;
        }
        $this->oldmyTopic($requestData['fcmtoken']);
        //先查找表中最近新的設備組的key,若key裏面的子帳號已經達到必定數目,就新建一個組

        //確保能有推送fcmtoken
        if( !DB::table('zeai_andro_push')->where(['tuid'=>$requestData['tuid']])->count(['id'])) {
            $requestData['groupkey'] = 0;
            DB::table('zeai_andro_push')->insertGetId($requestData);
        }

    }
    //建立設備組
    public function createGroup($fcmtoken=''){
        //建立和添加設備組的 url
        $url = 'https://android.googleapis.com/gcm/notification';
        $header = [
            'Content-Type:application/json',
            'Authorization:key='.$this->apikey,
            'project_id:196236249110',
        ];
        $data = [
            "operation"=>"create",
            "notification_key_name"=> (string)time(),//設備組名
            "registration_ids"=>[$fcmtoken]
        ];
        $res =  json_decode( $this->http($url,$data,'post',$header) ,true);
        return $res['notification_key'];
    }

    //向主題發送消息
    public function sendMessageToGroup(){
        $url = 'https://fcm.googleapis.com/fcm/send';
        $header = [
            'Content-Type:application/json',
            'Authorization:key='.$this->apikey,
            'project_id:196236249110',
        ];
        $data=[
            'to'=>'/topics/本身的主題名',
            "notification"=>[
                'body'=>"^_^ Come and get the coins.There are a lot of new tasks. ^_^",
                'title'=>'',
                'icon'=>'myicon'
            ],
        ];
        $res = $this->http($url,$data,'post',$header);
        var_dump($res);
    }

    /**
     * 獲取本身的主題內容
     */
    private function myTopic($fcmtoken='')
    {
        $url='https://iid.googleapis.com/iid/info/'.$fcmtoken.'?details=true';
        $header = [
//            'Content-Type:application/json',
            'Authorization:key='.$this->apikey,
            'details:true',
        ];
        $res=$this->http($url,'','get',$header);
        var_dump($res);
    }

    /**
     * 給本身添加主題 wdwdinstagram
     * @param $fcmtoken
     */
    private function oldmyTopic($fcmtoken)
    {
        $url='https://iid.googleapis.com/iid/v1/'.$fcmtoken.'/rel/topics/本身的主題名';
        $header = [
            'Content-Type:application/json',
            'Authorization:key='.$this->apikey,
            'Content-Length: 0',
        ];
        $this->http($url,'','post',$header);
    }
}
相關文章
相關標籤/搜索