<?php
/**
* 安卓和苹果消息主推
*/
class JPush extends CComponent
{ private $_push_body = array();
private $_android_body = array();
private $_ios_body = array();
const PUSH_URL = 'https://api.jpush.cn/v3/push';
const REPORT_URL = 'https://report.jpush.cn/v2/received';
const VALIDATE_URL = 'https://api.jpush.cn/v3/push/validate';
const CONNECT_TIMEOUT = 5;
const READ_TIMEOUT = 30;
const DEFAULT_MAX_RETRY_TIMES = 3;
private $_header = '';
private $_auth_basic = '';
private $_app_key = 'a7d627a70f752f32de5a0939';
private $_master_secret = 'f02725c2a9802badb60ab312';
public $retryTimes; public function __construct($retryTimes = self::DEFAULT_MAX_RETRY_TIMES, $appKey = '', $masterSecret = '')
{
$this->retryTimes = $retryTimes; if(!empty($appKey))
$this->_app_key = $appKey;
if(!empty($masterSecret))
$this->_master_secret = $masterSecret; //base64(appKey:masterSecret)
$this->_auth_basic = base64_encode($this->_app_key . ':' . $this->_master_secret);
//$this->_auth_basic = $this->_app_key . ':' . $this->_master_secret;
$this->_push_body = array(
'platform' => 'ios',// 推送平台设置 必填
'audience' => 'all', //推送设备指定 必填
//'notification' => array(), // 通知内容体。是被推送到客户端的内容。与 message 一起二者必须有其一,可以二者并存 可选
//'message' => array(), //推送参数 可选
'options' => array('time_to_live' => 30, 'apns_production' => false), //推送参数 可选
);
$this->_ios_body = array(
'alert' => '',
'sound' => 'ding.caf',
'badge' => '+1',
);
$this->_android_body = array(
'alert' => '',
'title' => 'send to android',
'builder_id' => 1,
);
$this->_header = array(
'Connection: Keep-Alive',
'Charset: UTF-8',
'Content-Type: application/json',
'Authorization: Basic ' . $this->_auth_basic,
); } /**
* notification android
*/
public function setAndroid($alert, $title = '', $builder_id = '')
{
$this->_android_body['alert'] = $alert;
if(!empty($title))
$this->_android_body['title'] = $title;
if(!empty($builder_id))
$this->_android_body['builder_id'] = $builder_id; $this->_push_body['notification']['android'] = $this->_android_body; }
/**
* notification ios
*/
public function setIos($alert, $sound = '', $badge = '')
{
$this->_ios_body['alert'] = $alert;
if(!empty($sound))
$this->_ios_body['sound'] = $sound;
if(!empty($badge))
$this->_ios_body['badge'] = $badge;
$this->_push_body['notification']['ios'] = $this->_ios_body;
}
/**
* set extras
*/
public function setExtras($extras)
{
if(!empty($extras) && is_array($extras)) {
if(!empty($this->_push_body['notification']['ios']))
$this->_push_body['notification']['ios']['extras'] = $extras;
if(!empty($this->_push_body['notification']['android']))
$this->_push_body['notification']['android']['extras'] = $extras;
}
} /**
* set options
*/
public function setOptions($options)
{
$this->_push_body['options'] = $options;
} /**
* 设置 推送
*/
public function push($platform = '', $audience = '')
{
if(!empty($platform)) {
$this->_push_body['platform'] = $platform;
}
if(!empty($audience)) {
$this->_push_body['audience'] = $audience;
} $data = json_encode($this->_push_body);
$res = $this->curlPost(self::PUSH_URL, $data, $this->_header); return $res;
} /**
* curl post
*/
public function curlPost($url, $data, $header = array())
{
$ch = curl_init();
if(!empty($header))
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
return $result;
}
/**
* curl get
*/
public function curlGet($url, $data, $header = array())
{
$ch = curl_init();
if(!empty($header))
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url.'?'.http_build_query($data));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
ob_start();
curl_exec($ch);
$result = ob_get_contents() ;
ob_end_clean(); return $result;
} }

  

