Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    How to Send Push Notification by Using FCM(Firebase Cloud Messaging)

    Initially, we have used GCM(Google Cloud Messaging) for sending the push notification to Android devices, and now Google has been launched Firebase Cloud Messaging(FCM), the newer version of GCM with more features.

    FCM is a new Cloud Messaging Services by Google under the Firebase brand, which inherits the GCM's core infrastructure, plus SDKs and new features to make Cloud Messaging development easier.

    This tutorial is useful for the PHP developers who want to send FCM(Firebase Cloud Messaging) notifications by using PHP.

    To send GCM messages, you required two things:

    • Authorization key
    • Device Token

    You can get Authorization key(api_key) available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key, and device token you can get at the time of registering or logging by the android users through API.

    PHP Function to Send Push Notification by Using FCM(Firebase Cloud Messaging)

    function push_notification_android($device_id,$message){
    
        //API URL of FCM
        $url = 'https://fcm.googleapis.com/fcm/send';
    
        /*api_key available in:
        Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key*/
        $api_key = 'AAAAKZLje1I:APbGQDw8FD...TjmtuINVB-g';
                    
        $fields = array (
            'registration_ids' => array (
                    $device_id
            ),
            'data' => array (
                    "message" => $message
            )
        );
    
        //header includes Content type and api key
        $headers = array(
            'Content-Type:application/json',
            'Authorization:key='.$api_key
        );
                    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('FCM Send Error: ' . curl_error($ch));
        }
        curl_close($ch);
        return $result;
    }


    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.