使用webpush-java CLI工具

时间:2018-03-15 13:28:12

标签: java push-notification web-push vapid

我正在查看webpush-java代码。

我尝试使用CLI工具发送通知

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

...但这会导致HTTP / 1.1 400 UnauthorizedRegistration。可能导致这种情况的原因是什么?

更新:我用新密钥和订阅重复了发送通知......这次我注意到了进一步的错误诊断: -

SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。 SLF4J:默认为无操作(NOP)记录器实现 SLF4J:有关详细信息,请参阅http://www.slf4j.org/codes.html#StaticLoggerBinder。 HTTP / 1.1 400 UnauthorizedRegistration [内容类型:text / html; charset = UTF-8,日期:星期五,2018年3月16日10:02:46 GMT,到期日:星期五,2018年3月16日10:02:46 GMT,缓存控制:私人,最大年龄= 0,X-内容 - 类型选项: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 个答案:

答案 0 :(得分:1)

图书馆作者在这里。正如@collimarco所提到的,生成或编码密钥可能出错了。您使用的是最新版本的库吗?你是如何生成密钥的?您是否将公钥复制到Web应用程序中?

您可能还想查看spring-boot-web-push项目,我将Web推送库集成到一个干净的Spring Boot应用程序中。 README以五个步骤解释了如何运行应用程序,它应该是Just Work(™)。

具体来说,SendController.java显示了如何发送通知:

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);
        }
    }
}

push.js显示了如何注册订阅:

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);
            }
        });
    });
}