Skip to content

Commit 5c185f7

Browse files
authored
feat: add contentType option (#433)
* Added contentType option to Notification * Added contentType option to WebPush * Updated README
1 parent 7b6d1e9 commit 5c185f7

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ $defaultOptions = [
202202
'urgency' => 'normal', // protocol defaults to "normal". (very-low, low, normal, or high)
203203
'topic' => 'newEvent', // not defined by default. Max. 32 characters from the URL or filename-safe Base64 characters sets
204204
'batchSize' => 200, // defaults to 1000
205+
'contentType' => 'application/json', // defaults to "application/octet-stream"
205206
];
206207

207208
// for every notification
@@ -235,6 +236,11 @@ In order to fix this, WebPush sends notifications in batches. The default size i
235236
to decrease this number. Do this while instantiating WebPush or calling `setDefaultOptions`. Or, if you want to customize this for a specific flush, give
236237
it as a parameter : `$webPush->flush($batchSize)`.
237238

239+
#### contentType
240+
241+
Sets the "Content-Type" header for HTTP requests with a non-empty payload sent to the push service.
242+
Especially newer [Declarative push messages](https://www.w3.org/TR/push-api/#declarative-push-message) require a specific JSON payload, so this should be set to "application/json" in such cases.
243+
238244
### Server errors
239245

240246
You can see what the browser vendor's server sends back in case it encountered an error (push subscription expiration, wrong parameters...).

src/Notification.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function getOptions(array $defaultOptions = []): array
4343
$options['TTL'] = array_key_exists('TTL', $options) ? $options['TTL'] : $defaultOptions['TTL'];
4444
$options['urgency'] = array_key_exists('urgency', $options) ? $options['urgency'] : $defaultOptions['urgency'];
4545
$options['topic'] = array_key_exists('topic', $options) ? $options['topic'] : $defaultOptions['topic'];
46+
$options['contentType'] = array_key_exists('contentType', $options) ? $options['contentType'] : $defaultOptions['contentType'];
4647

4748
return $options;
4849
}

src/WebPush.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ protected function prepare(array $notifications): array
263263
$localPublicKey = $encrypted['localPublicKey'];
264264

265265
$headers = [
266-
'Content-Type' => 'application/octet-stream',
266+
'Content-Type' => $options['contentType'],
267267
'Content-Encoding' => $contentEncoding,
268268
];
269269

@@ -384,6 +384,7 @@ public function setDefaultOptions(array $defaultOptions): WebPush
384384
$this->defaultOptions['topic'] = $defaultOptions['topic'] ?? null;
385385
$this->defaultOptions['batchSize'] = $defaultOptions['batchSize'] ?? 1000;
386386
$this->defaultOptions['requestConcurrency'] = $defaultOptions['requestConcurrency'] ?? 100;
387+
$this->defaultOptions['contentType'] = $defaultOptions['contentType'] ?? 'application/octet-stream';
387388

388389

389390
return $this;

0 commit comments

Comments
 (0)