PHP code example of zbox / unified-push

1. Go to this page and download the library: Download zbox/unified-push library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zbox / unified-push example snippets




use Zbox\UnifiedPush\NotificationService\ServiceClientFactory;
use Zbox\UnifiedPush\NotificationService\ServiceCredentialsFactory;
use Zbox\UnifiedPush\Utils\ClientCredentials\CredentialsMapper;

$credentialsFactory = 
    new ServiceCredentialsFactory(
        new CredentialsMapper()
    );

$credentials = ['certificate' => 'path', 'certificatePassPhrase' => 'pass'];
$credentialsFactory->addCredentialsForService('APNS', $credentials);

$clientFactory = new ServiceClientFactory($credentialsFactory);
$clientFactory->setDefaultConfigPath();



use Zbox\UnifiedPush\Dispatcher;
use Zbox\UnifiedPush\Notification\NotificationBuilder;
use Zbox\UnifiedPush\NotificationService\ResponseHandler;

$dispatcher =
    new Dispatcher(
        $clientFactory,
        new NotificationBuilder(),
        new ResponseHandler()
    );

$dispatcher->setDevelopmentMode(true);



use Zbox\UnifiedPush\Message\MessageCollection;
use Zbox\UnifiedPush\Message\Type\APNS as APNSMessage;
use Zbox\UnifiedPush\Message\Type\APNSAlert;
use Zbox\UnifiedPush\Message\Type\GCM as GCMMessage;

$message1 = new APNSMessage();
$message1
    ->setAlertDictionary(
        (new APNSAlert)
            ->setActionLocKey('btn')
            ->setLocKey('msg')
            ->setLocArgs([$args])
    )
	->setSound('alert')
	->getBadge('2');

$message1->addRecipient('deviceToken1');

$message2 = new GCMMessage();
$message2
	->setCollapseKey('key')
	->addRecipientIdentifiers(
       new \ArrayIterator([
			'deviceToken1', 
			'deviceToken2'
		])
	)
    ->setPayloadData([
		'keyA' => 'value1',
		'keyB' => 'value2',
    ]);
    
$messages = 
    new MessageCollection([
        $message1, 
        $message2
    ]);



$dispatcher
    ->dispatchAll($messages)
    ->loadFeedback();



$responseHandler = $dispatcher->getResponseHandler();
$responseHandler->handleResponseCollection();

$invalidRecipients  = $responseHandler->getInvalidRecipients();
$messageErrors      = $responseHandler->getMessageErrors();