naturalprogrammer/webpush-java-demo

Cannot send notification => pushService.send(notification); do not send anything despite the ConcurrentHashMap is set with subscription

Closed this issue · 4 comments

kopax commented

Hello and thanks for doing this super example, I took it and implemented it, it's almost working (subsription, unsubscription) except the most important part , notifying all subscription.

This is what I am trying :

    @PostMapping(value = "/notify-all", produces = MediaType.APPLICATION_JSON_VALUE)
    public WebPushMessage notifyAll(@RequestBody WebPushMessage message) throws GeneralSecurityException, IOException, JoseException, ExecutionException, InterruptedException {
        WebPushMessage msg = null != message && message.getMessage() == null ? new WebPushMessage("test", "https://staging-www.icimatin.com/?clickTarget=me", "test message") : message;
        log.debug("Total subscriptions where sending: " + subscriptions.size());
        for (WebPushSubscription subscription: subscriptions.values()) {
            Notification notification = new Notification(
                    subscription.getNotificationEndPoint(),
                    subscription.getPublicKey(),
                    subscription.getAuth(),
                    objectMapper.writeValueAsBytes(msg));
            pushService.send(notification);
            log.debug("===> Sending notification with: " + notification.getPayload().toString());
        }

        return message;
    }

This is my WebPushMessage.java class:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class WebPushMessage {
    @JsonProperty("title")
    private String title;
    @JsonProperty("clickTarget")
    private String clickTarget;
    @JsonProperty("message")
    private String message;
}

This is how I test it:

  1. Register using a button on my UI
  2. send from CURL a post request to this controller : curl -vvv -H 'Content-Type: application/json' -H 'accept: application/json' --data '{"title": "testing", "clickTarget": "http://localhost:8080/?ok=true", "message": "hello welcome"}' http://localhost:8080/public/web-push/notify-all

For some reason, I have the following error when serializing the message:

