<?php
namespace common\extensions\push;
use \CComponent; /**
* @desc iphone推送的接口程序
*/
class ApnsPush extends CComponent{ //证书地址
public $cert; //ipad证书地址
public $ipadCert; //密钥
public $passphrase; public $applePushUrl = 'ssl://gateway.push.apple.com:2195'; //ssl://gateway.sandbox.push.apple.com:2195测试地址
public $defaultConf = array(
'badge' => 0,
'sound' => 'default',
'ispad' => 0,
'alert' => '',
'token' => ''
);
public $failedSend = array();
public $resendTime = 0;
public $maxResendTime = 10; function init()
{ } /**
* 获取证书
* @param boolean $ispad
*/
function getCert($ispad = false)
{
return $ispad ? (empty($this->ipadCert) ? $this->cert : $this->ipadCert) : $this->cert;
} function getPayload($data = array())
{
$body = array();
strlen($data['alert']) > 87 && $data['alert'] = mb_convert_encoding(substr($data['alert'], 0, 87), 'UTF-8', 'UTF-8') . '...';
$body['aps']['alert'] = $data['alert'];
$body['aps']['badge'] = (int) $data['badge'];
$body['aps']['sound'] = $data['sound'];
$body = array_merge($body, array_diff_assoc(array_merge($data, $this->defaultConf), $this->defaultConf));
return $body;
} /**
* 推送
* @param array $sendInfo
* @return boolean
*/
function send($sendInfo = array())
{
$ctx = stream_context_create();
$sendInfo = array_merge($this->defaultConf, $sendInfo);
stream_context_set_option($ctx, 'ssl', 'local_cert', $this->getCert($sendInfo['ispad']));
stream_context_set_option($ctx, 'ssl', 'passphrase', $this->passphrase); //如果设置了密码,这里就不能注释了
$fp = stream_socket_client($this->applePushUrl, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp)
{
return false;
}
$payload = json_encode($this->getPayload($sendInfo));
//echo strlen($payload); //这里可以精心测试,最大不能超过256个字节即strlen超过256后苹果直接不予处理。
$msg = chr(0) . pack("n", 32) . pack('H*', str_replace(' ', '', $sendInfo['token'])) . pack("n", strlen($payload)) . $payload;
fwrite($fp, $msg);
fclose($fp);
return true;
} /**
* 批量推送
* @param array $batchSendInfo
*/
function batchSend($batchSendInfo = array())
{
$this->failedSend = array();
if( ! empty($batchSendInfo))
{
foreach ($batchSendInfo as $val)
{
if( ! $this->send($val))
{
$this->failedSend[] = $val;
}
}
if( ! empty($this->failedSend) && $this->resendTime < $this->maxResendTime )
{
$this->resendTime++;
$this->batchSend($this->failedSend);
}
}
}
}

ios推送基于YII第三方组件的类库的更多相关文章

  1. 友盟iOS推送配置(从真机调试到推送)

    下面我来讲解一下友盟iOS的推送配置,其实友盟只是一个示例,换做其余的第三方推送服务也会适用,只是第三方的后面服务变了而已. iOS推送(包括真机调试)所需要的步骤和文件如下: 备注:这里我将省略掉一 ...

  2. iOS 推送全解析

    本文旨在对 iOS 推送(以下简称 推送)进行一个完整的剖析,如果你之前对推送一无所知,那么在你认真地阅读了全文后必将变成一个推送老手,你将会对其中的各种细节和原理有充分的理解.以下是 pikacod ...

  3. 利用pyapns进行ios推送

    ios客户端的同事提供了2个证书,分别是aps_development.cer  和 pushdevelopment.p12, 我最终目的就是把这2个证书合并成1个pem证书,然后通过pyapns进行 ...

  4. iOS推送介绍

    iOS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPhone软件的Push服务器,APNS是Apple Push Notification Service的缩写,是苹果的服务 ...

  5. iOS 推送问题全解答《十万个为啥吖?》

    Q 1:为啥收不到推送(1)? 如果收到推送时,App 在前台运行,那么: iOS 10 before 顶部横幅不会弹出.没有任何展示,你以为「没有收到推送」. iOS 10 after 如果没有实现 ...

  6. iOS 推送全解析,你不可不知的所有 Tips!

    本文旨在对 iOS 推送进行一个完整的剖析,如果你之前对推送一无所知,那么在你认真地阅读了全文后必将变成一个推送老手,你将会对其中的各种细节和原理有充分的理解.以下是 pikacode 使用 iOS ...

  7. iOS推送证书转pem文件

    iOS推送证书转 .pem文件. 推送证书转pem文件openssl x509 -in apns_miaobozhibo.cer -inform der -out apns_miaobozhibo.p ...

  8. IOS 推送-客户端处理推送消息

    IOS 推送-客户端处理推送消息 1.推送调用顺序 APN push的消息到达后,UIApplicationDelegate有两个方法和处理消息有关: 1)application:didReceive ...

  9. IOS 推送-配置与代码编写

    IOS 推送配置与代码编写 这里介绍IOS的推送,本文章已经在IOS6/7/8上都能运行OK,按照道理IOS9应该没问题. 大纲: 1.文章前提 2.推送介绍 3.推送文件账号设置 4.推送证书介绍 ...

随机推荐

  1. 在windows2012&2008中设置防火墙允许filezilla的passive模式

    添加入战规则, 允许程序和服务 "filezilla" 被连接 只允许21端口通过防火墙是不够的.

  2. C#程序实现动态调用DLL的研究

    摘 要:在<csdn开发高手>2004年第03期中的<化功大法——将DLL嵌入EXE>一文,介绍了如何把一个动态链接库作为一个资源嵌入到可执行文件,在可执行文件运行时,自动从资 ...

  3. 博客转移到lyso.me

    博客转移到http://lyso.me :)

  4. android开发学习:打电话和发短信

    1.新建一个android项目 File--New--Other--android application project 填写application name(就是应用的名字.比方:天天酷跑) 填写 ...

  5. Windows二进制文件的Python扩展包

    http://www.lfd.uci.edu/~gohlke/pythonlibs/ https://pypi.python.org/simple/

  6. MySQL 中的两种临时表

    来源:阿里云RDS - 数据库内核组 链接:http://mysql.taobao.org/monthly/2016/06/07/ 外部临时表 通过CREATE TEMPORARY TABLE 创建的 ...

  7. C如何获取文件夹下所有文件

    http://baike.baidu.com/view/1186290.htm?fr=aladdin 使用io.h中的_findfirst,_findnext,_findclose,_finddata ...

  8. 文件和目录之chmod和fchmod函数

    本篇博文内容摘自<UNIX环境高级编程>(第二版),仅作个人学习记录所用.关于本书可参考:http://www.apuebook.com/. 这两个函数使我们可以更改现有文件的访问权限: ...

  9. 心跳之旅—💗—iOS用手机摄像头检测心率(PPG)

    [前情提要] 光阴似箭,日月如梭,最近几年,支持心率检测的设备愈发常见了,大家都在各种测空气测雪碧的,如火如荼,于是我也来凑一凑热闹.[0]这段时间,我完成了一个基于iOS的心率检测Demo,只要稳定 ...

  10. JAVA_HttpClientUtils

    package org.mobiletrain.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStre ...