|
1
|
|
|
<?php namespace App\Modules\V1\Notifications\Repositories; |
|
2
|
|
|
|
|
3
|
|
|
use App\Modules\V1\Core\AbstractRepositories\AbstractRepository; |
|
4
|
|
|
|
|
5
|
|
|
class PushNotificationDeviceRepository extends AbstractRepository |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Return the model full namespace. |
|
9
|
|
|
* |
|
10
|
|
|
* @return string |
|
11
|
|
|
*/ |
|
12
|
|
|
protected function getModel() |
|
13
|
|
|
{ |
|
14
|
|
|
return 'App\Modules\V1\Notifications\PushNotificationDevice'; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Fetch the given users devices and broadcast the given |
|
19
|
|
|
* message to all of them. |
|
20
|
|
|
* |
|
21
|
|
|
* @param array $users_ids |
|
22
|
|
|
* @param string $messageText |
|
23
|
|
|
* @param boolean $activeOnly |
|
24
|
|
|
* @return void |
|
25
|
|
|
*/ |
|
26
|
|
|
public function broadcast($users_ids, $messageText, $activeOnly = false) |
|
27
|
|
|
{ |
|
28
|
|
|
$devicesArray = []; |
|
29
|
|
|
$devices = $this->model->whereIn('user_id', $users_ids); |
|
30
|
|
|
$devices = $activeOnly ? $device->where('active', 1)->get() : $devices->get(); |
|
|
|
|
|
|
31
|
|
|
foreach ($devices as $device) |
|
32
|
|
|
{ |
|
33
|
|
|
$devicesArray[$device->device_type][] = \PushNotification::Device($device->device_token, array('badge' => 5)); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$message = $this->constructMessage($messageText); |
|
37
|
|
|
if (array_key_exists('ios', $devicesArray)) |
|
38
|
|
|
{ |
|
39
|
|
|
$iosDevices = \PushNotification::DeviceCollection($devicesArray['ios']); |
|
40
|
|
|
$this->push('ios', $iosDevices, $message); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if (array_key_exists('android', $devicesArray)) |
|
44
|
|
|
{ |
|
45
|
|
|
$androidDevices = \PushNotification::DeviceCollection($devicesArray['android']); |
|
46
|
|
|
$this->push('android', $androidDevices, $message); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Push the given message to the given devices. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $type |
|
55
|
|
|
* @param colletion $devices |
|
56
|
|
|
* @param string $message |
|
57
|
|
|
* @return void |
|
58
|
|
|
*/ |
|
59
|
|
|
public function push($type, $devices, $message) |
|
60
|
|
|
{ |
|
61
|
|
|
$collection = \PushNotification::app($type)->to($devices)->send($message); |
|
62
|
|
|
foreach ($collection->pushManager as $push) |
|
63
|
|
|
{ |
|
64
|
|
|
$response[] = $push->getAdapter()->getResponse(); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Construct the notification message. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $messageText |
|
72
|
|
|
* @param array $options |
|
73
|
|
|
* @return object |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function constructMessage($messageText, $options = []) |
|
76
|
|
|
{ |
|
77
|
|
|
return \PushNotification::Message($messageText, $options); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$xwould be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.