2020-08-17 18:06:29,273 DEBUG org.springframework.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing POST request for [/public/web-push/notify-all]
2020-08-17 18:06:29,274 DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping : Looking up handler method for path /public/web-push/notify-all
2020-08-17 18:06:29,274 DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping : Returning handler method [public com.kopaxgroup.api.userManagement.domain.WebPushMessage com.kopaxgroup.api.userManagement.controller.PublicWebPushController.notifyAll(com.kopaxgroup.api.userManagement.domain.WebPushMessage) throws java.security.GeneralSecurityException,java.io.IOException,org.jose4j.lang.JoseException,java.util.concurrent.ExecutionException,java.lang.InterruptedException]
2020-08-17 18:06:29,274 DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor : Read [class com.kopaxgroup.api.userManagement.domain.WebPushMessage] as "application/json;charset=UTF-8" with [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@55877274]
2020-08-17 18:06:29,274 DEBUG com.kopaxgroup.api.userManagement.controller.PublicWebPushController : Total subscriptions where sending: 9
2020-08-17 18:06:29,379 DEBUG com.kopaxgroup.api.userManagement.controller.PublicWebPushController : ===> Sending notification with: [B@56237836
2020-08-17 18:06:29,448 DEBUG com.kopaxgroup.api.userManagement.controller.PublicWebPushController : ===> Sending notification with: [B@2717438d
2020-08-17 18:06:29,518 DEBUG com.kopaxgroup.api.userManagement.controller.PublicWebPushController : ===> Sending notification with: [B@30b535cf
2020-08-17 18:06:29,606 DEBUG com.kopaxgroup.api.userManagement.controller.PublicWebPushController : ===> Sending notification with: [B@427d8e3d
2020-08-17 18:06:29,747 DEBUG com.kopaxgroup.api.userManagement.controller.PublicWebPushController : ===> Sending notification with: [B@76ccd38b
2020-08-17 18:06:29,809 DEBUG com.kopaxgroup.api.userManagement.controller.PublicWebPushController : ===> Sending notification with: [B@5a2cec7b
2020-08-17 18:06:29,891 DEBUG com.kopaxgroup.api.userManagement.controller.PublicWebPushController : ===> Sending notification with: [B@51feb5f1
2020-08-17 18:06:29,964 DEBUG com.kopaxgroup.api.userManagement.controller.PublicWebPushController : ===> Sending notification with: [B@6cc70596
2020-08-17 18:06:30,026 DEBUG com.kopaxgroup.api.userManagement.controller.PublicWebPushController : ===> Sending notification with: [B@7a08a18d
2020-08-17 18:06:30,027 DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor : Written [WebPushMessage(title=testing, clickTarget=https://staging-www.icimatin.com/?ok=true, message=hello welcome)] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@55877274]
2020-08-17 18:06:30,028 DEBUG org.springframework.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2020-08-17 18:06:30,028 DEBUG org.springframework.web.servlet.DispatcherServlet : Successfully completed request

There is no error, it just does not push anything, any idea why ?

How I can fix it to get web push working? Thanks in advance

I've forgotten this thingy absolutely, so sorry won't be of help :( Someone else, or you yourself may need to dig deeper, I'm afraid.

kopax commented

I got this one finally solved but I still can't use the web push using a java server using your code.

Now I am absolutely stuck because after copying your code into a react implementation.

I can't get the notification to work, it seems that the error log no errors at all but nothing happen. I can subscribe, unregister and post a message with the same content it's just that it does not show any notifications nor errors in the logs.

This is the comand used for notifying all subscriptions:

curl -vvv -H 'Content-Type: application/json' -H 'accept: application/json' --data '{"title": "testing", "clickTarget": "https://staging-www.icimatin.com/?ok=true", "message": "hello welcome"}' https://staging-api.******.com/public/web-push/notify-all

This is the controller used for posting notification:

    @PostMapping(value = "/notify-all", produces = MediaType.APPLICATION_JSON_VALUE)
    public WebPushMessage notifyAll(@RequestBody(required = false) WebPushMessage message) throws GeneralSecurityException, IOException, JoseException, ExecutionException, InterruptedException {
        WebPushMessage msg = null != message && message.getMessage() == null ? new WebPushMessage("test", "https://staging-www.icimatin.com/?clickTarget=me", "test message") : message;
        log.debug("Total subscriptions where sending: " + subscriptions.size());
        for (WebPushSubscription subscription: subscriptions.values()) {
            log.debug("getNotificationEndPoint :" + subscription.getNotificationEndPoint());
            log.debug("getPublicKey :" + subscription.getPublicKey());
            log.debug("getAuth :" + subscription.getAuth());

            Notification notification = new Notification(
                    subscription.getNotificationEndPoint(),
                    subscription.getPublicKey(),
                    subscription.getAuth(),
                    objectMapper.writeValueAsBytes(msg));
            try {
                log.debug("sending start");
                HttpResponse response = pushService.send(notification);
                log.debug("sending done" + response.getStatusLine() + " " + Arrays.toString(response.getAllHeaders()));
                Future<HttpResponse> response2 = pushService.sendAsync(notification);
                log.debug("response2 : " + response2.toString());

            } catch (Exception e) {
                log.debug("sending produced exception :" + e.getMessage());
                log.debug("sending produced stacktrace :" + Arrays.toString(e.getStackTrace()));
            }
            log.debug("===> Sending notification is over, payload was: " + Arrays.toString(notification.getPayload()));
        }

        return message;
    }

This my the server logs:

2020-08-18 01:32:30,928 DEBUG org.springframework.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing POST request for [/public/web-push/notify-all]
2020-08-18 01:32:30,929 DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping : Looking up handler method for path /public/web-push/notify-all
2020-08-18 01:32:30,929 DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping : Returning handler method [public com.kopaxgroup.api.userManagement.domain.WebPushMessage com.kopaxgroup.api.userManagement.controller.PublicWebPushController.notifyAll(com.kopaxgroup.api.userManagement.domain.WebPushMessage) throws java.security.GeneralSecurityException,java.io.IOException,org.jose4j.lang.JoseException,java.util.concurrent.ExecutionException,java.lang.InterruptedException]
2020-08-18 01:32:30,930 DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor : Read [class com.kopaxgroup.api.userManagement.domain.WebPushMessage] as "application/json;charset=UTF-8" with [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@6d4a82]
2020-08-18 01:32:30,930 DEBUG com.kopaxgroup.api.userManagement.controller.PublicWebPushController : Total subscriptions where sending: 1
2020-08-18 01:32:30,930 DEBUG com.kopaxgroup.api.userManagement.controller.PublicWebPushController : getNotificationEndPoint :https://fcm.googleapis.com/fcm/send/c2GQhRRT4hM:APA91bFppaJUBXwLqbs73Zz4Y99XYXXz-eK5ZNiKfGdnZSUY1TctWb4i5mheDkgSkZHVUARWP5vSFCvQ1s8JQk6hF5KdBmdJPq4ipeMBlnqPxSzHy1KFYUgAR6iGTk-ok7076zT2dz78
2020-08-18 01:32:30,930 DEBUG com.kopaxgroup.api.userManagement.controller.PublicWebPushController : getPublicKey :BOKs3TGARO613OQS4ipN8/u69jWUjxRf7z+MP1/xGcrnFc6OnxjEZe8GBItwxDCzhx4FEFOvqtWE326OPr3jF6g=
2020-08-18 01:32:30,930 DEBUG com.kopaxgroup.api.userManagement.controller.PublicWebPushController : getAuth :qGM5BoPOoqZRBicvQGR9pw==
2020-08-18 01:32:30,933 DEBUG com.kopaxgroup.api.userManagement.controller.PublicWebPushController : sending start
2020-08-18 01:32:31,894 DEBUG com.kopaxgroup.api.userManagement.controller.PublicWebPushController : sending doneHTTP/1.1 403 Forbidden [Content-Type: text/plain; charset=utf-8, X-Content-Type-Options: nosniff, X-Frame-Options: SAMEORIGIN, X-Xss-Protection: 0, Date: Tue, 18 Aug 2020 01:43:46 GMT, Content-Length: 194, Alt-Svc: h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"]
2020-08-18 01:32:31,944 DEBUG com.kopaxgroup.api.userManagement.controller.PublicWebPushController : response2 : org.apache.http.concurrent.BasicFuture@6576786a
2020-08-18 01:32:31,945 DEBUG com.kopaxgroup.api.userManagement.controller.PublicWebPushController : ===> Sending notification is over, payload was: [123, 34, 116, 105, 116, 108, 101, 34, 58, 34, 116, 101, 115, 116, 105, 110, 103, 34, 44, 34, 99, 108, 105, 99, 107, 84, 97, 114, 103, 101, 116, 34, 58, 34, 104, 116, 116, 112, 115, 58, 47, 47, 115, 116, 97, 103, 105, 110, 103, 45, 119, 119, 119, 46, 105, 99, 105, 109, 97, 116, 105, 110, 46, 99, 111, 109, 47, 63, 111, 107, 61, 116, 114, 117, 101, 34, 44, 34, 109, 101, 115, 115, 97, 103, 101, 34, 58, 34, 104, 101, 108, 108, 111, 32, 119, 101, 108, 99, 111, 109, 101, 34, 125]
2020-08-18 01:32:31,945 DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor : Written [WebPushMessage(title=testing, clickTarget=https://staging-www.icimatin.com/?ok=true, message=hello welcome)] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@6d4a82]
2020-08-18 01:32:31,945 DEBUG org.springframework.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2020-08-18 01:32:31,945 DEBUG org.springframework.web.servlet.DispatcherServlet : Successfully completed request
****

I never get a notifications with your exacte same code even on the client (except I ported it into react) and the same server (except I have generated the the VAPID pair using a tool on npm and I moved the PushServiceinto@beanand the configuration into@Configuration`.

The error is:

sending done HTTP/1.1 403 Forbidden [Content-Type: text/plain; charset=utf-8, X-Content-Type-Options: nosniff, X-Frame-Options: SAMEORIGIN, X-Xss-Protection: 0, Date: Tue, 18 Aug 2020 01:43:46 GMT, Content-Length: 194, Alt-Svc: h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"]

It seems that the keys are wrong, I have check all the payloads and I always have keys defined.

Alll requests perform apparently normally, That would really help if you could give a hint on how to troubleshoot further, thanks!

kopax commented

Should I configure FCM ? You did not in your demo.

kopax commented

After hours of digging and reading documentation, I realised your demo was not working as well as https://gauntface.github.io/simple-push-demo/ which is also supported by firefox. This is 3 years old so it must be deprecated now. Closing and thank you