0
votes

I'm looking at the webpush-java code.

I tried to send a notification using the CLI tools

java -jar build/libs/web-push-3.1.0-all.jar send-notification, etc.. etc..

... But this causes a HTTP/1.1 400 UnauthorizedRegistration. What might cause this?

UPDATE: I repeated the send-notification with new keys and subscription... and this time I noticed further error diagnostics:-

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. HTTP/1.1 400 UnauthorizedRegistration [Content-Type: text/html; charset=UTF-8, Date: Fri, 16 Mar 2018 10:02:46 GMT, Expires: Fri, 16 Mar 2018 10:02:46 GMT, Cache-Control: private, max-age=0, X-Content-Type-Options: nosniff, X-Frame-Options: SAMEORIGIN, X-XSS-Protection: 1; mode=block, Server: GSE, Alt-Svc: hq=":443"; ma=2592000; quic=51303431; quic=51303339; quic=51303335,quic=":443"; ma=2592000; v="41,39,35", Accept-Ranges: none, Vary: Accept-Encoding, Transfer-Encoding: chunked] [Chunked: false]

1
Usually 400 UnauthorizedRegistration is caused by VAPID errors: make sure that VAPID keys are configured properly and that you are using the correct key to sign the requests - collimarco
I've repeated the process of recreating the public and private keys, pushManager.subscribe to get a subscription... and then send a notification. This time I noticed more error diagnostics... I've added these above. - Daniel Freeman

1 Answers

1
votes

Library author here. As @collimarco mentioned, something probably went wrong with generating or encoding the keys. Are you using the latest version of the library? How did you generate the keys? Did you copy the public key to your web application?

You might also want to take a look at the spring-boot-web-push project, where I integrate the web push library in a clean Spring Boot application. The README explains in five steps how to run the application, and it should Just Work (™).

Specifically, SendController.java shows how to send a notification:

public class SendController {
    private static final String PUBLIC_KEY = "BAPGG2IY3Vn48d_H8QNuVLRErkBI0L7oDOOCAMUBqYMTMTzukaIAuB5OOcmkdeRICcyQocEwD-oxVc81YXXZPRY";
    private static final String PRIVATE_KEY = "A7xDGuxMZ4ufflcAhBW23xpoWZNOLwM4Rw2wXjP0y6M";
    private static final String SUBJECT = "Foobarbaz";
    private static final String PAYLOAD = "My fancy message";

    @RequestMapping("/send")
    public String send(@RequestParam("subscriptionJson") String subscriptionJson) {
        Security.addProvider(new BouncyCastleProvider());

        try {
            PushService pushService = new PushService(PUBLIC_KEY, PRIVATE_KEY, SUBJECT);
            Subscription subscription = new Gson().fromJson(subscriptionJson, Subscription.class);
            Notification notification = new Notification(subscription, PAYLOAD);
            HttpResponse httpResponse = pushService.send(notification);
            int statusCode = httpResponse.getStatusLine().getStatusCode();

            return String.valueOf(statusCode);
        } catch (Exception e) {
            return ExceptionUtils.getStackTrace(e);
        }
    }
}

and push.js shows how to register the subscription:

function subscribe() {
    const publicKey = base64UrlToUint8Array('BAPGG2IY3Vn48d_H8QNuVLRErkBI0L7oDOOCAMUBqYMTMTzukaIAuB5OOcmkdeRICcyQocEwD-oxVc81YXXZPRY');

    navigator.serviceWorker.ready.then(function (serviceWorkerRegistration) {
        serviceWorkerRegistration.pushManager.subscribe({
            userVisibleOnly: true,
            applicationServerKey: publicKey
        })
        .then(function (subscription) {
            return sendSubscriptionToServer(subscription);
        })
        .catch(function (e) {
            if (Notification.permission === 'denied') {
                console.warn('Permission for Notifications was denied');
            } else {
                console.error('Unable to subscribe to push.', e);
            }
        });
    });
}