dongqing220586 2012-05-03 13:18
浏览 33
已采纳

如何在Code Igniter项目中包含APNS-PHP?

I've successfully used APNS-PHP to send PNs - it's great!

However, I'm a little stuck on how to add it to a Code Igniter project (inside a controller that will be called from command-line).

Calling:

require_once APPPATH.'/third_party/ApnsPHP/Autoload.php'; 

Results in errors:

Fatal error: Uncaught exception 'Exception' with message 'Class file 
'XXXXXXX/application/third_party/CI/DB/mysql/result.php' does not 
exists' in XXXXXXX/application/third_party/ApnsPHP/Autoload.php:49 

I'm assuming it's some kind of autoloading conflict? But I'm not really sure!

Any help would be brilliant - I've trawled Google without much luck!

Here's the line I've being trying to use inside the function:

require_once APPPATH.'/third_party/ApnsPHP/Autoload.php';

Here's the __autoload function I've added for loading Ion Auth's libs:

function __autoload($class)
{

    if (strpos($class, 'CI_') !== 0)
    {
        if (file_exists($file = APPPATH.'core/'.$class.EXT))
        {
            include $file;
        }
        else if (file_exists($file = APPPATH.'libraries/'.$class.EXT))
        {
            include $file;
        }
        else if (file_exists($file = APPPATH.'core/base_controllers/'.$class.EXT))
        {
            include $file;  
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dousha1831 2013-05-19 04:09
    关注

    With no other changes to the library, this works for me. It sidesteps CI a bit, but still allows you to load in APNS-PHP as a model:

    <?php
    
    if(!defined('BASEPATH')) exit('No direct script access allowed');
    
    class Notification_model extends CI_Model {
    
        protected $apnsDir = '';
    
        // -----------------------------------------------
    
        /**
         * Setup some basic stuffs
         * @param void
         * @return void
         * @access public 
         */
        public function __construct() {
    
            parent::__construct();
    
            /* get all the APNS files */
            $this->apnsDir = $_SERVER['DOCUMENT_ROOT'].'/application/third_party/ApnsPHP/';
            $this->_apns_req();
    
            return;
    
        } /* /__construct() */
    
        // -----------------------------------------------
    
        /**
         * Will send the actual iOS notification to the user
         * @param $token string iOS device token
         * @param $msg string 
         * @param $attrs array Key/value pairs to be sent as meta with APN
         * @return void
         * @access public
         */
        private function send_ios($token=null, $msg=null, $attrs=array()) {
    
            if(!$token || !$msg) return;
    
            // Instantiate a new ApnsPHP_Push object
            $push = new ApnsPHP_Push(
                ApnsPHP_Abstract::ENVIRONMENT_SANDBOX,
                $this->apnsDir.'SSL/server_certificates_bundle_sandbox.pem'
            );
    
            // Set the Provider Certificate passphrase
            // $push->setProviderCertificatePassphrase('tablecan29');
    
            // Set the Root Certificate Autority to verify the Apple remote peer
            $push->setRootCertificationAuthority($this->apnsDir.'SSL/entrust_root_certification_authority.pem');
    
            // Connect to the Apple Push Notification Service
            $push->connect();
    
            // Instantiate a new Message with a single recipient
            $message = new ApnsPHP_Message($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(0);
    
            // Set a simple welcome text
            $message->setText($msg);
    
            // Play the default sound
            $message->setSound();
    
            // Set custom properties
            if( is_array($attrs) && count($attrs) )
            {
                foreach( $attrs as $attr_key => $attr_val )
                {
                    $message->setCustomProperty($attr_key, $attr_val);
                }
            }
    
            // Set the expiry value - in seconds
            $message->setExpiry(120);
    
            // Add the message to the message queue
            $push->add($message);
    
            // Send all messages in the message queue
            $push->send();
    
            // Disconnect from the Apple Push Notification Service
            $push->disconnect();
    
            // Examine the error message container
            // $aErrorQueue = $push->getErrors();
            // if (!empty($aErrorQueue)) {
            //  var_dump($aErrorQueue);
            // }
    
            return TRUE;
    
        } /* /send_ios() */
    
        // -----------------------------------------------
    
        private function _apns_req() {
    
            require_once $this->apnsDir.'Abstract.php';
            require_once $this->apnsDir.'Exception.php';
            require_once $this->apnsDir.'Feedback.php';
            require_once $this->apnsDir.'Message.php';
            require_once $this->apnsDir.'Log/Interface.php';
            require_once $this->apnsDir.'Log/Embedded.php';
            require_once $this->apnsDir.'Message/Custom.php';
            require_once $this->apnsDir.'Message/Exception.php';
            require_once $this->apnsDir.'Push.php';
            require_once $this->apnsDir.'Push/Exception.php';
            require_once $this->apnsDir.'Push/Server.php';
            require_once $this->apnsDir.'Push/Server/Exception.php';
    
            return;
    
        } /* /_apns_req() */
    
    } /* /Notification_model{} */
    
    /* End of file notification_model.php */
    /* Location: ./application/models/notification_model.php */
    

    Example usage:

    $this->load->model('notification_model');
    $this->notification_model->send_ios($token, 'Test Message', array('custom_var' => 'val'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建