Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Why PushSubscription need to be prepared for Subscription::create()? #179

@pwFoo

Description

@pwFoo

I built a working notification system (test, demo) with webpush-php, but why I can't use the original PushSubscription from client for Subscription::create()?

Unmodified PushSubscription server side ($_POST):

Array
(
    [subscription] => Array
        (
            [endpoint] => <ENDPOINT_URL>
            [keys] => Array
                (
                    [p256dh] => <STRING>
                    [auth] => <STRING>
                )

        )

    [payload] => Array
        (
            [title] => Cool!
            [message] => It works!
        )

)

I noticed that contentEncoding is missing with the original PushSubscription?!
The PushSubscription object isn't compatible because of the keys array with p256dh and auth instead of publicKey / authToken.

Error message from Subscription::create($_POST['subscription']):

<b>Fatal error</b>:  Uncaught TypeError: Argument 3 passed to Minishlink\WebPush\Encryption::padPayload() must be of the type string, null given, called in C:\xampp\htdocs\pushnotify\webpush\minishlink\web-push\src\WebPush.php on line 115 and defined in C:\xampp\htdocs\pushnotify\webpush\minishlink\web-push\src\Encryption.php:34
Stack trace:
#0 C:\xampp\htdocs\pushnotify\webpush\minishlink\web-push\src\WebPush.php(115): Minishlink\WebPush\Encryption::padPayload('{&quot;title&quot;:&quot;Cool!...', 3052, NULL)
#1 C:\xampp\htdocs\pushnotify\api\notify.php(35): Minishlink\WebPush\WebPush-&gt;sendNotification(Object(Minishlink\WebPush\Subscription), '{&quot;title&quot;:&quot;Cool!...', true)
#2 {main}
  thrown in <b>C:\xampp\htdocs\pushnotify\webpush\minishlink\web-push\src\Encryption.php</b> on line <b>34</b><br />

It works fine if I prepare the PushSubscription at client side as array ($_POST server side):

Array
(
    [subscription] => Array
        (
            [endpoint] => <ENDPOINT_URL>
            [publicKey] => <STRING>
            [authToken] => <STRING>
            [contentEncoding] => aes128gcm
        )

    [payload] => Array
        (
            [title] => Cool!
            [message] => It works!
        )

)

Client side code to prepare subscription:

    var key = sub.getKey('p256dh'),
        token = sub.getKey('auth'),
        contentEncoding = (PushManager.supportedContentEncodings || ['aesgcm'])[0];

    var data = {
            subscription: {
                endpoint: sub.endpoint,
                publicKey: key ? btoa(String.fromCharCode.apply(null, new Uint8Array(key))) : null,
                authToken: token ? btoa(String.fromCharCode.apply(null, new Uint8Array(token))) : null,
                contentEncoding
            },
            payload: {
                title: title,
                message: message
            }
    }

Prepare PushSubscription Object client side would be fine, but it looks like other webpush libs (webpush-go for example?) handle the original object instead of a custom array?

subJSON := `{<YOUR SUBSCRIPTION JSON>}`

So maybe it would be more flexible to (additional?) handle the PushSubscription without the need to modify / prepare it on client side?

At the moment I would need to change the client side code if the backend would move from php to go, but client side should work independent from the server side.

Maybe you just could explain to me why you decided to not support the "original" PushSubscription format?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions