gidix/pushnotifier-php

PHP的PushNotifier库

2.0 2018-01-01 23:02 UTC

This package is not auto-updated.

Last update: 2024-09-29 04:43:10 UTC


README

一个方便使用的库,用于在PHP项目中使用PushNotifier。

安装

将此内容添加到composer.json

"require": {
	"gidix/pushnotifier-php": "^2.0.0"
}

...或者运行composer require gidix/pushnotifier-php

使用方法

您只需使用一个类:GIDIX\PushNotifier\SDK\PushNotifier。所有内容都由此类派生。

创建您的应用程序

在您实际进行操作之前,您必须创建您的应用程序。它包括一个API令牌和一个包名。这两个都可以在pushnotifier.de/account/api中配置。

然后您可以创建您的实例

    $app = new GIDIX\PushNotifier\SDK\PushNotifier([
        'api_token'     =>  'YOUR_API_TOKEN',
        'package'       =>  'YOUR.PACKAGE.NAME'
    ]);

登录

当作为用户进行身份验证时,您必须登录,然后使用其AppToken进行所有进一步的通信

    $appToken = $app->login('username', 'password');

您可以通过将其转换为字符串来在任何地方存储$appToken(参见examples/storing-app-token.php)。

之后,您可以使用AppToken进行身份验证

    $app = new GIDIX\PushNotifier\SDK\PushNotifier([
        'api_token'     =>  'YOUR_API_TOKEN',
        'package'       =>  'YOUR.PACKAGE.NAME',
        'app_token'     =>  $appToken
    ]);

检索设备

    $devices = $app->getDevices();

将检索一个包含ID、标题、型号和设备图片链接的GIDIX\PushNotifier\SDK\Device对象的数组。设备ID在时间上不会改变,作为唯一的标识符。

发送文本

    $result = $app->sendMessage($devices, 'Some Content');

$devices必须是一个包含Device对象或设备ID字符串的数组,例如['abc', 'xyz']

发送URL

    $result = $app->sendURL($devices, 'https://some.example.org/with/path.html');

$devices必须是一个包含Device对象或设备ID字符串的数组,例如['abc', 'xyz']

发送通知

    $result = $app->sendNotification($devices, 'Some Content', 'https://some.example.org/with/path.html');

异常

  • DeviceNotFoundException:当找不到设备时
  • InvalidAPITokenException:当api_token或包无法验证时
  • InvalidAppTokenException:当AppToken无法验证或已过期时
  • InvalidCredentialsException:当登录凭证不正确时
  • InvalidRequestException:当某些请求数据格式不正确时,例如通知的格式不正确的URL
  • PushNotifierException:所有这些的基异常,仅在出现未知错误(500)时抛出

$devices必须是一个包含Device对象或设备ID字符串的数组,例如['abc', 'xyz']

示例

示例可以在本存储库的examples/中找到。