1

我正在尝试在我的应用程序中实现推送通知,当我将 devicetoken 静态放在 php 文件中时,它在我的设备上运行良好。

现在,我想将安装应用程序的所有设备插入到我的数据库中的表apns_devicesclass_APNS.php他们在课堂上放了private $certificate = our certificate另一个:private $sandboxCerficate = 'another certificate'实际上我不知道应该放哪些证书,我有不同扩展名的证书(.cer, .p12, .pem),我很困惑。

当我在设备上运行应用程序以获取所需信息时,它会提供:

Register URL: http://......./apns.php?task=register&appname=myfirstapp&appversion=1.0&devicetoken=2ca0c25ed7acea73e19c9d9193e57a12c1817ed48ddf8f44baea42e68a51563c&devicemodel=iPad&deviceversion=6.1.3&pushbadge=enabled&pushalert=enabled&pushsound=enabled

请有人建议我在这门课上放什么,因为数据库中插入设备的查询不起作用。

apns.php 是:

<?PHP

// AUTOLOAD CLASS OBJECTS... YOU CAN USE INCLUDES IF YOU PREFER
if(!function_exists("__autoload")){
    function __autoload($class_name){
        require_once('class_'.$class_name.'.php');
    }
}

// CREATE DATABASE OBJECT ( MAKE SURE TO CHANGE LOGIN INFO IN CLASS FILE )
$db = new DbConnect();
$db->show_errors();

// FETCH $_GET OR CRON ARGUMENTS TO AUTOMATE TASKS
$args = (!empty($_GET)) ? $_GET:array('task'=>$argv[1]);

// CREATE APNS OBJECT, WITH DATABASE OBJECT AND ARGUMENTS
$apns = new APNS($db, $args);
?>

class_APNS.php 是:

<?PHP
class APNS {
    private $db;
    private $apnsData;
    private $showErrors = true;
    private $logErrors = true;
    private $logPath = 'apns.log';
    private $logMaxSize = 1048576; // max log size before it is truncated
    // private $certificate = 'CHANGE THIS';
    private $ssl = 'ssl://gateway.push.apple.com:2195';
    private $feedback = 'ssl://feedback.push.apple.com:2196';
    private $sandboxCertificate = 'apns-dev.pem';
    private $sandboxSsl = 'ssl://gateway.sandbox.push.apple.com:2195';
    private $sandboxFeedback = 'ssl://feedback.sandbox.push.apple.com:2196';
    private $message;

    function __construct($db, $args=NULL) {
        $this->db = $db;
        $this->checkSetup();
        $this->apnsData = array(
            'production'=>array(
                'certificate'=>$this->certificate,
                'ssl'=>$this->ssl,
                'feedback'=>$this->feedback
            ),
            'sandbox'=>array(
                'certificate'=>$this->sandboxCertificate,
                'ssl'=>$this->sandboxSsl,
                'feedback'=>$this->sandboxFeedback
            )
        );
        if(!empty($args)){
            switch($args['task']){
                case "register":
                    $this->_registerDevice(
                        $args['appname'],
                        $args['appversion'],
                        $args['deviceuid'],
                        $args['devicetoken'],
                        $args['devicename'],
                        $args['devicemodel'],
                        $args['deviceversion'],
                        $args['pushbadge'],
                        $args['pushalert'],
                        $args['pushsound']
                    );
                    break;

                case "fetch";
                    $this->_fetchMessages();
                    break;

                default:
                    echo "No APNS Task Provided...\n";
                    break;
            }
        }
    }

