pushbots/pushbots-php

PushBots PHP SDK

1.2.0 2018-10-06 22:15 UTC

This package is auto-updated.

Last update: 2024-09-14 02:56:13 UTC


README

Build Status Maintainability

pushbots-php

PushBots的官方PHP包

安装

需要PHP 5.5。

使用Composer安装

$ php composer.phar require pushbots/pushbots-php

然后在您的PHP代码中要求库

require "vendor/autoload.php";

示例

<?php
// load dependencies
require 'vendor/autoload.php';

use Pushbots\PushbotsClient;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Psr7;

$client = new PushbotsClient("APPLICATION_ID", "APPLICATION_SECRET");

try {
	//Sample sending campaign to all users
	$res = $client->campaign->send([
		//Platforms
		//0 => iOS
		//1 => Android
		//2 => Chrome
		//3 => Firefox
		//4 => Opera
		//5=> Safari
		"platform" => [0,1,2,3,4,5], 
		//Message
		"msg" => "Notification message",
		//Badge [iOS only]
		"badge"	=> "+1",
		//Notification payload
		"payload"=>[
			"key"=> "value"
		]
	]);
} catch (ClientException $e) {
    echo Psr7\str($e->getRequest());
    echo Psr7\str($e->getResponse());
} catch (ServerException $e) {
    echo Psr7\str($e->getRequest());
    echo Psr7\str($e->getResponse());
}

别名

//Sample sending campaign to an alias
$client->campaign->alias("ALIAS", "Notification message");

测试通知

//Sample sending campaign to an alias
$client->campaign->test();

异常

Guzzle 在传输过程中出现错误时抛出异常。

发送到单个设备 [事务性]

use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Psr7;
......
try {
	$client->transactional->send([
		//topic [Required] of the transactional notification [can be any value, used only for tracking]
		"topic" => "welcome_campaign",
		//Platform [Required]
		//0 => iOS
		//1 => Android
		//2 => Chrome
		//3 => Firefox
		//4 => Opera
		//5=> Safari
		"platform" => 0,
		//recipients [Required]
		"recipients"=> [
			"tokens"=> ["343aa292e2bb642db2abb24124417cdf945a03e18c9434499d0dcef8b0d7dd0f"]
			//==OR== "ids"=>["ID"]
		],
		//Message [Required]
		"message" => [
			"title"=>"title", //[Optional]
			"body" => "Notification message",
			"payload"	=> ["key"=>"value"] //[Optional]
		]
	]);
} catch (ClientException $e) {
    echo Psr7\str($e->getRequest());
    echo Psr7\str($e->getResponse());
} catch (ServerException $e) {
    echo Psr7\str($e->getRequest());
    echo Psr7\str($e->getResponse());
}

变更日志

版本 1.2.1

  • 添加V3版本的广告支持。
  • 错误修复。

版本 1.2.0

  • 错误修复。
  • 添加v3事务性API支持。
  • 在示例代码中捕获GuzzleExceptions。

版本 1.1.0

  • 添加测试通知。
  • 添加Push/one支持。
  • 添加别名支持。

版本 1.0.0

  • 发布PushBots PHP包