Question Detail

Firebase Cloud Messaging for Android using PHP

6 years ago Views 1693 Visit Post Reply

How to sent Notification from Server to Mobile?

I am trying to implement but not having the fine idea how to do it? I have created Json File and Google-services account. Please help me. Suggest me any Easy and accurate way to use Firebase Cloud Messaging.

 

 


Thread Reply

alex levine

- 4 years ago

You have to get some Key from Firebase Console Website

 

public function fcmSendNotification($title, $message, $type, $notificationIDs =array()){
        define( 'ACCESS_KEY', 'API_KEY_HERE');
        $registrationIds = $notification_ids;
        
        $messageArr = array
            ( 
                'title'         => $title,
                'message'       => $message,
                'summaryText'   => 'The internet is built on cat pictures',
                'click_action'  => 'FCM_PLUGIN_ACTIVITY',
                'vibrate'       => 1,
                'sound'         => 1,
                'type'          => $type
            );
     
        $fields = array
        (
            'registration_ids'  => $registrationIds,
            'data'              => $messageArr
           
        );
         
        $headers = array
        (
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
        );
         
        $ch = curl_init();
        curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
        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( $fields ) );
        $result = curl_exec($ch );
        curl_close( $ch );
        echo $result;
}