Skip to content

Commit a24aabc

Browse files
authored
Merge pull request #3 from davirxavier/feature/fcm-notifications
add fcm notification docs
2 parents 4768efa + 96a1ca6 commit a24aabc

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

README.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,88 @@ This function must be called in the `loop()` of the Arduino program. It continuo
196196
197197
#### `ember.channelWrite(channel, value)`
198198
This function sends data to a specific channel in the Firebase Realtime Database. The first argument, `channel`, is the channel number (e.g., `EMBER_BUTTON_OFF`), and the second argument, `value`, is the data being sent. In the example, it's used to update the channel with the button status (e.g., turning the button OFF).
199+
200+
### FCM Notifications
201+
202+
The EmberIoT library also supports **Firebase Cloud Messaging (FCM)** for sending push notifications directly from your microcontroller to registered users or devices.
203+
This allows your IoT device to send alerts, status updates, or messages to your mobile app or other connected devices through Firebase.
204+
205+
To learn how to configure your Firebase project and service account, refer to the [Firebase project setup tutorial](FIREBASE_SETUP.md).
206+
207+
#### Example: Sending a Simple Notification
208+
209+
````C++
210+
#include <Arduino.h>
211+
#include <EmberIotNotifications.h>
212+
213+
// Your Firebase service account email (created during setup)
214+
const char accountEmail[] = "[email protected]";
215+
216+
// The private key associated with the service account
217+
const char privateKey[] = "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n";
218+
219+
// The unique Firebase Authentication User UID that will receive the notification
220+
const char userUid[] = "MNvs5SUXkRda1sSYA6bjvKAPpvR2";
221+
222+
// Create an FCMEmberNotifications instance
223+
FCMEmberNotifications notifications(accountEmail, privateKey);
224+
225+
void setup()
226+
{
227+
// Initialize the notifications instance with the recipient user UID
228+
notifications.init(userUid);
199229
230+
// (Optional) Set additional information for this device.
231+
// The first argument is the EmberIot Device ID — this lets the app know which device sent the notification.
232+
// The second argument is a title prefix for all notifications from this device.
233+
notifications.setAdditionalDeviceInfo("-ORcOt-eYeGOW8mi4XqK", "My Device");
234+
235+
// Send a notification with a title and body message
236+
notifications.send("Test", "Hello World!");
237+
}
238+
239+
void loop()
240+
{
241+
// Maintain the notification service connection and handle background tasks
242+
notifications.loop();
243+
}
244+
````
245+
246+
Below is a brief overview of the key functions used in the example code:
247+
248+
#### `FCMEmberNotifications(accountEmail, privateKey)`
249+
This is the constructor that initializes the `FCMEmberNotifications` instance using your Firebase service account credentials.
250+
It authenticates your device with Firebase Cloud Messaging, allowing it to send push notifications.
251+
252+
- `accountEmail`: The service account email created in your Firebase project.
253+
- `privateKey`: The private key string associated with the service account (in PEM format).
254+
255+
#### `notifications.init(userUid)`
256+
This function initializes the FCM notification service and defines the recipient of the messages.
257+
258+
- `userUid`: The Firebase Authentication User UID of the recipient. This is the unique user identifier from your Firebase project's Authentication section.
259+
260+
> **Note:**
261+
> If you are already using other EmberIot functionality (such as the Data Channels),
262+
> you should call `notifications.init()` **passing your existing `EmberIot` instance instead of the `userUid`**.
263+
> This automatically links the notifications to the already initialized EmberIot instance, decreasing memory usage.
264+
265+
#### `notifications.setAdditionalDeviceInfo(deviceId, titlePrefix)`
266+
This optional function adds extra information about the device sending notifications.
267+
268+
- `deviceId`: The `DEVICE_ID` registered for this board in EmberIot. This allows the receiving app to know which device sent the notification.
269+
- `titlePrefix`: A short text added as a prefix to every notification title sent from this device, helping distinguish messages between multiple devices.
270+
271+
#### `notifications.send(title, body)`
272+
This function sends a Firebase Cloud Messaging notification with a specified title and message body to the user defined in `notifications.init()`.
273+
274+
- `title`: The title of the notification displayed to the user.
275+
- `body`: The message content shown in the notification.
276+
277+
#### `notifications.loop()`
278+
This function must be called inside the main `loop()` of your Arduino program.
279+
It maintains the connection to the Firebase Cloud Messaging service and processes any pending background tasks required for notifications to work correctly.
280+
200281
## How it Works - What even is a Data Channel?
201282
202283
In this library, a data channel represents a stream of data received from a specific location in the Firebase Realtime Database.
@@ -211,4 +292,4 @@ To send updates, the library uses the same REST API to perform HTTP requests—m
211292
212293
- [X] Compatibility with ESP8266
213294
- [ ] Add option for LAN communication, for when there's no network connection
214-
- [ ] Notification service
295+
- [X] Notification service

0 commit comments

Comments
 (0)