Configure Onesignal PHP API URL

Asked

Viewed 502 times

1

I’m deploying onesignal in my php system, following the 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, that is, so that when the user clicks on the notification it is redirected to this url.

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
);

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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.