-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPushKit.php
184 lines (164 loc) · 7.5 KB
/
PushKit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
namespace HMS\PushKit;
use HMS\AccountKit\AccountKit;
use HMS\Core\Wrapper;
use HMS\PushKit\Android\AndroidConfig;
use HMS\PushKit\Android\AndroidNotification;
use JetBrains\PhpStorm\ArrayShape;
use stdClass;
/**
* Class HMS PushKit Wrapper
*
* @author Martin Zeitler
*/
class PushKit extends Wrapper {
private string $url_message_send;
private string $url_topics_list;
private string $url_topic_subscribe;
private string $url_topic_unsubscribe;
private string $url_token_data_query;
private string $url_token_data_delete;
/** Constructor. */
public function __construct( array|string $config ) {
parent::__construct( $config );
$this->post_init();
$this->url_message_send = str_replace('{appId}', $this->oauth2_client_id, Constants::PUSHKIT_MESSAGE_SEND);
$this->url_topics_list = str_replace('{appId}', $this->oauth2_client_id, Constants::PUSHKIT_TOPICS_LIST);
$this->url_topic_subscribe = str_replace('{appId}', $this->oauth2_client_id, Constants::PUSHKIT_TOPIC_SUBSCRIBE);
$this->url_topic_unsubscribe = str_replace('{appId}', $this->oauth2_client_id, Constants::PUSHKIT_TOPIC_UNSUBSCRIBE);
$this->url_token_data_query = str_replace('{appId}', $this->oauth2_client_id, Constants::PUSHKIT_TOKEN_DATA_QUERY);
$this->url_token_data_delete = str_replace('{appId}', $this->oauth2_client_id, Constants::PUSHKIT_TOKEN_DATA_DELETE);
/* Obtain an access-token. */
$account_kit = new AccountKit( $config );
$this->access_token = $account_kit->get_access_token();
}
/** Unset properties irrelevant to the child class. */
protected function post_init(): void {
unset($this->api_key, $this->api_signature);
}
/**
* Querying the Topic Subscription List.
*
* @link https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/topic-list-api-0000001050706152 Querying the Topic Subscription List
* @param string $token
* @return stdClass
*/
public function topics_list( string $token ): stdClass {
$payload = ['token' => $token];
return $this->request( 'POST', $this->url_topics_list, $this->auth_headers(), $payload);
}
/**
* Subscribing to a Topic.
*
* @link https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/topic-sub-api-0000001051066122 Subscribing to a Topic
* @param string $topic_name
* @param string|array $tokens
* @return stdClass
*/
public function topic_subscribe( string $topic_name, string|array $tokens ): stdClass {
if (is_string($tokens)) {$tokens = [ $tokens ];}
$payload = ['topic' => $topic_name, 'tokenArray' => $tokens];
return $this->request('POST', $this->url_topic_subscribe, $this->auth_headers(), $payload);
}
/**
* Unsubscribing from a Topic.
*
* @link https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/topic-unsub-api-0000001050746185 Unsubscribing from a Topic
* @param string $topic_name
* @param string|array $tokens
* @return stdClass
*/
public function topic_unsubscribe( string $topic_name, string|array $tokens ): stdClass {
if (is_string($tokens)) {$tokens = [ $tokens ];}
$payload = ['topic' => $topic_name, 'tokenArray' => $tokens];
return $this->request('POST', $this->url_topic_unsubscribe, $this->auth_headers(), $payload);
}
/**
* Sending Downstream Messages to Token.
*
* @link https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/https-send-api-0000001050986197 Sending Downstream Messages
* @param string|array $token
* @param string $title
* @param string $body
* @param string|null $image
* @return stdClass
*/
public function send_message_to_token( string|array $token, string $title, string $body, string|null $image=null ): stdClass {
if (is_string($token)) {$token = [ $token ];}
$payload = $this->get_payload_by_mode( 'token', $token, $title, $body, $image );
return $this->request('POST', $this->url_message_send, $this->auth_headers(), $payload);
}
/**
* Sending Downstream Messages to Topic.
*
* @link https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/https-send-api-0000001050986197 Sending Downstream Messages
* @param string $topic
* @param string $title
* @param string $body
* @param string|null $image
* @return stdClass
*/
public function send_message_to_topic( string $topic, string $title, string $body, string|null $image=null ): stdClass {
$payload = $this->get_payload_by_mode( 'topic', $topic, $title, $body, $image );
return $this->request('POST', $this->url_message_send, $this->auth_headers(), $payload);
}
/**
* Sending Downstream Messages to Condition.
*
* @link https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/https-send-api-0000001050986197 Sending Downstream Messages
* @param string $condition
* @param string $title
* @param string $body
* @param string|null $image
* @return stdClass
*/
public function send_message_to_condition( string $condition, string $title, string $body, string|null $image=null ): stdClass {
$payload = $this->get_payload_by_mode( 'condition', $condition, $title, $body, $image );
return $this->request('POST', $this->url_message_send, $this->auth_headers(), $payload);
}
#[ArrayShape(['validate_only' => "bool", 'message' => "object"])]
private function get_payload_by_mode( string $mode, string|array $argument, string $title, string $body, string|null $image=null ): array {
if (! in_array( $mode , ['token', 'topic', 'condition'] ) ) {return [];}
$notification = new Notification( $title, $body, $image );
$android = AndroidConfig::fromArray([
'notification' => AndroidNotification::fromArray([
'click_action' => [
'type' => 2,
'url' => 'https://syslogic.io'
]
])
]);
$message = CloudMessage::fromArray([
$mode => $argument, // one of: token, topic, condition.
'notification' => $notification->asObject(),
'android' => $android->asObject(),
'data' => ''
]);
return [
'validate_only' => false,
'message' => $message->asObject()
];
}
/**
* Querying Data as a Data Controller.
*
* @link https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/query-data-api-0000001051066126 Querying Data as a Data Controller
* @param string $token
* @return stdClass
*/
public function token_data_query( string $token ): stdClass {
$payload =['token' => $token];
return $this->request('POST', $this->url_token_data_query, $this->auth_headers(), $payload);
}
/**
* Deleting Data as a Data Controller.
*
* @link https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/del-data-api-0000001050746189 Deleting Data as a Data Controller
* @param string $token
* @return stdClass
*/
public function token_data_delete( string $token ): stdClass {
$payload = ['token' => $token];
return $this->request('POST', $this->url_token_data_delete, $this->auth_headers(), $payload);
}
}