|
| 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 | +} |
0 commit comments