Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OneSignal set custom title message in web push notification

Tags:

php

web

onesignal

i have integrated one-signal in my web application, notifications are working fine but if considering web page title in push notification title.

i need to set it custom title in my push notification.

enter image description here

i need to set custom message in place of "Dashboard"

Here is my code:

$content = array(
    "en" => 'Hello Hii..!!'
);

$fields = array(
    'app_id' => 'APP_ID',
    'include_player_ids' => ['ids'],
    'data' => array("foo" => "bar"),
    'url' => 'URL',
    '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 AuthorizationKey';
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;
like image 719
krupal parsana Avatar asked Nov 21 '17 06:11

krupal parsana


Video Answer


2 Answers

set heading in your fields array

$content = array(
       "en" => 'Your message..!!'
   );
$heading = array(
   "en" => "Your custom title message"
);

$fields = array(
   'app_id' => 'YOUR_APP_ID',
   'include_player_ids' => [ids],
   'data' => array("foo" => "bar"),
   'url' => 'http://www.yoursite.com',
   'contents' => $content,
   'headings' => $heading
);
like image 175
Vipul L. Avatar answered Oct 28 '22 16:10

Vipul L.


Use this one as mention bellow

 public function sendPush($players_id,$massage,$data,$heading){
 // $players_id your device id where you want to push
   $data1[]=$players_id;

//message for your push

     $content = array(

      "en" => $massage

      );

// if you want to send data in for of JSON or some values

     $data_response=array(
        "value" => $data
     );

//you can add heading through this

     $heading = array( "en" => $heading);


   // print_r($cat_data);
   $fields = array(
      'app_id' => 'YOUR_APP_ID',
      'include_player_ids' => $data1,
       'contents' => $content,
       'headings' => $heading,
       'data' =>$data_response
      );

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

     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;

    }
like image 26
kishan verma Avatar answered Oct 28 '22 17:10

kishan verma