Module Os_fcm_notif
Send push notifications to Android and iOS mobile devices.
This module provides a simple OCaml interface to Firebase Cloud Messaging (FCM) to send push notifications to Android and iOS mobile devices by using downstream HTTP messages in JSON.
You can find all information abou FCM at this address: https://firebase.google.com/docs/cloud-messaging/
Before using this module, you need to register your mobile application in FCM and save the server key FCM will give you. You need to pass this key to send when you want to send a notification.
On the client, you will need first to register the device on FCM. See
- for iOS: https://firebase.google.com/docs/cloud-messaging/ios/client
- for Android: https://firebase.google.com/docs/cloud-messaging/android/client
If you use this module to send push notifications to mobile devices created with ocsigen-start, you can use one of these plugins.
- cordova-plugin-fcm (binding ocaml-cordova-plugin-fcm).
- phonegap-plugin-push (binding ocaml-cordova-plugin-push-notifications). If you use one of them and if you want to add extra data, you need to use
Data.add_raw_stringorData.add_raw_jsondepending on the type of the value.
FCM works with tokens which represents a device. This token is used to target the device when you send a notification. The token is retrieved client-side.
To send a notification, you need to use send server_key notification ?data options where
notificationis of typeNotification.tand represents the notification payload in the JSON sent to FCM.datais an optional value of typeData.tand represents the data payload in the JSON sent to FCM. By default, it's empty.optionsis of typeOptions.tand represents options in the FCM documentation.
The type Options.t contains the list of registered ID you want to send the notification notification to. You can create a value of type Options.t with Options.create which needs a list of client ID. These ID's are the devices you want to send the notification to. You can add some parameters like priorities, restricted package name, condition, etc.
The type Notification.t contains the notification payloads. The description is given here: https://firebase.google.com/docs/cloud-messaging/http-server-ref
You can create an empty value of type Notification.t with Notification.empty. As described in the link given above, you can add a title, a body, etc to the notification. In general, to add the payload payload, you can use the function add_(payload). The notification value is at the end to be able to use the pipe. For example, to add a title and a message, you can use: <<code language="ocaml" |
Notification.empty () |>
add_title "Hello, World!" |>
add_body "Message to the world!"
>>
exception FCM_empty_responseexception FCM_no_json_response of stringexception FCM_missing_field of stringexception FCM_unauthorizedmodule Notification : sig ... endThis module provides an interface to create the JSON for the notification key.
module Data : sig ... endmodule Options : sig ... endmodule Response : sig ... endval send :
string ->
Notification.t ->
?data:Data.t ->
Options.t ->
Response.t Lwt.tsend server_key notification options