Search…
The Subscription
The subscription is created on client side when the end-user allows your application to send push messages.
On client side (Javascript), you can simply send to your server the object you receive using JSON.stringify.
Javascript examples to get a Subscription from the web browser are not provided here. Please refer to other resources such as blog posts or library documentation.
A subscription object will look like:
1
{
2
"endpoint":"https://updates.push.services.mozilla.com/wpush/v2/AAAAAAAA[…]AAAAAAAAA",
3
"keys":{
4
"auth":"XXXXXXXXXXXXXX",
5
"p256dh":"YYYYYYYY[…]YYYYYYYYYYYYY"
6
}
7
}
Copied!
On server side, you can get a WebPush\Subscription object from the JSON string using the dedicated method WebPush\Subscription::createFromString.
1
<?php
2
3
use WebPush\Subscription;
4
5
$subscription = Subscription::createFromString('{"endpoint":"https://updates.push.services.mozilla.com/wpush/v2/AAAAAAAA[…]AAAAAAAAA","keys":{"auth":"XXXXXXXXXXXXXX","p256dh":"YYYYYYYY[…]YYYYYYYYYYYYY"}}');
Copied!

Supported Content Encodings

By default, the content encoding aesgcm will be used. This encoding indicates how the payload of the notification should be formatted. The PushManager object from the Push API may list all acceptable encodings. In this case, it could be interesting to set these encodings to the Subscription object.
1
// Retreive the supported content encodings
2
const supportedContentEncodings = PushManager.supportedContentEncodings || ['aesgcm'];
3
4
// Assign the encodings to the subscription object
5
const jsonSubscription = Object.assign(
6
subscription.toJSON(),
7
{ supportedContentEncodings }
8
);
9
10
// Send the subscription object to the application server
11
fetch('/subscription/add', {
12
method: 'POST',
13
body: JSON.stringify(jsonSubscription),
14
});
Copied!
This will result in something like as follow:
1
{
2
"endpoint":"https://updates.push.services.mozilla.com/wpush/v2/AAAAAAAA[…]AAAAAAAAA",
3
"keys":{
4
"auth":"XXXXXXXXXXXXXX",
5
"p256dh":"YYYYYYYY[…]YYYYYYYYYYYYY",
6
"supportedContentEncodings":["aes128gcm","aesgcm"]
7
}
8
}
Copied!
The order of supportedContentEncodings is important. First supported item will be used. If possible, AES128GCM should be used as prefered content encoding.
Export as PDF
Copy link