felvieira
11/20/2019 - 11:35 PM

OneSignal Web Push Notifications

// Colocar os SDKs na pasta raíz

 <script
      src="https://cdn.onesignal.com/sdks/OneSignalSDK.js"
      async=""
    ></script>
    <script>
      var OneSignal = window.OneSignal || [];
      OneSignal.push(function() {
        OneSignal.init({
          appId: '220978c0-6406-46b8-88bf-5553ae66e3f8',
          autoResubscribe: true,
          notifyButton: {
            enable: true,
          },
          welcomeNotification: {
            title: 'Obrigado pela Inscrição',
            message: 'Agora iremos te avisar sobre seus lembretes!',
            // "url": "" /* Leave commented for the notification to not open a window on Chrome and Firefox (on Safari, it opens to your webpage) */
          },
        });
        OneSignal.showNativePrompt();
        // Pegar id e colocar no localstorage
        OneSignal.getUserId(function(id) {
          if (!localStorage.getItem('OSid')) localStorage.setItem('OSid', id);
          console.log(id);
        });
      });
    </script>
// Fazer uma chamada post Json
// url:https://onesignal.com/api/v1/notifications
// Header "Content-Type":"application/json"
//        "Authorization":"ODM5MzJjYjEtMDdhMi00NDQwLTg4YjItNzUxOTJjNGRhZGY3"
// Propriedade send_after para agendar o push pra data passada
// https://documentation.onesignal.com/reference#create-notification
// https://documentation.onesignal.com/docs/web-push-sdk#section-create-notification
// https://documentation.onesignal.com/docs/cordova-sdk#section--postnotification-
{
  "app_id": "220978c0-6406-46b8-88bf-5553ae66e3f8",
  "include_player_ids": ["1d084e6d-0ea6-48e7-a948-b5e3243a3773"],
  "data": {"foo": "bar"},
  "contents": {"en": "English Message"},
  "send_after": "Wed Nov 20 2019 20:22:00 GMT-0300 (Brasilia Standard Time)"
}

// Usando api do anota facil
axios.post('https://onesignal.com/api/v1/notifications', {
    headers: {
        'Content-Type': 'application/json',
        'Authorization': 'ODM5MzJjYjEtMDdhMi00NDQwLTg4YjItNzUxOTJjNGRhZGY3'
    },
    {
  "app_id": "220978c0-6406-46b8-88bf-5553ae66e3f8",
  "include_player_ids": ["1d084e6d-0ea6-48e7-a948-b5e3243a3773"],
  "data": {"foo": "bar"},
  "contents": {"en": "English Message"},
  "send_after": "Wed Nov 20 2019 20:22:00 GMT-0300 (Brasilia Standard Time)"
  }
})      
.then((response) => {
  console.log(response)
})
.catch((error) => {
    console.log(error)
})

//Correcao do post do axios
const updateWebNotification = (
    appID,
    authID,
    senderID,
    dateTime,
    data = { foo: 'bar' },
    contents = { en: 'English Message' }
  ) => {
    axios
      .post(
        'https://onesignal.com/api/v1/notifications',
        {
          app_id: appID,
          include_player_ids: senderID,
          data: data,
          contents: contents,
          send_after: dateTime,
        },
        {
          headers: {
            'Content-Type': 'application/json',
            Authorization: authID,
          },
        }
      )
      .then(response => {
        console.log(response);
      })
      .catch(error => {
        console.log(error);
      });
  };