1

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 );
        }
iknow
  • 8,358
  • 12
  • 41
  • 68

1 Answers1

0

Well I think the problem is contentTitle in $notification , replace it to title and it should work and also in your ios notification you are using notification as seperate variable, I would rather suggest you to pass the notification paramter in data , as notification don't work in background.If above don't work recheck your key

aryanknp
  • 1,135
  • 2
  • 8
  • 21