title: How to properly use the hook_insert in Drupal 8
body: <p>I created a custom module for Drupal 8. I made a hook that is to detect whenever a new node is created, then send a notification to a subscriber. The code I have is this:</p>
  
  <pre><code>&lt;?php 
  
  /**
  * @file
  * Contains onesignal_api.module.
  * 
  */
  
  use Drupal\Core\Entity\EntityInterface;
  
  /***
  * Hook into OneSignal API to send push notifications once a new node is created
  */
  
  function onesignal_api_insert(\Drupal\Core\Entity\EntityInterface $node) {
  if($node-&gt;isNew()) {
      function sendMessage() {
        $content      = array(
            "en" =&gt; 'New Node Created'
        );
        $hashes_array = array();
        array_push($hashes_array, array(
            "id" =&gt; "like-button",
            "text" =&gt; "Like",
            "icon" =&gt; "http://i.imgur.com/N8SN8ZS.png",
            "url" =&gt; "http://push-test/"
        ));
        array_push($hashes_array, array(
            "id" =&gt; "like-button-2",
            "text" =&gt; "Like2",
            "icon" =&gt; "http://i.imgur.com/N8SN8ZS.png",
            "url" =&gt; "http://push-test/"
        ));
        $fields = array(
            'app_id' =&gt; "XXXXXXXXX",
            'include_player_ids' =&gt; array("XXXXXX","XXXXX","XXXXXX"),
            'data' =&gt; array(
                "foo" =&gt; "bar"
            ),
            'contents' =&gt; $content,
            'web_buttons' =&gt; $hashes_array
        );
  
        $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 XXXXXXX'
        ));
        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);
  
    $data = json_decode($response, true);
    print_r($data);
    $id = $data['id'];
    print_r($id);
  
    print("\n\nJSON received:\n");
    print($return);
    print("\n");
  
   }//if Node is new
  }//func hook signal
  </code></pre>
  
  <p>Is there something that I need to change to get this to work? all of the code inside of the if statement works outside of the if statement.</p>