Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ getMessages: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/getMessages.cpp
createMessage: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/createMessage.cpp
@mkdir -p ./$(TESTS_DIR)
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/createMessage $(SRCS) $(EXAMPLES_DIR)/messaging/messages/createMessage.cpp $(LDFLAGS)
createFcmProvider: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/createFcmProvider.cpp
@mkdir -p ./$(TESTS_DIR)
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/createFcmProvider $(SRCS) $(EXAMPLES_DIR)/messaging/messages/createFcmProvider.cpp $(LDFLAGS)
deleteProvider: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/deleteProvider.cpp
@mkdir -p ./$(TESTS_DIR)
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/deleteProvider $(SRCS) $(EXAMPLES_DIR)/messaging/messages/deleteProvider.cpp $(LDFLAGS)
Expand All @@ -273,6 +276,7 @@ listTargets: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/listTargets.cpp
@mkdir -p ./$(TESTS_DIR)
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/listTargets $(SRCS) $(EXAMPLES_DIR)/messaging/messages/listTargets.cpp $(LDFLAGS)


# Messaging - Topics
getTopic: $(SRCS) $(EXAMPLES_DIR)/messaging/topics/getTopic.cpp
@mkdir -p ./$(TESTS_DIR)
Expand Down
25 changes: 25 additions & 0 deletions examples/messaging/messages/createFcmProvider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "Appwrite.hpp"
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
int main() {
std::string projectId = "68853010003a3f4fc106";
std::string apiKey = "";
std::string providerId = "68a22c7b00325882e4e5";
std::string name = "";
std::ifstream file("");
std::stringstream buffer;
buffer << file.rdbuf();
std::string service_account_json = buffer.str();
bool enabled = true;
try {
Messaging messaging(projectId, apiKey);
std::string response = messaging.createFcmProvider(
providerId, name, service_account_json, enabled);
std::cout << "FCM Provider Created!\nResponse: " << response << std::endl;
} catch (const AppwriteException &ex) {
std::cerr << "Exception: " << ex.what() << std::endl;
}
return 0;
}
12 changes: 12 additions & 0 deletions include/classes/Messaging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ class Messaging {
*/
std::string listProviders(Queries &queries);

/**
* @brief Create a new Firebase Cloud Messaging provider.
* @param providerId A unique Id for the provider.
* @param name provider name.
* @param service_account_json FCM service account JSON..
* @param enabled Whether the provider should be active immediately after creation.
* @return JSON response.
*/
std::string createFcmProvider(std::string &providerId, std::string name,
std::string service_account_json,
bool enabled);

/**
* @brief Delete a provider.
* @param providerId ID of the provider
Expand Down
30 changes: 30 additions & 0 deletions src/services/Messaging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,36 @@ std::string Messaging::updatePush(const std::string &messageId,
}
}

std::string Messaging::createFcmProvider(std::string &providerId,
std::string name,
std::string service_account_json,
bool enabled) {
if (providerId.empty()) {
throw AppwriteException("Missing required parameter: 'providerId'");
}
if (name.empty()) {
throw AppwriteException("Missing required parameter: 'name'");
}
std::string url = Config::API_BASE_URL + "/messaging/providers/fcm";
std::string payload =
R"({"providerId":")" + Utils::escapeJsonString(providerId) +
R"(","name":")" + Utils::escapeJsonString(name) +
R"(","serviceAccountJSON":)" + service_account_json + R"(,"enabled":)" +
(enabled ? "true" : "false") + R"(})";
std::vector<std::string> headers = Config::getHeaders(projectId);
headers.push_back("X-Appwrite-Key: " + apiKey);
headers.push_back("Content-Type: application/json");
std::string response;
int statusCode = Utils::postRequest(url, payload, headers, response);
if (statusCode == HttpStatus::CREATED) {
return response;
} else {
throw AppwriteException("Error Creating fcm provider. Status code: " +
std::to_string(statusCode) +
"\n\nResponse: " + response);
}
}

std::string Messaging::deleteProvider(const std::string &providerId) {
if (providerId.empty()) {
throw AppwriteException("Missing required parameter: providerId");
Expand Down
Loading