I am working on a push notification in android with PHP ( using fcm), Push notification is working fine but I am not getting any push notification whenever my App is "closed" (I am getting notification only whenever my app is "open") Here is my code, where I am wrong?
function testingPush(){
define("FCM_URL", "https://fcm.googleapis.com/fcm/send");
define( "FCM_KEY","xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
$devicetype1 = "ios";
$devicetype2 = "android";
$deviceType = $rows['device_type']; // coming from database
$deviceToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$titleMessage="Push Notification Testing";
$message="Lorem Ipsum";
$msgs = array();
if (strcasecmp($devicetype1, $deviceType) == 0)
{
/* sending push to IOS */
$notification = array('body'=>$message,
'sound'=>"default",
'title'=>$titleMessage,
'badge'=>1);
$data = array('notificationType'=>1,
'displayURL'=>"https://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/FloorGoban.JPG/1024px-FloorGoban.JPG");
$msgs = array('to'=>$deviceToken,
'content_available'=>true,
'mutable_content'=>true,
'notification'=>$notification,
'priority'=>'high',
'data'=>$data);
}
else{
/* sening push to android */
$notification = array('contentTitle'=>$titleMessage,
'message'=>$message);
$msgs = array('to'=>$deviceToken,
'priority'=>'high',
'data'=>$notification);
}
$this->sendPushNotification_With($msgs);
}
private function sendPushNotification_With($json) {
$headers = array('Authorization: key=' . FCM_KEY,'Content-Type: application/json');
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, FCM_URL );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode($json) );
$result = curl_exec($ch );
curl_close( $ch );
}