EasyApns - 将设备注册到数据库 - 有时会失败

时间:2015-07-22 12:33:45

标签: php ios apple-push-notifications push

我在ios应用程序上的推送通知很挣扎。许多设备正在注册,但有些设备没有,我不知道为什么。我想这可能是因为特殊人物?!

以下是神奇发生的线条。

AppDelegte.m

NSString *urlString = [NSString stringWithFormat:@"/apns.php?task=%@&appname=%@&appversion=%@&deviceuid=%@&devicetoken=%@&devicename=%@&devicemodel=%@&deviceversion=%@&pushbadge=%@&pushalert=%@&pushsound=%@&country=%@", @"register", appName,appVersion, deviceUuid, deviceToken, deviceName, deviceModel, deviceSystemVersion, pushBadge, pushAlert, pushSound, ccCodePush];

apns.php

function __construct($db, $args=NULL, $certificate=NULL, $sandboxCertificate=NULL, $logPath=NULL) {

    if(!empty($certificate) && file_exists($certificate))
    {
        $this->certificate = $certificate;
    }

    if(!empty($sandboxCertificate) && file_exists($sandboxCertificate))
    {
        $this->sandboxCertificate = $sandboxCertificate;
    }

    $this->db = $db;
    if($args['task']!="register")
        $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 ($logPath !== null) {
        $this->logPath = $logPath;
    }
    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'],
                    $args['country'],
                    isset($args['clientid'])?$args['clientid']:null
                );
                break;

            case "fetch":
                if($args['appname'])
                    $this->_fetchMessagesApp($args['appname']);
                else
                    $this->_fetchMessages();
                break;

            case "flush":
                if($args['appname'])
                    $this->_flushMessagesApp($args['appname']);
                else
                    $this->_flushMessages();
                break;

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

这是第二部分

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

    if(strlen($appname)==0) $this->_triggerError('Application Name must not be blank.', E_USER_ERROR);
    else if(strlen($appversion)==0) $this->_triggerError('Application Version must not be blank.', E_USER_ERROR);
    else if(strlen($deviceuid)>40) $this->_triggerError('Device ID may not be more than 40 characters in length.', E_USER_ERROR);
    else if(strlen($devicetoken)!=64) $this->_triggerError('Device Token must be 64 characters in length.', E_USER_ERROR);
    else if(strlen($devicename)==0) $this->_triggerError('Device Name must not be blank.', E_USER_ERROR);
    else if(strlen($devicemodel)==0) $this->_triggerError('Device Model must not be blank.', E_USER_ERROR);
    else if(strlen($deviceversion)==0) $this->_triggerError('Device Version must not be blank.', E_USER_ERROR);
    else if($pushbadge!='disabled' && $pushbadge!='enabled') $this->_triggerError('Push Badge must be either Enabled or Disabled.', E_USER_ERROR);
    else if($pushalert!='disabled' && $pushalert!='enabled') $this->_triggerError('Push Alert must be either Enabled or Disabled.', E_USER_ERROR);
    else if($pushsound!='disabled' && $pushsound!='enabled') $this->_triggerError('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);
    $clientid = $this->db->prepare($clientid);
    $country = $this->db->prepare($country);

    // 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,
                '{$clientid}',
                '{$appname}',
                '{$appversion}',
                '{$deviceuid}',
                '{$devicetoken}',
                '{$devicename}',
                '{$devicemodel}',
                '{$deviceversion}',
                '{$pushbadge}',
                '{$pushalert}',
                '{$pushsound}',
                '{$country}',
                '{$this->DEVELOPMENT}',
                'active',
                NOW(),
                NOW()
            )
            ON DUPLICATE KEY UPDATE
            # If not using real UUID (iOS5+), uid may change on reinstall.
            `deviceuid`='{$deviceuid}',
            `devicetoken`='{$devicetoken}',
            `devicename`='{$devicename}',
            `devicemodel`='{$devicemodel}',
            `deviceversion`='{$deviceversion}',
            `appversion`='{$appversion}',
            `pushbadge`='{$pushbadge}',
            `pushalert`='{$pushalert}',
            `pushsound`='{$pushsound}',
            `country`='{$country}',
            `status`='active',
            `modified`=NOW();";
    $this->db->query($sql);
}

0 个答案:

没有答案