Web push notification not working in Mozilla but it's working in Chrome, Java lang

99 Views Asked by At

Giving below error while sending notification to Mozilla server

class Test {

    private static String vapidPublickey = "BJz1hQvWgl62aB3Cd-HT1ElM8HGQJ7nrEC25bwRLMcINZVFH1d5iac5YbjW8sZ0Uh6FKu8v3O7Q3oJYdQF_sk_A";
    private static String vapidPrivatekey = "37COK2I3KoNJSh_Ir56OhfhrR8x8wqM4Qcfk7AxilIU";

    public static void main(String[] args) throws GeneralSecurityException {
         Security.addProvider(new BouncyCastleProvider());
         PushService pushService = new PushService(vapidPublickey, vapidPrivatekey);
         String messageJson = ""         {\"data\":\"https://mkjewel.listany.com/earrings\",\"icon\":\"https://listany-prod.s3.amazonaws.com/images/MKJewel/mk_logo__png.png\",\"title\":\"10%off\",\"body\":\"Gooddealondiwali\"}"";
         
         String endPoint = "https://updates.push.services.mozilla.com/wpush/v2/gAAAAABk5xu3VjaceCx995HyDNiB7HOWnqmv4s8b-76flzGnbTTAOQpnt3vUZxLlapIl_TIQyeTFwXxQ0Zoy2IO0FvnXYyziXdG01x8Ov7EvJIy7pCC_5YSylRHKL3UnhT4FfvLhCoALrL9S-N8xOE5er7xu8MipJo5OW4Jtm6YUq_NYGq0iX-o";
            String p256dh = "BHV2OOD6qGxyRThsaH5KfZu8C5RMxVK57CctdJOQp8dzxpq1C5heMutWDt6Wk_s_afNtLKe6ipiQT--YpcmafLY";
            String auth = "-BQQwuJmjFNCnroJ0JYh5A";
            Subscription.Keys keys = new Subscription.Keys(p256dh, auth);
            Subscription subscription = new Subscription(endPoint, keys);

         HttpResponse httpResponse = pushService.send(new Notification(subscription, messageJson));

         InputStream content = httpResponse.getEntity().getContent();
     List<String> strings = IOUtils.readLines(content, "UTF-8");
     System.out.println(strings);
    }  
}

Getting below out put on console:

{
    "code": 401,
    "errno": 109,
    "error": "Unauthorized",
    "message": "VAPID public key mismatch",
    "more_info": "http://autopush.readthedocs.io/en/latest/http.html#error-codes"
}

Note: Below two dependencies has to be added in pom.xml file

<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.70</version>
</dependency>

<dependency>
    <groupId>nl.martijndwars</groupId>
    <artifactId>web-push</artifactId>
    <version>5.1.1</version>
</dependency>

I tried creating new vapid key pair using below npm cmds in order to use

Step 1: npm install web-push

Step 2: In this created file "generate-keys.js" i written the below code

const webpush = require('web-push');

// Replace these with your actual details
const vapidKeys = webpush.generateVAPIDKeys();

console.log('VAPID Public Key:', vapidKeys.publicKey);
console.log('VAPID Private Key:', vapidKeys.privateKey);

Step : 3 Then execute this cmmd "node generate-keys.js" which will give below response:

VAPID Public Key: BJz1hQvWgl62aB3Cd-HT1ElM8HGQJ7nrEC25bwRLMcINZVFH1d5iac5YbjW8sZ0Uh6FKu8v3O7Q3oJYdQF_sk_A
VAPID Private Key: 37COK2I3KoNJSh_Ir56OhfhrR8x8wqM4Qcfk7AxilIU
0

There are 0 best solutions below