Skip to content

Commit a122ab9

Browse files
committed
Add supports FCM App Instances management
1 parent a6071f6 commit a122ab9

18 files changed

Lines changed: 990 additions & 78 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ The details for all classes used in this library are available. Click the follow
174174

175175
- [Messaging Class](resources/docs/messaging.md)
176176

177+
- [MessagingInstance Class](resources/docs/messaging_instance.md)
178+
177179
- [Storage Class](resources/docs/storage.md)
178180

179181
- [CloudStorage Class](resources/docs/cloud_storage.md)
@@ -722,4 +724,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
722724

723725
`THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.`
724726

725-
*Last updated 2025-08-09 UTC.*
727+
*Last updated 2025-09-16 UTC.*
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/**
2+
* The example to subscribe or unsubscribe the Cloud Messaging topic.
3+
*
4+
* This example uses the ServiceAuth class for authentication.
5+
* See examples/App/AppInitialization for more authentication examples.
6+
*
7+
* You can test this example with Firebase Cloud Messaging Web Client App in /examples/Messaging/WebClient.
8+
*
9+
* For Google Instance ID REST API reference documentation, please visit
10+
* https://developers.google.com/instance-id/reference/server#manage_relationship_maps_for_multiple_app_instances
11+
*
12+
*
13+
* For the complete usage guidelines, please read README.md or visit https://github.com/mobizt/FirebaseClient
14+
*/
15+
16+
#define ENABLE_SERVICE_AUTH
17+
#define ENABLE_MESSAGING
18+
19+
#include <FirebaseClient.h>
20+
#include "ExampleFunctions.h" // Provides the functions used in the examples.
21+
22+
#define WIFI_SSID "WIFI_AP"
23+
#define WIFI_PASSWORD "WIFI_PASSWORD"
24+
25+
#define FIREBASE_PROJECT_ID "PROJECT_ID"
26+
#define FIREBASE_CLIENT_EMAIL "CLIENT_EMAIL"
27+
const char PRIVATE_KEY[] PROGMEM = "-----BEGIN PRIVATE KEY-----XXXXXXXXXXXX-----END PRIVATE KEY-----\n";
28+
29+
#define FIREBASE_FCM_DEVICE_TOKEN_1 "IID_TOKEN_1"
30+
#define FIREBASE_FCM_DEVICE_TOKEN_2 "IID_TOKEN_2"
31+
32+
void processData(AsyncResult &aResult);
33+
34+
// ServiceAuth is required for Google Cloud Functions functions.
35+
ServiceAuth sa_auth(FIREBASE_CLIENT_EMAIL, FIREBASE_PROJECT_ID, PRIVATE_KEY, 3000 /* expire period in seconds (<= 3600) */);
36+
37+
FirebaseApp app;
38+
39+
SSL_CLIENT ssl_client;
40+
41+
using AsyncClient = AsyncClientClass;
42+
AsyncClient aClient(ssl_client);
43+
44+
MessagingInstance fcmInstance;
45+
AsyncResult fcmResult;
46+
47+
bool taskComplete = false;
48+
49+
void setup()
50+
{
51+
Serial.begin(115200);
52+
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
53+
54+
Serial.print("Connecting to Wi-Fi");
55+
while (WiFi.status() != WL_CONNECTED)
56+
{
57+
Serial.print(".");
58+
delay(300);
59+
}
60+
Serial.println();
61+
Serial.print("Connected with IP: ");
62+
Serial.println(WiFi.localIP());
63+
Serial.println();
64+
65+
Firebase.printf("Firebase Client v%s\n", FIREBASE_CLIENT_VERSION);
66+
67+
set_ssl_client_insecure_and_buffer(ssl_client);
68+
69+
// Assign the valid time only required for authentication process with ServiceAuth and CustomAuth.
70+
app.setTime(get_ntp_time());
71+
72+
Serial.println("Initializing app...");
73+
initializeApp(aClient, app, getAuth(sa_auth), auth_debug_print, "🔐 authTask");
74+
75+
// Or intialize the app and wait.
76+
// initializeApp(aClient, app, getAuth(sa_auth), 120 * 1000, auth_debug_print);
77+
78+
app.getApp<MessagingInstance>(fcmInstance);
79+
}
80+
81+
void loop()
82+
{
83+
// To maintain the authentication and async tasks
84+
app.loop();
85+
86+
if (app.ready() && !taskComplete)
87+
{
88+
taskComplete = true;
89+
90+
object_t iids;
91+
JsonWriter writer;
92+
iids.initArray();
93+
94+
writer.join(iids, 2 /* no. of object_t (s) to join */, object_t(string_t(FIREBASE_FCM_DEVICE_TOKEN_1)), object_t(string_t(FIREBASE_FCM_DEVICE_TOKEN_2)));
95+
96+
Serial.println("Subsribing the topics...");
97+
98+
fcmInstance.batchAdd(aClient, "test_topic", iids, processData, "fcmSubscribeTopicTask");
99+
100+
// Serial.println("Unsubsribing the topics...");
101+
// fcmInstance.batchRemove(aClient, "my_fcm_topic", iids, processData, "fcmUnsubscribeTopicTask");
102+
}
103+
104+
// For async call with AsyncResult.
105+
processData(fcmResult);
106+
}
107+
108+
void processData(AsyncResult &aResult)
109+
{
110+
// Exits when no result is available when calling from the loop.
111+
if (!aResult.isResult())
112+
return;
113+
114+
if (aResult.isEvent())
115+
{
116+
Firebase.printf("Event task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.eventLog().message().c_str(), aResult.eventLog().code());
117+
}
118+
119+
if (aResult.isDebug())
120+
{
121+
Firebase.printf("Debug task: %s, msg: %s\n", aResult.uid().c_str(), aResult.debug().c_str());
122+
}
123+
124+
if (aResult.isError())
125+
{
126+
Firebase.printf("Error task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.error().message().c_str(), aResult.error().code());
127+
}
128+
129+
if (aResult.available())
130+
{
131+
Firebase.printf("task: %s, payload: %s\n", aResult.uid().c_str(), aResult.c_str());
132+
}
133+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
* The example to get information about app instances.
3+
*
4+
* This example uses the ServiceAuth class for authentication.
5+
* See examples/App/AppInitialization for more authentication examples.
6+
*
7+
* You can test this example with Firebase Cloud Messaging Web Client App in /examples/Messaging/WebClient.
8+
*
9+
* For Google Instance ID REST API reference documentation, please visit
10+
* https://developers.google.com/instance-id/reference/server#get_information_about_app_instances
11+
*
12+
*
13+
* For the complete usage guidelines, please read README.md or visit https://github.com/mobizt/FirebaseClient
14+
*/
15+
16+
#define ENABLE_SERVICE_AUTH
17+
#define ENABLE_MESSAGING
18+
19+
#include <FirebaseClient.h>
20+
#include "ExampleFunctions.h" // Provides the functions used in the examples.
21+
22+
#define WIFI_SSID "WIFI_AP"
23+
#define WIFI_PASSWORD "WIFI_PASSWORD"
24+
25+
#define FIREBASE_PROJECT_ID "PROJECT_ID"
26+
#define FIREBASE_CLIENT_EMAIL "CLIENT_EMAIL"
27+
const char PRIVATE_KEY[] PROGMEM = "-----BEGIN PRIVATE KEY-----XXXXXXXXXXXX-----END PRIVATE KEY-----\n";
28+
29+
#define FIREBASE_FCM_DEVICE_TOKEN_1 "IID_TOKEN_1"
30+
31+
void processData(AsyncResult &aResult);
32+
33+
// ServiceAuth is required for Google Cloud Functions functions.
34+
ServiceAuth sa_auth(FIREBASE_CLIENT_EMAIL, FIREBASE_PROJECT_ID, PRIVATE_KEY, 3000 /* expire period in seconds (<= 3600) */);
35+
36+
FirebaseApp app;
37+
38+
SSL_CLIENT ssl_client;
39+
40+
using AsyncClient = AsyncClientClass;
41+
AsyncClient aClient(ssl_client);
42+
43+
MessagingInstance fcmInstance;
44+
AsyncResult fcmResult;
45+
46+
bool taskComplete = false;
47+
48+
void setup()
49+
{
50+
Serial.begin(115200);
51+
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
52+
53+
Serial.print("Connecting to Wi-Fi");
54+
while (WiFi.status() != WL_CONNECTED)
55+
{
56+
Serial.print(".");
57+
delay(300);
58+
}
59+
Serial.println();
60+
Serial.print("Connected with IP: ");
61+
Serial.println(WiFi.localIP());
62+
Serial.println();
63+
64+
Firebase.printf("Firebase Client v%s\n", FIREBASE_CLIENT_VERSION);
65+
66+
set_ssl_client_insecure_and_buffer(ssl_client);
67+
68+
// Assign the valid time only required for authentication process with ServiceAuth and CustomAuth.
69+
app.setTime(get_ntp_time());
70+
71+
Serial.println("Initializing app...");
72+
initializeApp(aClient, app, getAuth(sa_auth), auth_debug_print, "🔐 authTask");
73+
74+
// Or intialize the app and wait.
75+
// initializeApp(aClient, app, getAuth(sa_auth), 120 * 1000, auth_debug_print);
76+
77+
app.getApp<MessagingInstance>(fcmInstance);
78+
}
79+
80+
void loop()
81+
{
82+
// To maintain the authentication and async tasks
83+
app.loop();
84+
85+
if (app.ready() && !taskComplete)
86+
{
87+
taskComplete = true;
88+
89+
Serial.println("Get information about app instances...");
90+
91+
fcmInstance.get(aClient, FIREBASE_FCM_DEVICE_TOKEN_1, processData, "fcmGetInstanceInfoTask");
92+
}
93+
94+
// For async call with AsyncResult.
95+
processData(fcmResult);
96+
}
97+
98+
void processData(AsyncResult &aResult)
99+
{
100+
// Exits when no result is available when calling from the loop.
101+
if (!aResult.isResult())
102+
return;
103+
104+
if (aResult.isEvent())
105+
{
106+
Firebase.printf("Event task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.eventLog().message().c_str(), aResult.eventLog().code());
107+
}
108+
109+
if (aResult.isDebug())
110+
{
111+
Firebase.printf("Debug task: %s, msg: %s\n", aResult.uid().c_str(), aResult.debug().c_str());
112+
}
113+
114+
if (aResult.isError())
115+
{
116+
Firebase.printf("Error task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.error().message().c_str(), aResult.error().code());
117+
}
118+
119+
if (aResult.available())
120+
{
121+
Firebase.printf("task: %s, payload: %s\n", aResult.uid().c_str(), aResult.c_str());
122+
}
123+
}

0 commit comments

Comments
 (0)