Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably send messages at no cost.
Using FCM, you can notify a client app that new email or other data is available to sync. You can send notification messages to drive user re-engagement and retention. For use cases such as instant messaging, a message can transfer a payload of up to 4096 bytes to a client app.
The component supports the HTTP v1 API.
Google FCM component client can login to Google Servers using the following methods:
OAuth2
The login is done using a web browser where the user logs in with their own account and authorizes the FCM requests.
Service Accounts
The login is done by signing the requests using a private key provided by Google. This method is recommended for automated services or applications without user interaction.
TLS Options
Firebase Cloud Messaging connections can be tuned through the TLSOptions property.
Example configuration:
oFCM := TsgcHTTPGoogleCloud_FCM_Client.Create(nil);
oFCM.TLSOptions.IOHandler := iohOpenSSL;
oFCM.TLSOptions.Version := tls1_3;
oFCM.TLSOptions.VerifyCertificate := True;
oFCM.TLSOptions.OpenSSL_Options.LibPath := oslpDefaultFolder;
When a new service account is created, you can download a JSON file with all configurations. This file can be processed by the FCM component, just call the method LoadSettingsFromFile and pass the JSON filename as argument.
OAuth2
In order to work with the Google FCM API, the sgcWebSockets FCM component uses OAuth2 as the default authentication method, so first you must set your ClientId and ClientSecret from your Google account.
oFCM:= TsgcHTTPGoogleCloud_FCM_Client.Create(nil);
oFCM.GoogleCloudOptions.Authorization := gcaOAuth2;
oFCM.GoogleCloudOptions.OAuth2.ClientId := '... your google client id...';
oFCM.GoogleCloudOptions.OAuth2.ClientSecret := '... your google client secret...';
Service Accounts
Service Accounts require building a JWT and passing it as an authorization token.
oFCM := TsgcHTTPGoogleCloud_FCM_Client.Create(nil);
oFCM.GoogleCloudOptions.Authorization := gcaJWT;
oFCM.GoogleCloudOptions.JWT.ClientEmail := '...google email...';
oFCM.GoogleCloudOptions.JWT.Subject := '...google email...';
oFCM.GoogleCloudOptions.JWT.PrivateKeyId := '...private key id...';
oFCM.GoogleCloudOptions.JWT.PrivateKey.Lines.Text := '...private key certificate...';
This is required in order to get an Authorization Token Key from Google which will be used for all Rest API calls.
All methods return a response, which may be successful or return an error.
Use the method SendMessage to send a notification message. The function has 2 arguments:
{
"message": {
"topic": "news",
"notification": {
"title": "Breaking News",
"body": "New news story available."
},
"data": {
"story_id": "story_12345"
}
}
}