利用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

你可能感兴趣的:(apns)