Configure URL OneSignal PHP API

1

I'm deploying onesignal on my system in php, following Documentation

My code is:

    function sendMessage(){
        $content = array(
            "pt" => $conteudo
            );
        $title = array(
            "en" => $titulo
            );
        $fields = array(
            'app_id' => "************************",
            'included_segments' => array('All'),
            'data' => array("foo" => "bar"),
            'contents' => $content,
            'headings' => $title,
            'attachments' => array("url" => 'http://www.google.com')
        );

        $fields = json_encode($fields);
    print("\nJSON sent:\n");
    print($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 *******************************************************'));
        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);

        return $response;
    }

    $response = sendMessage();
    $return["allresponses"] = $response;
    $return = json_encode( $return);

  print("\n\nJSON received:\n");
    print($return);
  print("\n");

My problem is that the destination URL is not working, so that when the user clicks the notification it is redirected to this url.

    
asked by anonymous 04.10.2017 / 16:47

2 answers

2

From what I have in my code, it's working like this

$fields = array(
        'app_id' => "APP_ID",
        'included_segments' => array('All'),
        'headings' => $headings,
        'url' => "url_a_enviar",
        'data' => array("foo" => "bar"),
        'large_icon' =>"ic_launcher_round.png",
        'contents' => $content
);
    
09.05.2018 / 20:24
1

My friend tries to put this code here and take the test.

function sendMessage(){ 
    $content = array(
        "en" => "Você Tem Um Novo Aviso Individual !!!"
        );

    $fields = array(
        'app_id' => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",

        "include_player_ids" => ('ALL'),

       'data' => array("foo" => "bar"),
        'contents' => $content

        );


$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 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' ));
    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);

    return $response;
}

$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode( $return);

$content;
    
08.05.2018 / 14:35