在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
前几天刚写完的一个东西,写的比较简单,没有使用其他插件,原生php+计划任务实现 极光推送的代码
/* $receiver="registration_id" : [ "4312kjklfds2", "8914afd2", "45fdsa31" ]; $receiver="tag" : [ "深圳", "广州", "北京" ] $receiver= "tag" : [ "深圳", "广州" ] $receiver= "tag_and" : [ "女", "会员"] //自定义类型推送类型 $m_type = 'http';//推送附加字段的类型 $m_txt = 'http://www.groex.cn/';//推送附加字段的类型对应的内容(可不填) 可能是url,可能是一段文字。 $m_time = '86400';//离线保留时间 * * { "platform" : "all" } * * { "platform" : ["android", "ios"] } * 测试成不成功记得看管理平台上面的统计信息,因为之前把apns_production参数设置成了生产环境,调用接口成功,可是却没有用户,后来改为了开发环境,就成功了。 */ function jiguang_send($receive, $content, $platform, $m_type, $m_txt, $m_time) { $appkey = ''; //AppKey $secret = ''; //Secret $postUrl = "https://api.jpush.cn/v3/push"; $base64 = base64_encode("$appkey:$secret"); $header = array("Authorization:Basic $base64", "Content-Type:application/json"); $data = array(); $data['platform'] = $platform; //目标用户终端手机的平台类型android,ios,winphone $data['audience'] = $receive; //目标用户 $data['notification'] = array( //统一的模式--标准模式 "alert" => $content, //安卓自定义 "android" => array( "alert" => $content, "title" => "", "builder_id" => 1, "extras" => array("type" => $m_type, "txt" => $m_txt) ), //ios的自定义 "ios" => array( "alert" => $content, "badge" => "1", "sound" => "default", "extras" => array("type" => $m_type, "txt" => $m_txt) ) ); //苹果自定义---为了弹出值方便调测 $data['message'] = array( "msg_content" => $content, "extras" => array("type" => $m_type, "txt" => $m_txt) ); //附加选项 $data['options'] = array( "sendno" => time(), "time_to_live" => $m_time, //保存离线时间的秒数默认为一天 "apns_production" => false, //布尔类型 指定 APNS 通知发送环境:0开发环境,1生产环境。或者传递false和true ); $param = json_encode($data); // $postUrl = $this->url; $curlPost = $param; $ch = curl_init(); //初始化curl curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定网页 curl_setopt($ch, CURLOPT_HEADER, 0); //设置header curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_POST, 1); //post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); // 增加 HTTP Header(头)里的字段 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 终止从服务端进行验证 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); $return_data = curl_exec($ch); //运行curl curl_close($ch); return $return_data; } CREATE TABLE `v1_jiguang_user_data` ( `jg_id` int(10) NOT NULL AUTO_INCREMENT, `jiguang_id` varchar(200) CHARACTER SET utf8 NOT NULL, `user_id` int(10) NOT NULL, `app_type` varchar(200) CHARACTER SET utf8 NOT NULL, `is_sent` int(2) NOT NULL DEFAULT '1' COMMENT '判定是都接受app推送,默认1是推送 2是不推送', `time` int(11) NOT NULL COMMENT '登录时间', `user_sex` int(2) DEFAULT NULL COMMENT '性别 1为男 0为女', `longitude` varchar(30) CHARACTER SET utf8 DEFAULT NULL COMMENT '经度', `latitude` varchar(30) CHARACTER SET utf8 DEFAULT NULL COMMENT '纬度', `region_tag` varchar(40) CHARACTER SET utf8 DEFAULT NULL COMMENT '地区tag', PRIMARY KEY (`jg_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `v1_push_message_history` (
CREATE TABLE `v1_message_task` (
然后在crontab里面执行就可以了,查询的时候只需要根据v1_jiguang_user_data的男,女,地区来查询,推送给执行用户,不然就全部发送,直接把 $receiver="registration_id" : [ "4312kjklfds2", "8914afd2", "45fdsa31" ];这样发送就Ok了
|
2022-07-18
2022-08-17
2022-11-06
2022-07-08
2022-08-17
请发表评论