php 使用 极光推送 类的更多相关文章

  1. 总结:极光推送java服务端(1)

    遇到的问题: 1.怎么用极光推送 2.极光推送发送失败报错 返回{ } 3.透传和推送区别以及怎么设置 我的解决方案: 问题1.极光推送类里面有不同的方法,需要发给那些人就调用相应的方法.有安卓.io ...

  2. 极光推送Jpush(v3)服务端PHP版本的api脚本类

    原文地址:http://www.dodobook.net/php/780 关于极光推送的上一篇文章已经说明了,此处就不多说了.使用v3版本的原因是v2使用到2014年年底就停止了.点击查看上一篇的地址 ...

  3. 使用极光推送(www.jpush.cn)向安卓手机推送消息【服务端向客户端主送推送】C#语言

    在VisualStudio2010中新建网站JPushAndroid.添加引用json帮助类库Newtonsoft.Json.dll. 在web.config增加appkey和mastersecret ...

  4. 用JPUSH极光推送实现服务端向安装了APP应用的手机推送消息(C#服务端接口)

    这次公司要我们做一个功能,就是当用户成功注册以后,他登录以后要收到消息,当然这个消息是安装了我们的手机APP应用的手机咯. 极光推送的网站的网址是:https://www.jpush.cn/ 极光推送 ...

  5. iOS推送(利用极光推送)

    本文主要是基于极光推送的SDK封装的一个快速集成极光推送的类的封装(不喜勿喷) (1)首先说一下推送的一些原理: Push的原理: Push 的工作机制可以简单的概括为下图 图中,Provider是指 ...

  6. 极光推送Jpush(v3)服务端PHP版本集成(V3版本只调用推送API)

    因为版本升级,极光推送的API也有了V3,功能也更丰富了,但是对于我们有的用户来说,我们还是只需要调用推送的API就够了. 下载了一份PHP服务端的SDK(下载地址:http://docs.jpush ...

  7. JPush API client library for C Sharp(极光推送API)

    概述 这是 JPush REST API 的 C# 版本封装开发包,是由极光推送官方提供的,一般支持最新的 API 功能. 对应的 REST API 文档:http://docs.jpush.io/s ...

  8. (转载)iOS 极光推送SDK 集成指南

    iOS SDK 集成指南 使用提示 本文匹配的 SDK版本:r1.2.5 以后. 查看最近更新了解最新的SDK更新情况. 产品功能说明 极光推送(JPush)是一个端到端的推送服务,使得服务器端消息能 ...

  9. java 极光推送

    Web.xml配置文件 <context-param> <param-name>contextConfigLocation</param-name> <par ...

随机推荐

  1. group_concat函数使用

    t1表 语句: select type,group_concat(name) from t1 group by type 结果

  2. information_schema系列十二

    1: INNODB_SYS_VIRTUAL 表存储的是INNODB表的虚拟列的信息,当然这个还是比较简单的,我们直接通过SHOW CREATE TABLE 或者DESC TABLE就能看得到. Col ...

  3. Windows 10设置桌面图标间距、窗口的背景颜色、选中文字的背景颜色

    Windows 10取消了“高级外观设置”(或者叫“窗口颜色和外观”设置),想调整一些参数只能进注册表了. 修改后可能需要注销或重启才能生效. 按Win+R,然后输入regedit进入注册表编辑器. ...

  4. leetCode 354. Russian Doll Envelopes

    You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...

  5. Unity3D 物体移动方式

    1. 简介 在Unity3D中,有多种方式可以改变物体的坐标,实现移动的目的,其本质是每帧修改物体的position. 2. 通过Transform组件移动物体 Transform 组件用于描述物体在 ...

  6. Appium学习路—Android定位元素与操作

    一.常用识别元素的工具 uiautomator:Android SDK自带的一个工具,在tools目录下 monitor:Android SDK自带的一个工具,在tools目录下 Appium Ins ...

  7. python 学习 第一课

    # -*- coding: utf-8 -*- import urllib2 import cookielib url="http://www.baidu.com" print ' ...

  8. 解决: maven编译项目报“非法字符: \65279 ”错误

    打包maven项目的时候,出现异常: [INFO] ------------------------------------------------------------------------ [ ...

  9. flexbox布局

    一.侧轴对齐伸缩项目--align-items 它充许调整伸缩项目在侧轴(也就是y轴)的对齐方式,主要包括以下几个值: flex-start/baseline:伸缩项目在侧轴起点边的外边距紧靠住该行在 ...

  10. python 识别图片验证码报IOError

    说一下困扰了我一周的问题:识别图片验证码 本来我按照安装步骤(http://www.cnblogs.com/yeayee/p/4955506.html?utm_source=tuicool&u ...