zoukankan      html  css  js  c++  java
  • 利用ApnsPHP包向IOS推送消息

    header('content-type:text/html;charset=utf-8');
    
    require_once 'ApnsPHP/Autoload.php';
    require_once 'ApnsPHP/Push.php';
    require_once 'ApnsPHP/Message.php';
    require_once 'ApnsPHP/Push/Exception.php';
    
    function push_ios_message($fans, $push_id, $text, $message_type, $artist_id)
    {
        $user_count  = count($fans);
    
        if ($user_count == 0) {
            return;
        }
    
        $pem  = './aaa.pem';
        $password = '1234';
    
        //$mode = ApnsPHP_Abstract::ENVIRONMENT_SANDBOX;
        $mode = ApnsPHP_Abstract::ENVIRONMENT_PRODUCTION;
       
        // Instanciate a new ApnsPHP_Push object
        $push = new ApnsPHP_Push($mode, $pem, $password);
        // Set the Root Certificate Autority to verify the Apple remote peer
        $push->setRootCertificationAuthority($pem);
    
        // Connect to the Apple Push Notification Service
        $push->connect();
    
        $message = '';
        for ($i = 0; $i < $user_count; $i++) {
            $fans[$i]['device_token'] = trim($fans[$i]['device_token']);
    
            try {
                // Instantiate a new Message with a single recipient
                $message = new ApnsPHP_Message($fans[$i]['device_token']);
                // Set a custom identifier. To get back this identifier use the getCustomIdentifier() method
                // over a ApnsPHP_Message object retrieved with the getErrors() message.
                $message->setCustomIdentifier("Message-Badge-3");
                // Set badge icon to "3"
                $message->setBadge(8);
                // Set a simple welcome text
                $message->setText($text);
                // Play the default sound
                $message->setSound();
                $message->setCustomProperty('push_id', $push_id);
                $message->setCustomProperty('message_type', $message_type);
                // Set the expiry value to 30 seconds
                $message->setExpiry(30);
                // Add the message to the message queue
                $push->add($message);
            } catch (Exception $e) {
                $message = '';
                var_dump($e->getMessage());
            }
    
        }
    
        try{
            $push->send();
            $push->disconnect();
    
            // Examine the error message container
            $aErrorQueue = $push->getErrors();
            if (!empty($aErrorQueue)) {
                var_dump($aErrorQueue);
                var_dump(serialize($aErrorQueue));
            }
        } catch(Exception $e) {
            echo 'Exception:';
            var_dump($e->getMessage());
        }
    
    }
    

    三方包下载地址 

    https://github.com/zhoutingze/adtuu.git

  • 相关阅读:
    MySQL优化十大技巧
    50个常用sql语句 网上流行的学生选课表的例子
    JDK常用命令
    linux中用date命令获取昨天、明天或多天前后的日期
    ***如何优雅的选择字体(font-family)
    将WordPress安装在网站子目录的相关问题
    PHP源码加密- php-beast
    如何安装ioncube扩展对PHP代码加密
    PHP防止表单重复提交的解决方法
    CI中SESSION的用法及其注意
  • 原文地址:https://www.cnblogs.com/adtuu/p/4670513.html
Copyright © 2011-2022 走看看