• iphone 消息推送 实现


    IPhone 消息推送实现

    参考 资料 http://blog.csdn.net/victormokai/article/details/39501277

    对生成pem 的补充

    拿到mac 上生成导出的两个文件 cert.p12 , key.p12 放到一个文件夹下
    终端切换到这个文件夹下

    openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
    生成cert.pem;输入访问密码123456,设置pem文件的密码123456(服务器访问pem的密码)
     
    openssl pkcs12 -nocerts -out key.pem -in key.p12
    生成key.pem;操作同上
    
    (可选)
    openssl rsa -in key.pem -out key.unencrypted.pem如果需要对key不进行加密。
    
    cat cert.pem key.unencrypted.pem > ck.pem
    合并两个.pem文件, 这个ck.pem就是服务端需要的证书了。
    
    
    
    
    
    

    PHP 案例

    <?php
    /**
     * PHP iospush
     *
     * Copyright (c) 2006 - 2014 iospush
     *
     *
     * @category   PHP iospush
     * @copyright  Copyright (c) iospush (http://www.leipi.org)
     * @license    http://www.apple.com    LGPL
     * @version    1.8.0, 2014-03-02
     */
    class iospush
    {
    	public $path='cert.pem';//证书
    	public $pass='linksame'; //密码
    	public $sound = 'tap.aif'; //ͨ声音
    	public $badge=0;
    	
    	public function __construct($path='',$pass='') {
    		if($path)
    			$this->path = $path;
    		if($pass)
    			$this->pass = $pass;
    
    	}
    	function push($token, $data)
    	{
    		
    		$ctx = stream_context_create();
    		stream_context_set_option($ctx, 'ssl', 'local_cert', $this->path);
    		stream_context_set_option($ctx, 'ssl', 'passphrase', $this->pass);
    		
    		// Open a connection to the APNS server
    		//这个为正是的发布地址
    		//$fp = stream_socket_client('tls://gateway.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
    		//这个是沙盒测试地址,发布到appstore后记得修改哦
    		$fp = stream_socket_client('tls://gateway.sandbox.push.apple.com:2195', $err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
    		
    
    		
    		if (!$fp){
    			return false;
    		}
    			
    		
    		
    		// Create the payload body
    		$body['aps'] = array(
    				'alert' => $message,
    				'sound' => 'default'
    		);
    		
    		$body['aps']['alert'] = $data['description'];
    		$body['aps']['sound'] = 'default';
    		foreach($data as $k => $v) {
    			if ($k != "description") {
    				$body[$k] = $v;
    			}
    		}
    		
    		// Encode the payload as JSON
    		$payload = json_encode($body);
    		
    		// Build the binary notification
    		$msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', $token)) . pack('n', strlen($payload)) . $payload;
    		
    		// Send it to the server
    		$result = fwrite($fp, $msg, strlen($msg));
    		
    		if (!$result)
    			return  false;
    			//echo 'Message not delivered' . PHP_EOL;
    		else
    			//echo 'Message successfully delivered' . PHP_EOL;
    			return  true;
    		// Close the connection to the server
    		fclose($fp);
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		// Construct the notification payload
    // 		$body = array();
    // 		if ($this->badge) {
    // 			$body['aps']['badge'] = $this->badge;
    // 		} 
    // 		$body['aps']['alert'] = $data['description'];
    // 		$body['aps']['sound'] = $this->sound; 
    // 		// ��װ���
    // 		foreach($data as $k => $v) {
    // 			if ($k != "description") {
    // 				$body[$k] = $v;
    // 			} 
    // 		} 
    		
    // 		// $body['append_1']="appendent_1";
    // 		// $body['append_2']="appendent_2";
    // 		$ctx = stream_context_create();
    // 		stream_context_set_option($ctx, 'ssl', 'local_cert', $path); //pem�ļ���ַ
    // 		stream_context_set_option($ctx, 'ssl', 'passphrase', $this->pass); //֤������
    		 
    // 		// $fp=stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);//��ʽ
    // 		$fp = @stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx); //����ɳ��
    		
    // 		if (!$fp) {
    // 			return false;
    // 		} 
    
    // 		$payload = json_encode($body);
    // 		$msg = chr(0) . pack("n", 32) . pack('H*', str_replace(' ', '', $token)) . pack("n", strlen($payload)) . $payload;
    
    // 		fwrite($fp, $msg);
    // 		fclose($fp);
    		return true;
    	} 
    }
    
    
    $deviceToken='93f852218f8c911b40432999ab1d02280a491e8ed0d64a0178e2d7f82e827452';  
    
    $pp= new iospush('ck.pem','linksame');
    $data['description']="回个哈哈哈";
    $statusss=$pp->push($deviceToken, $data);
    var_dump($statusss);
    ?>
    
    
  • 相关阅读:
    C++之友元机制(友元函数和友元类)
    C++之全局函数和成员函数互相转换
    C++之面向对象初探----对象管理模型(关键是this指针)
    C++之const类成员变量,const成员函数
    C++之static类成员,static类成员函数
    c++之函数值传递和引用传递解析----关键在于理解函数return的实现机制(内存分配)
    C++之new和delete
    C++之构造函数、参数列表、析构函数
    C++之匿名对象解析
    C结构体、C++结构体、C++类的区别
  • 原文地址:https://www.cnblogs.com/freefei/p/6830636.html
Copyright © 2020-2023  润新知