tomloprod/ionic-push-php

ionic-push-php 是一个库,允许您消费 Ionic Cloud API 以发送推送通知(普通和计划通知),获取发送推送通知的分页列表,获取已注册设备的详细信息,通过令牌删除已注册设备等。

1.5.4 2017-07-17 13:29 UTC

README

ionic-push-php 是一个库,允许您使用 Ionic Cloud API发送推送通知(普通和计划通知),获取发送推送通知的 分页列表,获取 已注册设备的详细信息,通过令牌 删除已注册设备,等等。

Ionic 官方文档:Ionic HTTP API - Push

需求

  • PHP 5.1+
  • cURL

安装

composer require tomloprod/ionic-push-php

配置

首先,请确保您已设置 $ionicAPIToken$ionicProfile

  • (字符串) $ionicAPIToken: 您必须在 仪表板设置 › API 密钥 中创建的 API 令牌。
  • (字符串) $ionicProfile: 您可以在 仪表板设置 › 证书 中找到的安全配置文件标签。

更多信息请访问 这里

如果您不知道如何配置您的 Ionic 应用,可以参考这里:设置 Ionic Push

异常

此库可能会抛出

  • RequestException
echo $e;
echo $e->prettify();
echo $e->getCode();
echo $e->getMessage();
echo $e->getType();
echo $e->getLink();

使用方法

首先,按照以下方式实例化一个对象

use Tomloprod\IonicApi\Push,
    Tomloprod\IonicApi\Exception\RequestException;

$ionicPushApi = new Push($ionicProfile, $ionicAPIToken);

然后,您可以与 设备令牌消息通知 进行交互(列表、删除、创建 等)。

请注意,所有交互都返回一个 ApiResponse 对象实例,除了 notifications->deleteAll,它返回一个 ApiResponse 数组。

[设备令牌]

1) 列出令牌

try {

  $response = $ionicPushApi->deviceTokens->paginatedList([
      // Determines whether to include invalidated tokens (boolean)
      'show_invalid' => 1,
      // Only display tokens associated with the User ID (string)
      'user_id' => $desiredUserId,
      // Sets the number of items to return per page (integer)
      'page_size' => 4,
      // Sets the page number (integer)
      'page' => 1
  ]);

  foreach($response->data as $deviceToken){        
      print_r($deviceToken);
  }

} catch(RequestException $e) {
  echo $e;
}

2) 列出与设备令牌关联的用户

try {

  $response = $ionicPushApi->deviceTokens->listAssociatedUsers($desiredDeviceToken, [
      // Sets the number of items to return per page (integer)
      'page_size' => 1,
      // Sets the page number (integer)
      'page' => 1,
  ]);

  // Do what you want with $response->data

} catch(RequestException $e) {
  echo $e;
}

3) 将用户与设备令牌关联

try {

  $deviceToken = "c686...";
  $userId = "a99ee...";
  $ionicPushApi->deviceTokens->associateUser($deviceToken, $userId);

  // The user has been associated.

} catch(RequestException $e) {
  echo $e;
}

4) 断开用户与设备令牌的关联

try {

  $deviceToken = "c686...";
  $userId = "a99ee...";
  $ionicPushApi->deviceTokens->dissociateUser($deviceToken, $userId);

  // The user has been dissociated.

} catch(RequestException $e) {
  echo $e;
}

5) 创建由设备平台先前生成的设备令牌

try {

  $response = $ionicPushApi->deviceTokens->create([
      // Device token (string)
      'token' => $newToken,
      // User ID. Associate the token with the User (string)
      'user_id' => $uuid
  ]);

  // Do what you want with $response->data

} catch(RequestException $e) {
  echo $e;
}

6) 获取与设备令牌相关的设备信息

try {

  $response = $ionicPushApi->deviceTokens->retrieve($desiredDeviceToken);

  // Do what you want with $response->data

} catch(RequestException $e) {
  echo $e;
}

5) 更新特定令牌

