Error 400 UnauthorizedRegistration
See original GitHub issueI use this code:
import java.nio.charset.StandardCharsets;
import java.security.Security;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import com.google.gson.JsonObject;
import nl.martijndwars.webpush.Notification;
import nl.martijndwars.webpush.PushService;
public class Teste {
public static void main(String[] args) {
try {
Security.addProvider(new BouncyCastleProvider());
// Send notification!
sendPushMessage(getPayload());
} catch (Exception e) {
e.printStackTrace();
}
}
private static final int TTL = 255;
public static void sendPushMessage(byte[] payload) {
// Figure out if we should use GCM for this notification somehow
try{
Notification notification;
PushService pushService;
// Create a notification with the endpoint, userPublicKey from the subscription and a custom payload
notification = new Notification(
"https://fcm.googleapis.com/fcm/send/d8KX2q4goDM:APA91bH5Boq0076mY4-YdxIOrsD_pzfx6DorrD6FRaksk5sf64A3Z9cySX2JhxwOlql1wq-Bdo0SZvSmBbARZaxTgn4_O9MHbbG_JFY-ZJp0i6WauLwllglA54lBp6NkWB0q6axNHIa3",
"BPNcSFiObeUbcCg4m5c1AybHv7NSdBE_X5YJ6ZFQfpXWnXQbDnEILz3qPe4Zb-9M9B6Lc_W20uSzVmH1ZyNuWwk=",
"nJiZotPSQE4P4z75Igq57Q==",
"Hello, world!"
);
// Instantiate the push service, no need to use an API key for Push API
pushService = new PushService();
// Send the notification
HttpResponse httpResponse = pushService.send(notification);
System.out.println(httpResponse.getStatusLine().getStatusCode());
System.out.println(IOUtils.toString(httpResponse.getEntity().getContent(), StandardCharsets.UTF_8));
} catch (Exception e) {
e.printStackTrace();
}
}
private static byte[] getPayload() {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("title", "Hello");
jsonObject.addProperty("message", "World");
return jsonObject.toString().getBytes();
}
}
And receive this answer:
400
<HTML>
<HEAD>
<TITLE>UnauthorizedRegistration</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>UnauthorizedRegistration</H1>
<H2>Error 400</H2>
</BODY>
</HTML>
Issue Analytics
- State:
- Created 6 years ago
- Comments:18 (6 by maintainers)
Top Results From Across the Web
How to Fix a 400 Bad Request Error (Causes and Fixes) - Kinsta
The HTTP error 400 can occur due to incorrectly typed URL, malformed syntax, or a URL that contains illegal characters.
Read more >How to Fix a 400 Bad Request Error: 8 Easy Methods
The 400 bad request error is an HTTP status code that describes an error caused by an invalid request. Thus, the server can't...
Read more >How to Fix the 400 Bad Request Error - Lifewire
The 400 Bad Request error means that the request you sent to the website server to view the page was somehow incorrect.
Read more >How to Fix a 400 Bad Request Error [Causes and Fixes]
The 400 bad request is an HTTP response code sent from a web server that didn't understand the request sent from your browser....
Read more >Status Code 400 (Bad Request) Explained & How to fix it - Ryte
A status code 400 or a code 4xx indicates a client error. When the client sends an invalid request to the server, the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m glad you got this to work @dsteen338! I’d be happy to discuss ideas that could improve the wiki. PRs that improve the documentation are very welcome.
It seems that most comments on this issue are about problems with using the library and not with the library itself, so I will close the issue now. If someone bumps into a similar issue or wants to discuss improvements to the documentation, please create a new issue.
Scratch that! I got it to work, I just needed to un-register and re-register the service worker after adding the
applicationServerKey
to the subscription.My comment now becomes, that I needed to do a lot of googling and investigating to get to this point and get it working. I needed to find this thread specifically and use the example from https://github.com/MartijnDwars/spring-boot-web-push instead of the one from the Usage example in the wiki: https://github.com/web-push-libs/webpush-java/wiki/Usage-Example , I’m wondering if it might be of use to people who end up in a similar situation as me to update the wiki accordingly.