web-push-libs / webpush-java

Web Push library for Java
MIT License
323 stars 112 forks source link

Unable to send Push Notification #106

Closed iyashsoni closed 4 years ago

iyashsoni commented 5 years ago

Hey @MartijnDwars, while sending the Notification I'm getting the following error:

the key in the authorization header does not correspond to the sender ID 
used to subscribe to this user. Please ensure you are using the correct sender 
ID and server Key from the Firebase console.

Getting the same error from curl request:

curl "https://fcm.googleapis.com/fcm/send/c9QOiG7bkdc:APA91bEwwW7Qt088axeqO0n0GVzDnVbu5oWGIy8R5YTbx2qYFAKs1iwL3w0F6zjHE8zGv1xX1WTUG0LTDq-I8Z1Zu5Su-CYnmW9yao7VKvca3KZJoxzsPM2riDsDAjCb9x6Gfr_i395J" 
--request POST --header "TTL: 60" --header "Content-Length: 0" \
 --header "Authorization: key=AAAAZod_va4:APA91bGa76B43GWRtECRmuLV6qOXjI9I_ulPrhp4oIZM91d4tfGSRPdrAWqko4_HRD0bzGKs-AN_AUerBxcKoz9IQ5uzkuVmRFOKmdy22D9Buq6LPOqOosIJ8GIdIblX2oY67p7jUcdG"

I've generated VAPID keys using npm module. These are the keys:

Public Key:
BNqEgHEqLOmF_B5rAkjwbFiTr6k5HzthWYul3UmorHOKXeXubI1D0oRw-nMN30ju6KTle2b_q9vNY1LAFtCz9B8

Private Key:
LwhPxh4pWvVQuSOHrLHIyY3qDWihyaLofzfoKPwLxjI

The subscription object looks like this:

{
"endpoint":"https://fcm.googleapis.com/fcm/send/c9QOiG7bkdc:APA91bEwwW7Qt088axeqO0n0GVzDnVbu5oWGIy8R5YTbx2qYFAKs1iwL3w0F6zjHE8zGv1xX1WTUG0LTDq-I8Z1Zu5Su-CYnmW9yao7VKvca3KZJoxzsPM2riDsDAjCb9x6Gfr_i395J",
"userPublicKey":"BDxcO0vK8HjrlGAOHhlCBj75G21uuRlc64WQU/lf1KOKMvZ175sLLcpEPjaln3QvJK9a5idKKegvSjPbslbd8XA=",
"userAuth":"OXde8QZMgpvnN+05Dljhgw=="
}

This is the code that I'm using for sending Push Notification:

nl.martijndwars.webpush.Notification notification;
WebSubscription sub = new WebSubscription();
sub.setEndpoint("https://fcm.googleapis.com/fcm/send/c9QOiG7bkdc:APA91bEwwW7Qt088axeqO0n0GVzDnVbu5oWGIy8R5YTbx2qYFAKs1iwL3w0F6zjHE8zGv1xX1WTUG0LTDq-I8Z1Zu5Su-CYnmW9yao7VKvca3KZJoxzsPM2riDsDAjCb9x6Gfr_i395J");
sub.setAuth("OXde8QZMgpvnN+05Dljhgw==");
sub.setKey("BDxcO0vK8HjrlGAOHhlCBj75G21uuRlc64WQU/lf1KOKMvZ175sLLcpEPjaln3QvJK9a5idKKegvSjPbslbd8XA=");
try {
    PushService pushService;
    notification = new nl.martijndwars.webpush.Notification(
            sub.getEndpoint(),
            sub.getUserPublicKey(),
            sub.getAuthAsBytes(),
            wpNotification.getAlert().getBytes(),
            255);

    // Instantiate the push service with a GCM API key
    pushService = new PushService("AIzaSyDpFKofoaa-VCFAJGYbK6QBfIRW5n2_j9o");
    pushService.setPrivateKey(nl.martijndwars.webpush.Utils.loadPrivateKey("LwhPxh4pWvVQuSOHrLHIyY3qDWihyaLofzfoKPwLxjI"));
    pushService.setPublicKey(nl.martijndwars.webpush.Utils.loadPublicKey("BNqEgHEqLOmF_B5rAkjwbFiTr6k5HzthWYul3UmorHOKXeXubI1D0oRw-nMN30ju6KTle2b_q9vNY1LAFtCz9B8"));
    HttpResponse httpResponse = pushService.send(notification);
    InputStream resInputStream = httpResponse.getEntity().getContent();
    StringWriter writer = new StringWriter();
    IOUtils.copy(resInputStream, writer, "UTF-8");
    String result = writer.toString();
    System.out.println(result);
} catch (Exception e) {
    LOG.error(sourceMethod, "Send Message failure...", e);
}

Am I using the keys wrong with Web-Push Library?

From the web-app, registration happens as follows:

const applicationServerPublicKey = "BNqEgHEqLOmF_B5rAkjwbFiTr6k5HzthWYul3UmorHOKXeXubI1D0oRw-nMN30ju6KTle2b_q9vNY1LAFtCz9B8";
const applicationServerKey = urlB64ToUint8Array(applicationServerPublicKey);
registration.pushManager.subscribe({
    userVisibleOnly: true,
    applicationServerKey: applicationServerKey
}).then(function(subscription) {
    return registerUsingToken(subscription);
}).catch(function(error) {
    if (Notification.permission === 'denied') {
    logger.info('Permission for Notifications was denied');
    } else {
    logger.error('Unable to subscribe to push.', error);
    }
});

Any help is appreciated, Thanks

martijndwars commented 4 years ago

Sorry, didn't get the time to look into this. Were you able to resolve this issue? Could you share the solution (for those coming from Google)?

kopax commented 4 years ago

I am in the same situation. Can you share the solution ? Next time?

luisFebro commented 3 years ago

I am with the same situation.

JhonCordoba commented 3 years ago

i have the same problem, please help

EriHuang commented 2 years ago

i have the same problem, please help

taluks commented 1 year ago

Use a domain name, if you use an IP address then web pushes may not work