2
2
3
3
namespace NotificationChannels \Fcm ;
4
4
5
+ use Illuminate \Contracts \Container \BindingResolutionException ;
5
6
use Illuminate \Contracts \Events \Dispatcher ;
6
7
use Illuminate \Notifications \Events \NotificationFailed ;
7
8
use Illuminate \Notifications \Notification ;
8
9
use Kreait \Firebase \Exception \MessagingException ;
9
- use Kreait \Firebase \Messaging as MessagingClient ;
10
10
use Kreait \Firebase \Messaging \CloudMessage ;
11
11
use Kreait \Firebase \Messaging \Message ;
12
- use Kreait \Laravel \Firebase \Facades \FirebaseMessaging ;
13
12
use NotificationChannels \Fcm \Exceptions \CouldNotSendNotification ;
14
13
use Throwable ;
15
14
16
15
class FcmChannel
17
16
{
18
17
const MAX_TOKEN_PER_REQUEST = 500 ;
19
18
20
- /***
21
- * @var Dispatcher
19
+ /**
20
+ * @var \Illuminate\Contracts\Events\ Dispatcher
22
21
*/
23
22
protected $ events ;
24
23
25
24
/**
26
25
* FcmChannel constructor.
27
26
*
28
- * @param Dispatcher $dispatcher
27
+ * @param \Illuminate\Contracts\Events\ Dispatcher $dispatcher
29
28
*/
30
29
public function __construct (Dispatcher $ dispatcher )
31
30
{
32
31
$ this ->events = $ dispatcher ;
33
32
}
34
33
34
+ /**
35
+ * @var string|null
36
+ */
37
+ protected $ fcmProject = null ;
38
+
35
39
/**
36
40
* Send the given notification.
37
41
*
38
42
* @param mixed $notifiable
39
- * @param Notification $notification
43
+ * @param \Illuminate\Notifications\ Notification $notification
40
44
*
41
45
* @return array
42
- * @throws CouldNotSendNotification|\Kreait\Firebase\Exception\FirebaseException
46
+ * @throws \NotificationChannels\Fcm\Exceptions\CouldNotSendNotification
47
+ * @throws \Kreait\Firebase\Exception\FirebaseException
43
48
*/
44
49
public function send ($ notifiable , Notification $ notification )
45
50
{
@@ -56,6 +61,11 @@ public function send($notifiable, Notification $notification)
56
61
throw CouldNotSendNotification::invalidMessage ();
57
62
}
58
63
64
+ $ this ->fcmProject = null ;
65
+ if (method_exists ($ notification , 'fcmProject ' )) {
66
+ $ this ->fcmProject = $ notification ->fcmProject ($ notifiable , $ fcmMessage );
67
+ }
68
+
59
69
$ responses = [];
60
70
61
71
try {
@@ -77,10 +87,24 @@ public function send($notifiable, Notification $notification)
77
87
}
78
88
79
89
/**
80
- * @param Message $fcmMessage
90
+ * @return \Kreait\Firebase\Messaging
91
+ */
92
+ protected function messaging ()
93
+ {
94
+ try {
95
+ $ messaging = app ('firebase.manager ' )->project ($ this ->fcmProject )->messaging ();
96
+ } catch (BindingResolutionException $ e ) {
97
+ $ messaging = app ('firebase.messaging ' );
98
+ }
99
+
100
+ return $ messaging ;
101
+ }
102
+
103
+ /**
104
+ * @param \Kreait\Firebase\Messaging\Message $fcmMessage
81
105
* @param $token
82
106
* @return array
83
- * @throws MessagingException
107
+ * @throws \Kreait\Firebase\Exception\ MessagingException
84
108
* @throws \Kreait\Firebase\Exception\FirebaseException
85
109
*/
86
110
protected function sendToFcm (Message $ fcmMessage , $ token )
@@ -93,27 +117,27 @@ protected function sendToFcm(Message $fcmMessage, $token)
93
117
$ fcmMessage ->setToken ($ token );
94
118
}
95
119
96
- return FirebaseMessaging:: send ($ fcmMessage );
120
+ return $ this -> messaging ()-> send ($ fcmMessage );
97
121
}
98
122
99
123
/**
100
124
* @param $fcmMessage
101
125
* @param array $tokens
102
- * @return MessagingClient \MulticastSendReport
103
- * @throws MessagingException
126
+ * @return \Kreait\Firebase\Messaging \MulticastSendReport
127
+ * @throws \Kreait\Firebase\Exception\ MessagingException
104
128
* @throws \Kreait\Firebase\Exception\FirebaseException
105
129
*/
106
130
protected function sendToFcmMulticast ($ fcmMessage , array $ tokens )
107
131
{
108
- return FirebaseMessaging:: sendMulticast ($ fcmMessage , $ tokens );
132
+ return $ this -> messaging ()-> sendMulticast ($ fcmMessage , $ tokens );
109
133
}
110
134
111
135
/**
112
136
* Dispatch failed event.
113
137
*
114
- * @param $notifiable
115
- * @param Notification $notification
116
- * @param Throwable $exception
138
+ * @param mixed $notifiable
139
+ * @param \Illuminate\Notifications\ Notification $notification
140
+ * @param \ Throwable $exception
117
141
* @return array|null
118
142
*/
119
143
protected function failedNotification ($ notifiable , Notification $ notification , Throwable $ exception )
0 commit comments