Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Send push notifications using One Signal + PHP + Server API?

I am using one signal to send push notification for android app. My question is

How Can I setup send push notifications using server rest api?

like image 965
user3544256 Avatar asked Apr 21 '16 10:04

user3544256


People also ask

How do I send a notification from OneSignal?

On Thunkable, open the Push Notifications dialog and paste this ID into the Android App ID field. Click the Live Test button. Once the app is on your phone, go back to One Signal. Click the "Check Subscribed Users" button.

How do I use OneSignal API?

To get the API Key and the App ID from OneSignal, navigate to the OneSignal Dashboard and click on the app you created inside of OneSignal. Once you have selected the app you want to work with, click the Settings tab. On this page, click on Keys & IDs. Copy the OneSignal App ID and the REST API Key.

Is OneSignal an API?

OneSignal's API is a powerful way to send personalized messages at scale, allowing our customers to send transactional notifications to dynamically-defined user segments.


2 Answers

<?PHP
function sendMessage(){
    $content = array(
        "en" => 'Testing Message'
        );

    $fields = array(
        'app_id' => "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx",
        'included_segments' => array('All'),
        'data' => array("foo" => "bar"),
        'large_icon' =>"ic_launcher_round.png",
        'contents' => $content
    );

    $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 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'));
    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");
?>
like image 139
Kishan Avatar answered Sep 28 '22 02:09

Kishan


You can always refer to the official docs:

https://documentation.onesignal.com/reference#section-example-code-create-notification

  • 'app_id' is currently known as (OneSignal App ID) in the OneSignal Settings->Keys and IDs

  • in 'Authorization: Basic xxx...' past the "REST API Key" just below the App ID

like image 44
Ahmed Albarody Avatar answered Sep 28 '22 04:09

Ahmed Albarody