Friday, March 31, 2017

Send push Notification using android with php part 2

Please get Server key from firebase console like this

In this section is relative to the PHP,

make "fcm.php" file and past following code

$url= "";
$key= "Service KEY FROM ABOVE SCREEN";

                if(($url != null && $url!="") && ($key != null && $key!="")){
                    $post_data = array(
                        'to' => $ToGCMToken, // This is token which sync from android and saved in mysql, each user have own gcmtoken
                        'content_available' => TRUE,
                        'priority' => 'high',
                        'data' => array(
                            'type' =>  "SIMPLE",
                            'body' =>  "Hello This is announcement",
                            'title' => "New announcement",
                            'badge' => "1",
                            'date' => 'Now()',
                            'sound' => "default",
                            'objectJSON' => "Hello, This is FCM Notification"
                    $headers = array(
                        'Authorization: key=' . $key,
                        'Content-Type: application/json'

                    $data_json = json_encode($post_data);

                    $ch = curl_init();
                    curl_setopt( $ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
                    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

                    //execute post
                    $result = curl_exec($ch);

                    //close connection

                    echo "FCM sent succesfully";

No comments:

Post a Comment