try {

  $isValid = true; // Determines whether the device token is valid (boolean)
  $ionicPushApi->deviceTokens->update($desiredDeviceToken, ['valid' => $isValid]);

  // The device token has been updated.

} catch(RequestException $e) {
  echo $e;
}

6) 删除与设备令牌相关的设备

try {

  $ionicPushApi->deviceTokens->delete($desiredDeviceToken);

  // The device token has been deleted.

} catch(RequestException $e) {
  echo $e;
}

[消息]

1) 获取特定消息

try {
  $response = $ionicPushApi->messages->retrieve($desiredMessageId);

  // Do what you want with $response->data

} catch(RequestException $e) {
  echo $e;
}

2) 删除消息

try {
  $ionicPushApi->messages->delete($desiredMessageId);

  // The message has been deleted.

} catch(RequestException $e) {
  echo $e;
}

[通知]

1) 列出通知

try {

  $response = $ionicPushApi->notifications->paginatedList([
      // Sets the number of items to return per page (integer)
      'page_size' => 1,
      // Sets the page number (integer)
      'page' => 1,
      // You can also pass other fields like "message_total" or "overview" (string[])
      'fields' => [
          // Total number of messages tied to each notification.
          'message_total',
          // Get an overview of messages delivered and failed for each notification.
          'overview'
      ]
  ]);

  // Do what you want with $response->data

} catch(RequestException $e) {
  echo $e;
}

2) 获取特定通知

try {

  $response = $ionicPushApi->notifications->retrieve($desiredNotificationId);

  // Do what you want with $response->data

} catch(RequestException $e) {
  echo $e;
}

3) 删除通知

try {
  $ionicPushApi->notifications->delete($desiredNotificationId);

  // Notification has been deleted.

} catch(RequestException $e) {
  echo $e;
}

4) 删除所有通知

try {
?
  $responses = $ionicPushApi->notifications->deleteAll();

  // Notifications have been deleted.

} catch(RequestException $e) {
  echo $e;
}

5) 列出通知的消息

try {

  $response = $ionicPushApi->notifications->listMessages($desiredNotificationId, [
      // Sets the number of items to return per page (integer)
      'page_size' => 1,
      // Sets the page number (integer)
      'page' => 1
  ])

  // Do what you want with $response->data

} catch(RequestException $e) {
  echo $e;
}

6) 发送通知

/**
* ANDROID [OPTIONAL] CONFIG PARAMETERS
*/

// Filename of the Icon to display with the notification (string)
$icon = "icon";

// Filename or URI of an image file to display with the notification (string)
$image = "image";

// Indicates whether each notification message results in a new entry on the notification center on Android.
// If not set, each request creates a new notification.
// If set, and a notification with the same tag is already being shown, the new notification replaces the existing one in notification center.
$tag = "yourTagIfYouNeedIt";

// When this parameter is set to true, it indicates that the message should not be sent until the device becomes active. (boolean)
$delayWhileIdle = false;

// Identifies a group of messages that can be collapsed, so that only the last message gets sent when delivery can be resumed. (string)
$collapseKey = "group1";


/**
* IOS [OPTIONAL] CONFIG PARAMETERS
*/

// Message Priority. A value of 10 will cause APNS to attempt immediate delivery.
// A value of 5 will attempt a delivery which is convenient for battery life. (integer)
$priority = 10;

// The number to display as the badge of the app icon (integer)
$badge = 1;

// Alert Title, only applicable for iWatch devices
$iWatchTitle = "Hi!";


// Assign the previously defined configuration parameters to each platform, as well as the title and message:
$notificationConfig = [
    'title' => 'Your notification title',
    'message' => 'Your notification message. Bla, bla, bla, bla.',
    'android' => [
        'tag' => $tag,
        'icon' => $icon,
        'image' => $image,
        'delay_while_idle' => $delayWhileIdle,
        'collapse_key' => $collapseKey
    ],
    'ios' => [
        'priority' => $priority,
        'badge' => $badge,
        'title' => $iWatchTitle
    ]
];

