|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SemyonChetvertnyh\ApnNotificationChannel; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Notifications\ChannelManager; |
|
6
|
|
|
use Illuminate\Support\ServiceProvider; |
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Pushok\AuthProvider\Certificate; |
|
9
|
|
|
use Pushok\AuthProvider\Token; |
|
10
|
|
|
use Pushok\Client; |
|
11
|
|
|
|
|
12
|
|
|
class ApnServiceProvider extends ServiceProvider |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Bootstrap the application services. |
|
16
|
|
|
*/ |
|
17
|
|
|
public function boot() |
|
18
|
|
|
{ |
|
19
|
|
|
$this->app->bind(Client::class, function ($app) { |
|
20
|
|
|
$config = $app['config']->get('broadcasting.connections.apn'); |
|
21
|
|
|
|
|
22
|
|
|
return new Client( |
|
23
|
|
|
$this->authProvider($config), $config['is_production'] |
|
24
|
|
|
); |
|
25
|
|
|
}); |
|
26
|
|
|
|
|
27
|
|
|
$this->app->make(ChannelManager::class)->extend('apn', function () { |
|
28
|
|
|
return new ApnChannel( |
|
29
|
|
|
$this->app->make(Client::class) |
|
30
|
|
|
); |
|
31
|
|
|
}); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Determine and get the auth provider. |
|
36
|
|
|
* |
|
37
|
|
|
* @param array $config |
|
38
|
|
|
* @return \Pushok\AuthProviderInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function authProvider($config) |
|
41
|
|
|
{ |
|
42
|
|
|
$config['driver'] = $config['driver'] ?? 'jwt'; |
|
43
|
|
|
|
|
44
|
|
|
if ($config['driver'] === 'jwt') { |
|
45
|
|
|
return Token::create([ |
|
46
|
|
|
'key_id' => $config['key_id'], |
|
47
|
|
|
'team_id' => $config['team_id'], |
|
48
|
|
|
'app_bundle_id' => $config['app_bundle_id'], |
|
49
|
|
|
'private_key_path' => $config['private_key_path'], |
|
50
|
|
|
'private_key_secret' => $config['private_key_secret'], |
|
51
|
|
|
]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if ($config['driver'] === 'certificate') { |
|
55
|
|
|
return Certificate::create([ |
|
56
|
|
|
'certificate_path' => $config['certificate_path'], |
|
57
|
|
|
'certificate_secret' => $config['certificate_secret'], |
|
58
|
|
|
]); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported."); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|