Skip to content

Commit 104d2a6

Browse files
authored
Merge pull request #97 from ayazshakoor/issue/send_notification_to_external_user
Send notification by external user ids
2 parents 5e880ba + 5e25505 commit 104d2a6

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,27 @@ Read https://documentation.onesignal.com/docs/web-push-tagging-guide for additio
137137
a `$url` parameter, users will be redirecting to that url.
138138

139139

140+
141+
### Sending a Notification To A Specific external User (custom user id added by user)
142+
143+
After storing a user's tokens in a table, you can simply send a message with
144+
145+
```php
146+
OneSignal::sendNotificationToExternalUser(
147+
"Some Message",
148+
$userId,
149+
$url = null,
150+
$data = null,
151+
$buttons = null,
152+
$schedule = null
153+
);
154+
```
155+
156+
`$userId` is the user's unique external id (custom id) added by the user where he/she is registered for notifications.
157+
Read https://documentation.onesignal.com/docs/web-push-tagging-guide for additional details.
158+
`$url` , `$data` , `$buttons` and `$schedule` fields are exceptional. If you provide
159+
a `$url` parameter, users will be redirecting to that url.
160+
140161
### Sending a Notification To Segment
141162

142163
You can simply send a notification to a specific segment with

src/OneSignalClient.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,57 @@ public function sendNotificationToUser($message, $userId, $url = null, $data = n
174174
$this->sendNotificationCustom($params);
175175
}
176176

177+
/**
178+
* @param $message
179+
* @param $userId
180+
* @param null $url
181+
* @param null $data
182+
* @param null $buttons
183+
* @param null $schedule
184+
* @param null $headings
185+
* @param null $subtitle
186+
*/
187+
public function sendNotificationToExternalUser($message, $userId, $url = null, $data = null, $buttons = null, $schedule = null, $headings = null, $subtitle = null) {
188+
$contents = array(
189+
"en" => $message
190+
);
191+
192+
$params = array(
193+
'app_id' => $this->appId,
194+
'contents' => $contents,
195+
'include_external_user_ids' => is_array($userId) ? $userId : array($userId)
196+
);
197+
198+
if (isset($url)) {
199+
$params['url'] = $url;
200+
}
201+
202+
if (isset($data)) {
203+
$params['data'] = $data;
204+
}
205+
206+
if (isset($buttons)) {
207+
$params['buttons'] = $buttons;
208+
}
209+
210+
if(isset($schedule)){
211+
$params['send_after'] = $schedule;
212+
}
213+
214+
if(isset($headings)){
215+
$params['headings'] = array(
216+
"en" => $headings
217+
);
218+
}
219+
220+
if(isset($subtitle)){
221+
$params['subtitle'] = array(
222+
"en" => $subtitle
223+
);
224+
}
225+
226+
$this->sendNotificationCustom($params);
227+
}
177228
public function sendNotificationUsingTags($message, $tags, $url = null, $data = null, $buttons = null, $schedule = null, $headings = null, $subtitle = null) {
178229
$contents = array(
179230
"en" => $message

0 commit comments

Comments
 (0)