// [OPTIONAL] You can also pass custom data to the notification. Default => []
$notificationPayload = [
    'myCustomField' => 'This is the content of my customField',
    'anotherCustomField' => 'More custom content'
];

// [OPTIONAL] And define, if you need it, a silent notification. Default => false
$silent = true;

// [OPTIONAL] Or/and even a scheduled notification for an indicated datetime. Default => ''
$scheduled = '2016-12-10 10:30:10';

// [OPTIONAL] Filename of audio file to play when a notification is received. Setting this to default will use the default device notification sound. Default => 'default'
$sound = 'default';

// Configure notification:
$ionicPushApi->notifications->setConfig($notificationConfig, $notificationPayload, $silent, $scheduled, $sound);

try {

  // Send notification...
  $response = $ionicPushApi->notifications->sendNotificationToAll(); // ...to all registered devices
  // or
  $response = $ionicPushApi->notifications->sendNotification([$desiredToken1, $desiredToken2, $desiredToken3]); // ...to some devices

  // Do what you want with $response->data

} catch(RequestException $e) {
  echo $e;
}

7) 用新配置替换现有通知

// Identifier of the notification we want to replace.
$notificationToReplace = "a86feewx...";

/**
* ANDROID [OPTIONAL] CONFIG PARAMETERS
*/

// Filename of the Icon to display with the new notification (string)
$icon = "icon";

// Filename or URI of an image file to display with the new notification (string)
$image = "image";

// Indicates whether each notification message results in a new entry on the notification center on Android.
// If not set, each request creates a new notification.
// If set, and a notification with the same tag is already being shown, the new notification replaces the existing one in notification center.
$tag = "yourTagIfYouNeedIt";

// When this parameter is set to true, it indicates that the message should not be sent until the device becomes active. (boolean)
$delayWhileIdle = false;

// Identifies a group of messages that can be collapsed, so that only the last message gets sent when delivery can be resumed. (string)
$collapseKey = "group1";


/**
* IOS [OPTIONAL] CONFIG PARAMETERS
*/

// Message Priority. A value of 10 will cause APNS to attempt immediate delivery.
// A value of 5 will attempt a delivery which is convenient for battery life. (integer)
$priority = 10;

// The number to display as the badge of the app icon (integer)
$badge = 1;

// Alert Title, only applicable for iWatch devices
$iWatchTitle = "Hi!";


// Assign the previously defined configuration parameters to each platform, as well as the title and message:
$notificationConfig = [
    'title' => 'Your notification title',
    'message' => 'Your notification message. Bla, bla, bla, bla.',
    'android' => [
        'tag' => $tag,
        'icon' => $icon,
        'image' => $image,
        'delay_while_idle' => $delayWhileIdle,
        'collapse_key' => $collapseKey
    ],
    'ios' => [
        'priority' => $priority,
        'badge' => $badge,
        'title' => $iWatchTitle
    ]
];

// [OPTIONAL] You can also pass custom data to the new notification. Default => []
$notificationPayload = [
    'myCustomField' => 'This is the content of my customField',
    'anotherCustomField' => 'More custom content'
];

// [OPTIONAL] And define, if you need it, a silent notification. Default => false
$silent = true;

// [OPTIONAL] Or/and even a scheduled notification for an indicated datetime. Default => ''
$scheduled = '2016-12-10 10:30:10';

// [OPTIONAL] Filename of audio file to play when a notification is received. Setting this to default will use the default device notification sound. Default => 'default'
$sound = 'default';

// Configure new notification:
$ionicPushApi->notifications->setConfig($notificationConfig, $notificationPayload, $silent, $scheduled, $sound);

try {

  // Replace notification with new configuration
  $response = $ionicPushApi->notifications->replace($notificationToReplace);

  // Do what you want with $response->data

} catch(RequestException $e) {
  echo $e;
}

贡献

  1. 分支它
  2. 创建您的功能分支(git checkout -b my-new-feature)
  3. 提交您的更改(git commit -m '添加一些功能')
  4. 推送到分支(git push origin my-new-feature)
  5. 创建新的 Pull Request