How to send notification with onesignal for android app via Firebase

Asked

Viewed 181 times

0

So my app uses firebase, connected it with onesignal use a php function that sends notification to web browsers.

I WANT TO SEND VIA PHP DIRECT TO FIREBASE, OR BY ONESGINAL MY APP ALREADY AND CONFIGURED TO RECEIVE SYSTEM NOTIFICATION! I WILL MAKE A BUTTON THAT IF I CLICK IT CALLS THE FUNCTION AND SENDS THE NOTIFICATION TO THE APP

    function sendMessage() {
    $hashes_array = array();
    array_push($hashes_array, array(
      "id" => 'baixar-button',
      "text" => 'Baixar agora',
      "icon" => 'https://i.imgur.com/Uh3oJbP.png',
      "url" => ''.$_GET ["url_post"].'&ref=notificar#baixaragora'
    ));
    $fields = array(
      'app_id' => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
      'included_segments' => array('All'),
      'data' => array("foo" => "bar"),
      'contents' => array("en" => $_GET ["titulo_post"]),
      'headings' => array("en" => "Novo Lançamento!"),
      'chrome_web_image' => $_GET ["capa_post"],
      'small_icon' => 'https://i.imgur.com/PSGn27C.png',
      'large_icon' => 'https://i.imgur.com/PSGn27C.png',
      'adm_small_icon' => 'https://i.imgur.com/PSGn27C.png',
      'adm_large_icon' => 'https://i.imgur.com/PSGn27C.png',
      'chrome_web_badge' => 'https://i.imgur.com/PSGn27C.png',
      'firefox_icon' => 'https://i.imgur.com/PSGn27C.png',
      'chrome_icon' => 'https://i.imgur.com/PSGn27C.png',
      'android_sound' => 'notification',
      'url' => ''.$_GET ["url_post"].'&ref=notificar#baixaragora',
      'web_buttons' => $hashes_array,
    );

    $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 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
    ));
    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);
    $enviados = $data['recipients'];
    echo "Notificação enviada com sucesso para $enviados pessoas.";

It works 100%, but already their documentation in all forms but I do not know how to send to firebase for them... has how to do ? or has to make a way out of onesignal and yes directly with firebase, because I do not want to stay inside the firebase console to be creating a notification always!

REMEMBERING: want to send the notification with php, equal to the function above, and not within the app.

I TRIED TO USE THAT CODE:

function sendNotification()
{
 $url = "https://fcm.googleapis.com/fcm/send";
 $registrationIds = array( $_GET['id'] );
 $serverKey ='KEY AQUI';
 $title = "TESTE";
 $body = "This is body.";
 $notification = array('title' =>$title , 'text' => $body, 'sound' => 'default', 'badge' =>'1');

 $arrayToSend = array('registration_ids' => $registrationIds, 'notification'=>$notification,'priority'=>'high');
 $json = json_encode($arrayToSend);
 $headers = array();
 $headers[] = 'Content-Type: application/json';
 $headers[] = 'Authorization: key='. $serverKey;

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
 curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
 curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 //Send the request
 $result = curl_exec($ch);
 if ($result === FALSE)
 {
    die('FCM Send Error: ' . curl_error($ch));
 }

 curl_close( $ch );
 return $result;
}

But ai returns to blank page, and no notification arrives in the app..

  • Put inside a Try catch ai you can catch the Exception and with it check the error.

  • I have no idea what you’re talking about. kk

No answers

Browser other questions tagged

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