Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 859f60d

Browse files
committed
Merge branch 'develop'
2 parents bf3d98d + 7acfe6f commit 859f60d

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class AccountActivated extends Notification
7171
return FcmMessage::create()
7272
->setPriority(FcmMessage::PRIORITY_HIGH)
7373
->setTimeToLive(86400)
74+
->setFcmKey('xxxx') // (Optional) Use this to override the FCM key from broadcasting.php
7475
->setNotification($fcmNotification);
7576
}
7677
}
@@ -81,11 +82,17 @@ class AccountActivated extends Notification
8182
The `Message` object can differ between different operating systems (Android, iOS, and Chrome). In this perspective, a `Message` object is available for each
8283
platform which extends the `FcmMessage` object.
8384

84-
All the methods below are explained and defined in the Firebase Cloud Messaging documentation found here:
85+
Mostly all the methods below are explained and defined in the Firebase Cloud Messaging documentation found here:
8586
[https://firebase.google.com/docs/cloud-messaging/http-server-ref](https://firebase.google.com/docs/cloud-messaging/http-server-ref)
8687

8788
#### FcmMessage
8889

90+
```php
91+
setFcmKey(string $fcmKey)
92+
```
93+
94+
This method can be used to override the default FCM key defined in the `config/broadcasting.php` file.
95+
8996
```php
9097
setCondition(string $condition)
9198
```

src/FcmChannel.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public function send($notifiable, Notification $notification)
4343
}
4444

4545
// Get the message from the notification class
46-
$message = $notification->toFcm($notifiable);
46+
$fcmMessage = $notification->toFcm($notifiable);
4747

48-
if (empty($message)) {
48+
if (empty($fcmMessage)) {
4949
return;
5050
}
5151

@@ -67,10 +67,32 @@ protected function sendToFcm($message)
6767
{
6868
try {
6969
$this->client->request('POST', '/fcm/send', [
70-
'body' => $message->toJson(),
70+
'headers' => $this->getClientHeaders($fcmMessage),
71+
'body' => $fcmMessage->toJson(),
7172
]);
7273
} catch (RequestException $requestException) {
7374
throw CouldNotSendNotification::serviceRespondedWithAnError($requestException);
7475
}
7576
}
77+
78+
/**
79+
* This is used to get the headers for the FCM request. We can add customization to the headers here.
80+
*
81+
* @param FcmMessage $fcmMessage
82+
* @return array
83+
*/
84+
protected function getClientHeaders(FcmMessage $fcmMessage)
85+
{
86+
$headers = [
87+
'Authorization' => sprintf('key=%s', config('broadcasting.connections.fcm.key')),
88+
'Content-Type' => 'application/json',
89+
];
90+
91+
// Override the FCM key from the config
92+
if (! empty($fcmMessage->getFcmKey())) {
93+
$headers['Authorization'] = sprintf('key=%s', $fcmMessage->getFcmKey());
94+
}
95+
96+
return $headers;
97+
}
7698
}

src/FcmMessage.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ class FcmMessage
88

99
const PRIORITY_HIGH = 'high';
1010

11+
/**
12+
* @var string
13+
*/
14+
protected $fcmKey;
15+
1116
/**
1217
* @var string
1318
*/
@@ -68,6 +73,27 @@ public static function create()
6873
return new static();
6974
}
7075

76+
/**
77+
* @return string
78+
*/
79+
public function getFcmKey()
80+
{
81+
return $this->fcmKey;
82+
}
83+
84+
/**
85+
* This method can be used to override the default FCM key used in the config/broadcasting.php.
86+
*
87+
* @param string $fcmKey
88+
* @return $this
89+
*/
90+
public function setFcmKey($fcmKey)
91+
{
92+
$this->fcmKey = $fcmKey;
93+
94+
return $this;
95+
}
96+
7197
/**
7298
* @return string
7399
*/

src/FcmServiceProvider.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ public function boot()
1717
->give(function () {
1818
return new Client([
1919
'base_uri' => config('broadcasting.connections.fcm.url', FcmChannel::DEFAULT_API_URL),
20-
'headers' => [
21-
'Authorization' => sprintf('key=%s', config('broadcasting.connections.fcm.key')),
22-
'Content-Type' => 'application/json',
23-
],
2420
]);
2521
});
2622
}

0 commit comments

Comments
 (0)