1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Canvas\Cli\Jobs; |
4
|
|
|
|
5
|
|
|
use Canvas\Contracts\Queue\QueueableJobInterface; |
6
|
|
|
use Canvas\Jobs\Job; |
7
|
|
|
use Phalcon\Di; |
8
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
|
|
|
9
|
|
|
use Http\Adapter\Guzzle6\Client as GuzzleAdapter; |
|
|
|
|
10
|
|
|
use Http\Client\Common\HttpMethodsClient as HttpClient; |
|
|
|
|
11
|
|
|
use Http\Message\MessageFactory\GuzzleMessageFactory; |
|
|
|
|
12
|
|
|
use OneSignal\Config; |
|
|
|
|
13
|
|
|
use OneSignal\OneSignal; |
|
|
|
|
14
|
|
|
use Canvas\Models\UserLinkedSources; |
15
|
|
|
use Canvas\Notifications\PushNotification; |
16
|
|
|
use Exception; |
17
|
|
|
|
18
|
|
|
class PushNotifications extends Job implements QueueableJobInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Realtime channel. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $users; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Realtime event. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $message; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Realtime params. |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $params; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor setup info for Pusher. |
43
|
|
|
* |
44
|
|
|
* @param string $channel |
45
|
|
|
* @param string $event |
46
|
|
|
* @param array $params |
47
|
|
|
*/ |
48
|
|
|
public function __construct(PushNotification $pushNotification) |
49
|
|
|
{ |
50
|
|
|
$this->users = $pushNotification->to; |
51
|
|
|
$this->message = $pushNotification->message; |
52
|
|
|
$this->params = $pushNotification->params; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Handle the pusher request. |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function handle() |
61
|
|
|
{ |
62
|
|
|
$config = Di::getDefault()->getConfig(); |
63
|
|
|
|
64
|
|
|
if (empty($this->users)) { |
65
|
|
|
return false; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$userDevicesArray = UserLinkedSources::getMobileUserLinkedSources($this->users->getId()); |
69
|
|
|
|
70
|
|
|
if (empty($userDevicesArray[2]) && empty($userDevicesArray[3])) { |
71
|
|
|
return false; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* One signal array params. |
76
|
|
|
*/ |
77
|
|
|
$pushBody = [ |
78
|
|
|
'contents' => [ |
79
|
|
|
'en' => $this->message |
80
|
|
|
], |
81
|
|
|
'data' => $this->params |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @todo change to use some constante , ID dont tell you what device it is |
86
|
|
|
*/ |
87
|
|
|
//send push android |
88
|
|
|
if (!empty($userDevicesArray[2])) { |
89
|
|
|
$pushBody['include_player_ids'][] = $userDevicesArray[2][0]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
//ios |
93
|
|
|
if (!empty($userDevicesArray[3])) { |
94
|
|
|
$pushBody['include_player_ids'][] = $userDevicesArray[3][0]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
try { |
98
|
|
|
$this->oneSignal( |
99
|
|
|
$config->pushNotifications->appId, |
100
|
|
|
$config->pushNotifications->authKey, |
101
|
|
|
$config->pushNotifications->userAuthKey |
102
|
|
|
)->notifications->add( |
103
|
|
|
$pushBody |
104
|
|
|
); |
105
|
|
|
} catch (Exception $e) { |
106
|
|
|
if (Di::getDefault()->has('log')) { |
107
|
|
|
Di::getDefault()->get('log')->error( |
108
|
|
|
'Error sending push notification via OneSignal - ' . $e->getMessage(), |
109
|
|
|
[$e->getTraceAsString()] |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return true; |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Give back a one signal object to send push notifications. |
119
|
|
|
* |
120
|
|
|
* @param string $appId |
121
|
|
|
* @param string $appAuthKey |
122
|
|
|
* @param string $appUserKey |
123
|
|
|
* @return OneSignal |
124
|
|
|
*/ |
125
|
|
|
private function oneSignal(string $appId, string $appAuthKey, string $appUserKey): OneSignal |
126
|
|
|
{ |
127
|
|
|
$config = new Config(); |
128
|
|
|
$config->setApplicationId($appId); |
129
|
|
|
$config->setApplicationAuthKey($appAuthKey); |
130
|
|
|
$config->setUserAuthKey($appUserKey); |
131
|
|
|
|
132
|
|
|
$guzzle = new GuzzleClient([ |
133
|
|
|
'timeout' => 2.0, |
134
|
|
|
]); |
135
|
|
|
|
136
|
|
|
$client = new HttpClient(new GuzzleAdapter($guzzle), new GuzzleMessageFactory()); |
137
|
|
|
|
138
|
|
|
return new OneSignal($config, $client); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths