Skip to content

Commit

Permalink
support hmos for pushapi
Browse files Browse the repository at this point in the history
  • Loading branch information
jg-json committed Jun 17, 2024
1 parent ebb191e commit d3a9697
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
22 changes: 22 additions & 0 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,28 @@ $push->iosNotification('hello', [
| intent | 表示扩展字段,接受一个数组,自定义 Key/value 信息以供业务使用 |
| extras | 表示扩展字段,接受一个数组,自定义 Key/value 信息以供业务使用 |

**hmos Notification**

```php
// hmosNotification($alert = '', $category = '', array $notification = array())
// 数组 $notification 的键支持 'title', 'category', 'large_icon', 'intent', 'badge_add_num', 'test_message', 'receipt_id', 'extras' 中的一个或多个
// 注意category为必填项,若$category为空字符串则会从$notification中取'category'的值
```

参数说明:

| 参数 | 说明 |
| --- | --- |
| alert | 表示通知内容,会覆盖上级统一指定的 alert 信息;默认内容可以为空字符串,表示不展示到通知栏 |
| title | 表示通知标题,会替换通知里原来展示 App 名称的地方 |
| category | 通知栏消息分类条目;对应官方「本地通知」category取值,开发者通过极光服务发起推送时如果传递了此字段值,请务必按照官方要求传递 |
| badge_add_num | 设置角标数字累加值;不填则不改变角标数字,取值范围为1-99 |
| test_message | 测试消息标识;false为正常消息(默认值),true为测试消息 |
| receipt_id | 华为回执 ID;输入一个唯一的回执 ID 指定本次下行消息的回执地址及配置,该回执 ID 可以在鸿蒙回执参数配置中查看 |
| large_icon | 表示通知栏大图标,图标路径可以是以 http 或 https 开头的网络图片,如:"http:jiguang.cn/logo.png",图标大小不超过 30k; 也可以是位于 drawable 资源文件夹的图标路径,如:"R.drawable.lg_icon";|
| intent | 表示扩展字段,接受一个数组,自定义 Key/value 信息以供业务使用 |
| extras | 表示扩展字段,接受一个数组,自定义 Key/value 信息以供业务使用 |

**WinPhone Notification**

```php
Expand Down
63 changes: 61 additions & 2 deletions src/JPush/PushPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class PushPayload {

private static $EFFECTIVE_DEVICE_TYPES = array('ios', 'android', 'winphone');
private static $EFFECTIVE_DEVICE_TYPES = array('ios', 'android', 'winphone', 'hmos');

private $client;
private $url;
Expand All @@ -25,6 +25,7 @@ class PushPayload {
private $iosNotification;
private $androidNotification;
private $winPhoneNotification;
private $hmosNotification;
private $voip;
private $smsMessage;
private $message;
Expand Down Expand Up @@ -52,7 +53,7 @@ public function setCid($cid) {
}

public function setPlatform($platform) {
# $required_keys = array('all', 'android', 'ios', 'winphone');
# $required_keys = array('all', 'android', 'ios', 'winphone', 'hmos');
if (is_string($platform)) {
$ptf = strtolower($platform);
if ('all' === $ptf) {
Expand Down Expand Up @@ -287,6 +288,20 @@ public function build() {
}
}

if (!is_null($this->hmosNotification)) {
$notification['hmos'] = $this->hmosNotification;
if (is_null($this->hmosNotification['alert'])) {
if (is_null($this->hmosNotification)) {
throw new InvalidArgumentException("hmos alert can not be null");
} else {
$notification['hmos']['alert'] = $this->notificationAlert;
}
}
if (is_null($this->hmosNotification['category'])) {
throw new InvalidArgumentException("hmos category can not be null");
}
}

if (!is_null($this->voip)) {
$notification['voip'] = $this->voip;
}
Expand Down Expand Up @@ -448,6 +463,50 @@ public function androidNotification($alert = '', array $notification = array())
return $this;
}

public function hmosNotification($alert = '', $category='', array $notification = array()) {
$hmos = array();
$hmos['alert'] = is_string($alert) ? $alert : '';
$hmos['category'] = is_string($category) ? $category : '';
if (!empty($notification)) {
if (isset($notification['badge_add_num'])) {
if (is_int($notification['badge_add_num']) && $notification['badge_add_num'] >= 1 && $notification['badge_add_num']<=99) {
$hmos['badge_add_num'] = $notification['badge_add_num'];
} else {
unset($notification['badge_add_num']);
}
}
if (isset($notification['test_message'])) {
if (is_bool($notification['test_message'])) {
$hmos['test_message'] = $notification['test_message'];
} else {
unset($notification['test_message']);
}
}
if (isset($notification['intent'])) {
if (is_array($notification['intent']) && !empty($notification['intent'])) {
$hmos['intent'] = $notification['intent'];
} else {
unset($notification['intent']);
}
}
if (isset($notification['extras'])) {
if (is_array($notification['extras']) && !empty($notification['extras'])) {
$hmos['extras'] = $notification['extras'];
} else {
unset($notification['extras']);
}
}
if ($hmos['category'] === '' && isset($notification['category'])) {
if (is_string($notification['category']) && $notification['category'] !== '') {
$hmos['category'] = $notification['category'];
}
}
$hmos = array_merge($notification, $hmos);
}
$this->hmosNotification = $hmos;
return $this;
}

/**
* Voip in notification
* could add any custom key/value into it
Expand Down

0 comments on commit d3a9697

Please sign in to comment.