zeyneloz / onesignal-node

A Node.js Library for OneSignal push notification service
MIT License
207 stars 48 forks source link

Sending Push Notification to IOS Sends Twice #77

Open Darrow8 opened 2 years ago

Darrow8 commented 2 years ago

Whenever I try to send a notification it is sent twice although on the console it is only logged as once.

Here is my code:

 const OneSignal = require('onesignal-node');    
 const client = new OneSignal.Client('****', '****');  // private keys

async function sendNotification(notification) {

  try {
    const response = await client.createNotification(notification);
    console.log(response.body.id);
  } catch (e) {
    if (e instanceof OneSignal.HTTPError) {
      // When status code of HTTP response is not 2xx, HTTPError is thrown.
      console.log(e.statusCode);
      console.log(e.body);
    }
  }

  // or you can use promise style:
  client.createNotification(notification)
      .then(response => {
          console.log('sent notification')
        console.log(response);
    })
      .catch(e => {
          console.log('problem w notification sending')
          console.log(e)
    });
}

sendNotification({
    contents: {
        'en': 'test 3!',
    },
    include_player_ids: ['****'] // my phone's ID
});

Here is the response from the console:

Screen Shot 2022-01-01 at 12 32 51 PM

And I am getting the notification twice on my test device (IOS 14,2). I have looked on the duplicated notifications one signal page and it has been no help: https://documentation.onesignal.com/docs/duplicated-notifications.

max-sv-ma commented 2 years ago

@Darrow8 you are triggering client.createNotification(notification) twice, choose the async await solution OR the promise based one