java实现谷歌fcm推送_google fcm 推送的流程

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值