Showing posts with label Create custom notification in andoid using fcm. Show all posts
Showing posts with label Create custom notification in andoid using fcm. Show all posts

Sunday 30 July 2017

Create custom notification in android using fcm

Create custom notification in andoid using fcm


Create custom nofication code


function sendPushNotification($pushMessage){

$gcm_regid = "define GCM ID";

if($gcm_regid != ""){


// Custom Nofification


$icon_image = "front/images/example.png";



$fields = array (
/*'notification' =>
array (
'title' => 'title',
'body' => $pushMessage,
'icon' => $icon_image,
'sound' => 'default',

//'click_action' => $main_url,
),*/
'data' => array("title"=>(string)"title",
"message"=>(string)$pushMessage,
//"is_background"=>false,
"image"=> (string)$icon_image,
"timestamp"=> (string)date("Y-m-d G:i"),

),
"to" => $gcm_regid,
);

$gcm_api_key =  "Google Api Key";


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "Authorization: key=".$gcm_api_key;
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
//echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
}


}
call the function 
sendPushNotification("your push  notification")

 this example for normal push notification

function sendPushNotification($pushMessage){

$gcm_regid = "define GCM ID";

if($gcm_regid != ""){


// Custom Nofification


$icon_image = "front/images/example.png";



$fields = array (
'notification' =>
array (
'title' => 'title',
'body' => $pushMessage,
'icon' => $icon_image,
'sound' => 'default',

//'click_action' => $main_url,
),
/*'data' => array("title"=>(string)"title",
"message"=>(string)$pushMessage,
//"is_background"=>false,
"image"=> (string)$icon_image,
"timestamp"=> (string)date("Y-m-d G:i"),

),*/
"to" => $gcm_regid,
);

$gcm_api_key =  "Google Api Key";


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "Authorization: key=".$gcm_api_key;
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
//echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
}



call the function 
sendPushNotification("your push  notification")