Google FCM HTTP v1 Delphi

From sgcWebSockets 2024.9.0 there is a new client component to Send Notifications using Google Firebase Cloud Messaging and the latest API HTTP v1.

Apps using the deprecated FCM legacy APIs for HTTP and XMPP should migrate to the HTTP v1 API at the earliest opportunity. Sending messages (including upstream messages) with those APIs was deprecated on June 20, 2023, and shutdown begins on July 22, 2024.

In addition to ongoing support and new features, the HTTP v1 API has these advantages over the legacy APIs:

- Better security via access tokens The HTTP v1 API uses short-lived access tokens according to the OAuth2 security model. In the event that an access token becomes public, it can only be maliciously used for an hour or so before it expires. Refresh tokens are not transmitted as often as the security keys used in the legacy API, so they are much less likely to be captured.

- More efficient customization of messages across platforms For the message body, the HTTP v1 API has common keys that go to all targeted instances, plus platform-specific keys that let you customize the message across platforms. This allows you to create "overrides" that send slightly different payloads to different client platforms in a single message.

- More extendable and future-proof for new client platform versions The HTTP v1 API fully supports messaging options available on Apple platforms, Android and Web. Since each platform has its own defined block in the JSON payload, FCM can extend the API to new versions and new platforms as needed.

Google FCM Delphi Client

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.

Authorization

Google FCM component client can login to Google Servers using the following methods:

  • gcaOAuth2: OAuth2 protocol
  • gcaJWT: JWT tokens.

OAuth2

The login is done using a webbrowser where the user logins with his own user and authorizes the FCM requests.

  • GoogleCloudOptions.OAuth2.ClientId: is the ClientID provided by Google to Authenticate through OAuth2 protocol.
  • GoogleCloudOptions.OAuth2.ClientSecret: is the Client Secret string provided by Google to Authenticate through OAuth2 protocol.
  • GoogleCloudOptions.OAuth2.Scope: is the scope of OAuth2, usually there is no need to modify the default value unless you need to get more access than default.
  • GoogleCloudOptions.OAuth2.LocalIP: OAuth2 protocol requires a server listening answer from Authentication server, this is the IP or DNS. By default is 127.0.0.1.
  • GoogleCloudOptions.OAuth2.LocalPort: Local server listening port.
  • GoogleCloudOptions.OAuth2.RedirectURL: if you need to set a redirect url different from LocalPort + LocalIP, you can set in this property (example: http://127.0.0.1:8080/oauth2).

Service Accounts

The login is done signing the requests using a private key provided by google, these method is recommended for automated services or applications without user interaction.

  • GoogleCloudOptions.JWT.ClientEmail: is the Client Email name provided creating the new service account. "client_email" node in the JSON configuration file.
  • GoogleCloudOptions.JWT.Subject: is the Client Email name provided creating the new service account. "client_email" node in the JSON configuration file.
  • GoogleCloudOptions.JWT.PrivateKeyId: is the Private Key Id provided by google. "private_key_id" node in the JSON configuration file.
  • GoogleCloudOptions.JWT.PrivateKey: is the Private Key certificate provided by google. "private_key" node in the JSON configuration file.

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. 

Sample

Find below a simple example to send notifications using FCM and JWT as authentication method.

oFCM := TsgcHTTPGoogleCloud_FCM_Client.Create(nil);
oFCM.GoogleCloudOptions.Authorization := gcaJWT;
oFCM.LoadSettingsFromFile('sgcfirebase-7x92e-5c80b8506a6c.json');
oFCM.GoogleCloudOptions.JWT.Subject := GoogleFCM.GoogleCloudOptions.JWT.ClientEmail;
ShowMessage(
  oFCM.SendMessage('Project-id', 
  '{"message": {"topic": "news","notification": ' +
  '{"title": "Breaking News",' + 
  '"body": "New news story available."},"data": {"story_id": "story_12345"}}}')
); 

Compiled Demo

sgcGoogle_FCM
2.8 mb
×
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

OpenAPI Parser Bundle Schemas

Related Posts