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微信服务号关注授权、消息推送流程
阅读目录 推送的方式: 微信推送的流程: 流程分析: 给用户推送消息功能 推送的方式: 短信推送(第三方) 邮件推送 微信推送 公众号:认证的公众号(个人的认证公众号每天只能发一篇文章),粉丝可以跟公 ...
随机推荐
- [洛谷P4139]上帝与集合的正确用法
题目大意:多次询问,每次给你$p$询问$2^{2^{2^{\dots}}}\bmod p$ 题解:扩展欧拉定理,求出$\varphi(p)$即可.因为$2^{2^{2^{\dots}}}>> ...
- poj3207 Ikki's Story IV - Panda's Trick 2-sat问题
---题面--- 题意:给定一个圈,m条边(给定),边可以通过外面连,也可以通过里面连,问连完这m条边后,是否可以做到边两两不相交 题解: 将连里面和连外面分别当做一种决策(即每条边都是决策点), 如 ...
- BZOJ1008:[HNOI2008]越狱——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=1008 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中 ...
- BZOJ3236:[AHOI2013]作业——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3236 第一种做法: 建两棵主席树分别处理两个问题. 第一个问题水,第二个问题参考SPOJ3267/ ...
- 删边(cip)
删边(cip) 给出一个没有重边和自环的无向图,现在要求删除其中两条边,使得图仍然保持连通. 你的任务是计算有多少组不合法的选边方案.注意方案是无序二元组. Sol 神题,无从下手啊. 考虑点dfs建 ...
- 手动实现一个简易版SpringMvc
版权声明:本篇博客大部分代码引用于公众号:java团长,我只是在作者基础上稍微修改一些内容,内容仅供学习与参考 前言:目前mvc框架经过大浪淘沙,由最初的struts1到struts2,到目前的主流框 ...
- LCD显示屏原理与应用
1.什么是LCD? (1)LCD(Liquid Crystal Display)俗称液晶.(2)液晶是一种材料,液晶这种材料具有一种特点:可以在电信号的驱动下液晶分子进行旋转,旋转时会影响透光性,因此 ...
- MySQL 5.6 for Windows 解压缩版配置安装(转载)
原文地址:点击这里 MySQL是一个小巧玲珑但功能强大的数据库,目前十分流行.但是官网给出的安装包有两种格式,一个是msi格式,一个是zip格式的.很多人下了zip格式的解压发现没有setup.exe ...
- php中相关函数
1.php标准风格 <?php //这是标准风格 echo '推荐标准风格'; ?> 2.php中文乱码 .html:<meta http-equiv="Content-T ...
- Python爬虫学习笔记之点触验证码的识别
代码: Chaojiying.py: #!/usr/bin/env python # coding:utf-8 import requests from hashlib import md5 clas ...