    private function checkSetup(){
        if(!file_exists($this->certificate)) $this->_triggerError('ERROR: Missing Production Certificate.', E_USER_ERROR);
        if(!file_exists($this->sandboxCertificate)) $this->_triggerError('ERROR: Missing Sandbox Certificate.', E_USER_ERROR);

        clearstatcache();
        $certificateMod = substr(sprintf('%o', fileperms($this->certificate)), -3);
        $sandboxCertificateMod = substr(sprintf('%o', fileperms($this->sandboxCertificate)), -3); 

        if($certificateMod>644)  $this->_triggerError('NOTICE: Production Certificate is insecure! Suggest chmod 644.');
        if($sandboxCertificateMod>644)  $this->_triggerError('NOTICE: Sandbox Certificate is insecure! Suggest chmod 644.');
    }

    private function _registerDevice($appname, $appversion, $deviceuid, $devicetoken, $devicename, $devicemodel, $deviceversion, $pushbadge, $pushalert, $pushsound){

        if(strlen($appname)==0) $this->_triggerError('ERROR: Application Name must not be blank.', E_USER_ERROR);
        else if(strlen($appversion)==0) $this->_triggerError('ERROR: Application Version must not be blank.', E_USER_ERROR);
        else if(strlen($deviceuid)!=40) $this->_triggerError('ERROR: Device ID must be 40 characters in length.', E_USER_ERROR);
        else if(strlen($devicetoken)!=64) $this->_triggerError('ERROR: Device Token must be 64 characters in length.', E_USER_ERROR);
        else if(strlen($devicename)==0) $this->_triggerError('ERROR: Device Name must not be blank.', E_USER_ERROR);
        else if(strlen($devicemodel)==0) $this->_triggerError('ERROR: Device Model must not be blank.', E_USER_ERROR);
        else if(strlen($deviceversion)==0) $this->_triggerError('ERROR: Device Version must not be blank.', E_USER_ERROR);
        else if($pushbadge!='disabled' && $pushbadge!='enabled') $this->_triggerError('ERROR: Push Badge must be either Enabled or Disabled.', E_USER_ERROR);
        else if($pushalert!='disabled' && $pushalert!='enabled') $this->_triggerError('ERROR: Push Alert must be either Enabled or Disabled.', E_USER_ERROR);
        else if($pushsound!='disabled' && $pushsound!='enabled') $this->_triggerError('ERROR: Push Sount must be either Enabled or Disabled.', E_USER_ERROR);

        $appname = $this->db->prepare($appname);
        $appversion = $this->db->prepare($appversion);
        $deviceuid = $this->db->prepare($deviceuid);
        $devicetoken = $this->db->prepare($devicetoken);
        $devicename = $this->db->prepare($devicename);
        $devicemodel = $this->db->prepare($devicemodel);
        $deviceversion = $this->db->prepare($deviceversion);
        $pushbadge = $this->db->prepare($pushbadge);
        $pushalert = $this->db->prepare($pushalert);
        $pushsound = $this->db->prepare($pushsound);

        // store device for push notifications
        $this->db->query("SET NAMES 'utf8';"); // force utf8 encoding if not your default
        $sql = "INSERT INTO `apns_devices`
                VALUES (
                    NULL,
                    '{$appname}',
                    '{$appversion}',
                    '{$deviceuid}',
                    '{$devicetoken}',
                    '{$devicename}',
                    '{$devicemodel}',
                    '{$deviceversion}',
                    '{$pushbadge}',
                    '{$pushalert}',
                    '{$pushsound}',
                    'production',
                    'active',
                    NOW(),
                    NOW()
                )
                ON DUPLICATE KEY UPDATE
                `devicetoken`='{$devicetoken}',
                `devicename`='{$devicename}',
                `devicemodel`='{$devicemodel}',
                `deviceversion`='{$deviceversion}',
                `pushbadge`='{$pushbadge}',
                `pushalert`='{$pushalert}',
                `pushsound`='{$pushsound}',
                `status`='active',
                `modified`=NOW();";
        $this->db->query($sql);
    }
private function _unregisterDevice($token){
        $sql = "UPDATE `apns_devices`
                SET `status`='uninstalled'
                WHERE `devicetoken`='{$token}'
                LIMIT 1;";
        $this->db->query($sql);
    }
}
?>
4

0 回答 0