如何在Zend框架2中建立实时通知系统?

时间:2017-05-13 05:59:34

标签: php zend-framework

我想用在共享服务器上运行的PHP实现实时通知系统。它可以由socket.io或Firebase云消息传递(FCM)构建。我该如何实现呢?

1 个答案:

答案 0 :(得分:1)

您可以将ZendService GCM用于Zend 2。

示例:

创建您的班级经理推送

use Useful\Controller\ControlController;
use ZendService\Google\Gcm\Client;
use ZendService\Google\Gcm\Message;
use ZendService\Apple\Apns\Message\Alert;
use ZendService\Google\Exception\RuntimeException;

class PushNotificationController {
   const SERVER_KEY = "YOUR FIREBASE SERVER KEY";
}

将功能添加到发送

public function sendGoogleMessage($registatoin_ids, $data) {
    try {
        $key = SERVER_KEY ;
        $Google = new \ZendService\Google\Gcm\Client ();
        $Google->setApiKey ( $key );

        $Message = new \ZendService\Google\Gcm\Message ();          
        $Message->setData ( $data );

        // up to 100 registration ids can be sent to at once
        $Message->setRegistrationIds ( $registatoin_ids );
        $Message->setDelayWhileIdle ( false );
        $Message->setTimeToLive ( 600 );
    } catch ( \Exception $e ) {
        return false;
    }
    // Envio
    try {
        $Response = $Google->send ( $Message );
    } catch ( RuntimeException $e ) {
        echo $e->getMessage () . PHP_EOL;
        exit ( 1 );
    }

    return array (
            "Successful" => $Response->getSuccessCount (),
            "Failures" => $Response->getFailureCount (),
            "Canonicals" => $Response->getCanonicalCount () 
    );
}

ZendService\Google\Gcm — Zend Framework 2 2.4.9 documentation