Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

450
Visualizações
How to fetch User ID from OneSignal using JavaScript?

I have a OneSignal CURL code implemented in my webview app which sends notifications to every single user.

To target a specific user based on userid from OneSignal, i tried below code to fetch userID

<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>

  window.OneSignal = window.OneSignal || [];
  OneSignal.push(function() {
    OneSignal.init({
      appId: "xxxxx",
    });
    OneSignal.on('subscriptionChange', function (isSubscribed) {
        console.log("The user's subscription state is now:", isSubscribed);
        OneSignal.getUserId(function(userId) {
          console.log("OneSignal User ID:", userId);
          // (Output) OneSignal User ID: 270a35cd-4dda-4b3f-b04e-41d7463a2316    
         });
    });
  });

// Out put was nothing, maybe subscriptionchange was not true.

And also this:

window.OneSignal = window.OneSignal || [];
  OneSignal.push(function() {
    OneSignal.init({
      appId: "abbd6373-f394-4912-a314-40493c2a6201",
    });
     OneSignal.getUserId(function(userid){
        console.log(userid);
        $(".some").html(userid);
      });
  });

//Output 'NULL' on userid.

I all want to do is fetch the userid from OneSignal and use it on below code to target specific user:

notify("Alert","Testing Notification");
        $content = array("en" => "Testing Notification ");
        $fields = array(
            'app_id' => "xxxxxx",
            'included_segments' => array('All'),
            'contents' => $content,
            'data' => array("en" => "bar"),
            'large_icon' =>"ic_launcher_round.png",
        );
    
    $fields = json_encode($fields);
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json; charset=utf-8',
        'Authorization: Basic xxxxxx'
    ));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    
    $response = curl_exec($ch);
    curl_close($ch);

Any help is greatly appreciated..

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda