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);
}
}
google fcm 推送的流程的更多相关文章
- Google FireBase - fcm 推送 (Cloud Messaging)
要将 Firebase 添加到您的应用,您需要有一个 Firebase 项目以及适用于您的应用的 Firebase 配置文件. 如果您还没有 Firebase 项目,请在 Firebase 控制台中创 ...
- [iOS 高级] iOS远程推送与本地推送大致流程
本地推送: UILocalNotification *notification=[[UILocalNotification alloc] init]; if (notification!=nil) { ...
- iOS推送通知流程
①注册推送通知使用方法:registerUserNotificationSettings, registerForRemoteNotifications ④APP发送deviceToken到第三方: ...
- App集成极光推送开发流程[关键步骤]
1.客户端集成SDK 1.1初始化 JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志 JPushInterface.init(this); / ...
- 微信小程序简单的推送消息流程
1.进入开发设置-消息推送,启用消息推送 url: 启用并设置消息推送配置后,用户发给小程序的消息以及开发者需要的事件推送,都将被微信转发至该服务器地址中. 2.创建消息模板. 3.WXML代码: 4 ...
- GCM(Google Cloud Messaging)推送完全解析
应该是目前为止全网最详细的GCM推送解析. GCM官方解释: https://developers.google.cn/cloud-messaging/?hl=zh-cn Send data from ...
- 使用GCM服务(Google Cloud Messaging)实现Android消息推送
最近在网上查了关于很多Android消息推送的资料,其中主要有四种方法. 1) 使用GCM服务(Google Cloud Messaging) 2) 使用XMPP协议(Openfire + Spark ...
- 海外 App 的推送服务,试试 FCM 吧!!!
> **版权声明:** > > **本账号发布文章均来自公众号,承香墨影(cxmyDev),版权归承香墨影所有.** > > **每周会统一更新到这里,如果喜欢,可关注公 ...
- python微信服务号关注授权、消息推送流程
阅读目录 推送的方式: 微信推送的流程: 流程分析: 给用户推送消息功能 推送的方式: 短信推送(第三方) 邮件推送 微信推送 公众号:认证的公众号(个人的认证公众号每天只能发一篇文章),粉丝可以跟公 ...
随机推荐
- [APIO2015]巴厘岛的雕塑 贪心+DP+特殊数据优化
写了好久.... 刚刚调了一个小时各种对拍,,,,最后发现是多写了一个等号,,,,内心拒绝 表示一开始看真的是各种懵逼啊 在偷听到某位大佬说的从高位开始贪心后发现可做 首先考虑小数据(因为可以乱搞) ...
- BZOJ4827:[AH2017/HNOI2017]礼物——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4827 https://www.luogu.org/problemnew/show/P3723 题面 ...
- CC DGCD:Dynamic GCD——题解
https://vjudge.net/problem/CodeChef-DGCD https://www.codechef.com/problems/DGCD 题目大意: 给一颗带点权的树,两个操作: ...
- 洛谷 P3521 [POI2011]ROT-Tree Rotations 解题报告
P3521 [POI2011]ROT-Tree Rotations 题意:递归给出给一棵\(n(1≤n≤200000)\)个叶子的二叉树,可以交换每个点的左右子树,要求前序遍历叶子的逆序对最少. 大体 ...
- Kindle 电子书相关的工具软件【转】
这里是与 Kindle 电子书相关的工具软件.它们可以帮助我们解决在日常使用电子书时所可能遇到的问题,比如 kindle 管理工具.kindle 转换工具.kindle电子书制作工具.kindle 推 ...
- Pycharm中一些不为人知的技巧
工欲善其事必先利其器,Pycharm 是最受欢迎的Python开发工具,它提供的功能非常强大,是构建大型项目的理想工具之一,如果能挖掘出里面实用技巧,能带来事半功倍的效果. 以下操作都是基于 Wind ...
- 【贪心】【CF3D】 Least Cost Bracket Sequence
传送门 Description 给一个序列,序列里面会有左括号.问号.右括号.对于一个\(?\)而言,可以将其替换为一个\((\),也可以替换成一个\()\),但是都有相应的代价.问:如何替换使得代价 ...
- c++ linux 判断string是中文的 or 英文的 字符串。
#include <iostream> #include <string.h> #include <stdio.h> #include <stdlib.h&g ...
- 使用snmp4j实现Snmp功能(一)
相关链接:Snmp学习笔记使用snmp4j实现Snmp功能(一)使用snmp4j实现Snmp功能(二)使用snmp4j实现Snmp功能(三) 上一篇文章讲了Snmp的一些基本概念(Snmp学习笔记), ...
- Qt ------ 内存回收机制、new对象的回收
写在前面的总结: 建议:对于不能指定父对象的对象(对象通过moveToThread()移入其他线程.没有继承QObject的类产生的对象),在其他线程通过deleteLater()内存回收,其他通过指 ...