|
1
|
|
|
<?php namespace App\Modules\PushNotificationDevices\Services; |
|
2
|
|
|
|
|
3
|
|
|
use App\Modules\Core\BaseClasses\BaseService; |
|
4
|
|
|
use LaravelFCM\Message\OptionsBuilder; |
|
5
|
|
|
use LaravelFCM\Message\PayloadDataBuilder; |
|
6
|
|
|
use LaravelFCM\Message\PayloadNotificationBuilder; |
|
7
|
|
|
use App\Modules\PushNotificationDevices\Repositories\PushNotificationDeviceRepository; |
|
8
|
|
|
|
|
9
|
|
|
class PushNotificationDeviceService extends BaseService |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Init new object. |
|
13
|
|
|
* |
|
14
|
|
|
* @param PushNotificationDeviceRepository $repo |
|
15
|
|
|
* @return void |
|
|
|
|
|
|
16
|
|
|
*/ |
|
17
|
|
|
public function __construct(PushNotificationDeviceRepository $repo) |
|
18
|
|
|
{ |
|
19
|
|
|
parent::__construct($repo); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Register the given device to the logged in user. |
|
24
|
|
|
* |
|
25
|
|
|
* @param array $data |
|
26
|
|
|
* @return void |
|
27
|
|
|
*/ |
|
28
|
|
|
public function registerDevice($data) |
|
29
|
|
|
{ |
|
30
|
|
|
$data['access_token'] = \Auth::user()->token(); |
|
31
|
|
|
$data['user_id'] = \Auth::id(); |
|
32
|
|
|
$device = $this->repo->first([ |
|
33
|
|
|
'and' => [ |
|
34
|
|
|
'device_token' => $data['device_token'], |
|
35
|
|
|
'user_id' => $data['user_id'] |
|
36
|
|
|
] |
|
37
|
|
|
]); |
|
38
|
|
|
|
|
39
|
|
|
if ($device) { |
|
40
|
|
|
$data['id'] = $device->id; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $this->repo->save($data); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Generate the given message data. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $title |
|
50
|
|
|
* @param string $message |
|
51
|
|
|
* @param array $data |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
|
|
public function generateMessageData($title, $message, $data = []) |
|
55
|
|
|
{ |
|
56
|
|
|
$optionBuilder = new OptionsBuilder(); |
|
57
|
|
|
$notificationBuilder = new PayloadNotificationBuilder($title); |
|
58
|
|
|
$dataBuilder = new PayloadDataBuilder(); |
|
59
|
|
|
|
|
60
|
|
|
$optionBuilder->setTimeToLive(60 * 20); |
|
61
|
|
|
$notificationBuilder->setBody($message); |
|
62
|
|
|
$dataBuilder->addData($data); |
|
63
|
|
|
|
|
64
|
|
|
$options = $optionBuilder->build(); |
|
65
|
|
|
$notification = $notificationBuilder->build(); |
|
66
|
|
|
$data = $dataBuilder->build(); |
|
67
|
|
|
|
|
68
|
|
|
return compact('options', 'notification', 'data'); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.