function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kirtish ShrotriyaKirtish Shrotriya 

How to integrate apex with firebase cloud messaging(FCM) to send push notification to react native apps?

I want to send a push notification to a react native apps for that, I need to integrate my apex server with FCM. Can you help me that what steps I need to follow to integrate them?

Thanks in advance.
Kirtish ShrotriyaKirtish Shrotriya
HttpGet
    global static String SendFCMNotification(){
        
        //Your logic here for now I am using hardcoeded values

        string deviceToken = 'dFjcNEwg_PY:APA9......... '; //your device token
        string msg ='test msg';
        string title =' Hello test 123';
       
        Http http = new Http(); 
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://fcm.googleapis.com/fcm/send');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request.setHeader('Authorization', 'Key=AIzaSyDzQd.......'); //legacy server key

        
        // Set the body as a JSON object
        request.setBody('{"to":"'+deviceToken+'", "notification":{"body":"'+msg+'", "title":"'+title+'"} }');
        HttpResponse response = http.send(request);
        
        // Parse the JSON response
        if (response.getStatusCode() != 200) {
            System.debug('The status code returned was not expected: ' +
                         response.getStatusCode() + ' ' + response.getStatus());
            return string.valueOf(response.getStatus());
        } else {
            System.debug(response.getBody());
            return string.valueOf(response.getStatus());
        }
    }

you can use the above code as a reference to call the API of Firebase, where you need to add your mobile application.