码迷,mamicode.com
首页 > 移动开发 > 详细

利用ApnsPHP包向IOS推送消息

时间:2015-07-23 15:29:31      阅读:1310      评论:0      收藏:0      [点我收藏+]

标签:

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

利用ApnsPHP包向IOS推送消息

标签:

原文地址:http://www.cnblogs.com/adtuu/p/4670513.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!