Skip to content

Commit 9f25a0c

Browse files
authored
feat: updatePush (#102)
* feat: API to update the push message in the console. * feat: API to update the push message in the console. * feat: API to update the push message in the console. * feat: API to update the push message in the console. * feat: API to update push message in console * Add Docs in Messaging.hpp
1 parent 2125d40 commit 9f25a0c

5 files changed

Lines changed: 159 additions & 9 deletions

File tree

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ getQueueMigrations: $(SRCS) $(EXAMPLES_DIR)/health/params/getQueueMigrations.cpp
242242
createPush: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/createPush.cpp
243243
@mkdir -p ./$(TESTS_DIR)
244244
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/createPush $(SRCS) $(EXAMPLES_DIR)/messaging/messages/createPush.cpp $(LDFLAGS)
245-
245+
updatePush: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/updatePush.cpp
246+
@mkdir -p ./$(TESTS_DIR)
247+
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/updatePush $(SRCS) $(EXAMPLES_DIR)/messaging/messages/updatePush.cpp $(LDFLAGS)
246248
listMessages: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/listMessages.cpp
247249
@mkdir -p ./$(TESTS_DIR)
248250
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/listMessages $(SRCS) $(EXAMPLES_DIR)/messaging/messages/listMessages.cpp $(LDFLAGS)

examples/messaging/messages/createPush.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ int main() {
77
std::string projectId = "";
88
std::string apiKey = "";
99
Appwrite appwrite(projectId, apiKey);
10-
std::string topicId = "67b3048600077f40b8a7";
10+
std::vector<std::string> topicId = {""};
11+
std::vector<std::string> userId = {""};
1112

1213
try {
1314
std::string response = appwrite.getMessaging().createPush(
14-
"unique()", "Title ", "Body", topicId);
15+
"unique()", "Title ", "Body", topicId, userId, true);
1516

1617
std::cout << "Push notification created: " << response << std::endl;
1718
} catch (const AppwriteException &e) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "Appwrite.hpp"
2+
#include "classes/Messaging.hpp"
3+
#include <iostream>
4+
5+
int main() {
6+
std::string projectId = "";
7+
std::string apiKey = "";
8+
9+
std::vector<std::string> topicId = {""};
10+
std::string messageId = "";
11+
std::vector<std::string> userId = {""};
12+
13+
Appwrite appwrite(projectId, apiKey);
14+
15+
try {
16+
std::string response = appwrite.getMessaging().updatePush(
17+
messageId, "Updated Title", "Updated Body", topicId, userId);
18+
19+
std::cout << "Push notification updated successfully:\n" << response << std::endl;
20+
} catch (const AppwriteException &e) {
21+
std::cerr << "Appwrite error: " << e.what() << std::endl;
22+
}
23+
24+
return 0;
25+
}

include/classes/Messaging.hpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,28 @@ class Messaging {
116116
const std::string &name,
117117
const std::string &targetId,
118118
const std::string &subscriberId);
119+
/**
120+
* @brief Creates a new push notification message.
121+
*
122+
* Sends a push notification to specified users, topics, or both.
123+
*
124+
* @param messageId A unique Id for the message.
125+
* @param title Title of the push notification.
126+
* @param body Body content of the push notification.
127+
* @param topicId A list of topic IDs to which the notification should be sent.
128+
* @param userId A list of user IDs to which the notification should be sent.
129+
* @param draft If true, saves the message as a draft.
130+
*
131+
* @return JSON response.
132+
*/
119133
std::string createPush(const std::string &messageId,
120134
const std::string &title,
121135
const std::string &body,
122-
const std::string &topicId);
123-
/**
136+
const std::vector<std::string> &topicId= {},
137+
const std::vector<std::string> &userId = {},
138+
bool draft = false);
139+
140+
/**
124141
* @brief Create a new email message.
125142
*
126143
* Sends a new email message to specific topics and/or target recipients.
@@ -137,7 +154,26 @@ class Messaging {
137154
const std::string& subject,
138155
const std::string& content,
139156
const std::vector<std::string>& topics = {},
140-
const std::vector<std::string>& targets = {});
157+
const std::vector<std::string>& targets = {});
158+
159+
/**
160+
* @brief Updates an existing push notification message.
161+
*
162+
* Modifies the title and body of an existing push message.
163+
*
164+
* @param messageId The ID of the message to update.
165+
* @param title New title of the push notification.
166+
* @param body New body content of the push notification.
167+
* @param topicId List of topic IDs to update the message.
168+
* @param userId List of user IDs to update the message.
169+
* @return JSON response
170+
*/
171+
std::string updatePush(const std::string &messageId,
172+
const std::string &title,
173+
const std::string &body,
174+
const std::vector<std::string> &topicId = {},
175+
const std::vector<std::string> &userId = {});
176+
141177

142178
private:
143179
std::string projectId; ///< Project ID

src/services/Messaging.cpp

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,9 @@ std::string Messaging::createSubscribers(const std::string &topicId,
344344
std::string Messaging::createPush(const std::string &messageId,
345345
const std::string &title,
346346
const std::string &body,
347-
const std::string &topicId){
347+
const std::vector<std::string> &topicId,
348+
const std::vector<std::string> &userId,
349+
bool draft){
348350
if (messageId.empty()) {
349351
throw AppwriteException("Missing required parameter: 'messageId'");
350352
}
@@ -361,13 +363,34 @@ std::string Messaging::createPush(const std::string &messageId,
361363
throw AppwriteException("Missing required parameter: 'topicId'");
362364
}
363365

366+
if (userId.empty()) {
367+
throw AppwriteException("Missing required parameter: 'userId'");
368+
}
369+
370+
std::string topicIdJson = "[";
371+
for (size_t i = 0; i < topicId.size(); ++i) {
372+
topicIdJson += "\"" + Utils::escapeJsonString(topicId[i]) + "\"";
373+
if (i < topicId.size() - 1) topicIdJson += ",";
374+
}
375+
topicIdJson += "]";
376+
377+
std::string userIdJson = "[";
378+
for (size_t i = 0; i < userId.size(); ++i) {
379+
userIdJson += "\"" + Utils::escapeJsonString(userId[i]) + "\"";
380+
if (i < userId.size() - 1) userIdJson += ",";
381+
}
382+
userIdJson += "]";
383+
384+
364385
std::string url = Config::API_BASE_URL + "/messaging/messages/push";
365-
366386
std::string payload =
367387
R"({"messageId":")" + Utils::escapeJsonString(messageId) +
368388
R"(","title":")" + Utils::escapeJsonString(title) +
369389
R"(","body":")" + Utils::escapeJsonString(body) +
370-
R"(","topics": [")" + Utils::escapeJsonString(topicId) + R"("]})";
390+
R"(","topicId":)" + topicIdJson +
391+
R"(,"userId":)" + userIdJson +
392+
R"(,"draft":)" + (draft ? "true" : "false") +
393+
"}";
371394

372395
std::vector<std::string> headers = Config::getHeaders(projectId);
373396
headers.push_back("X-Appwrite-Key: " + apiKey);
@@ -434,6 +457,7 @@ std::string Messaging::createMessage(const std::string& messageId,
434457
headers.push_back("Content-Type: application/json");
435458

436459
std::string response;
460+
437461
int statusCode = Utils::postRequest(url, payload, headers, response);
438462

439463
if (statusCode == HttpStatus::CREATED || statusCode == HttpStatus::OK) {
@@ -442,4 +466,66 @@ std::string Messaging::createMessage(const std::string& messageId,
442466
throw AppwriteException("Error creating email message. Status code: " +
443467
std::to_string(statusCode) + "\n\nResponse: " + response);
444468
}
469+
}
470+
471+
std::string Messaging::updatePush(const std::string &messageId,
472+
const std::string &title,
473+
const std::string &body,
474+
const std::vector<std::string> &topicId,
475+
const std::vector<std::string> &userId) {
476+
if (messageId.empty()) {
477+
throw AppwriteException("Missing required parameter: 'messageId'");
478+
}
479+
480+
if (title.empty()) {
481+
throw AppwriteException("Missing required parameter: 'title'");
482+
}
483+
484+
if (body.empty()) {
485+
throw AppwriteException("Missing required parameter: 'body'");
486+
}
487+
488+
if (topicId.empty()) {
489+
throw AppwriteException("Missing required parameter: 'topicId'");
490+
}
491+
492+
if (userId.empty()) {
493+
throw AppwriteException("Missing required parameter: 'userId'");
494+
}
495+
496+
std::string topicIdJson = "[";
497+
for (size_t i = 0; i < topicId.size(); ++i) {
498+
topicIdJson += "\"" + Utils::escapeJsonString(topicId[i]) + "\"";
499+
if (i < topicId.size() - 1) topicIdJson += ",";
500+
}
501+
topicIdJson += "]";
502+
503+
std::string userIdJson = "[";
504+
for (size_t i = 0; i < userId.size(); ++i) {
505+
userIdJson += "\"" + Utils::escapeJsonString(userId[i]) + "\"";
506+
if (i < userId.size() - 1) userIdJson += ",";
507+
}
508+
userIdJson += "]";
509+
510+
std::string url = Config::API_BASE_URL + "/messaging/messages/push/" + messageId;
511+
std::string payload =
512+
R"({"title":")" + Utils::escapeJsonString(title) +
513+
R"(","body":")" + Utils::escapeJsonString(body) +
514+
R"(","topicId":)" + topicIdJson +
515+
R"(,"userId":)" + userIdJson +
516+
"}";
517+
std::vector<std::string> headers = Config::getHeaders(projectId);
518+
headers.push_back("X-Appwrite-Key: " + apiKey);
519+
headers.push_back("Content-Type: application/json");
520+
std::string response;
521+
522+
int statusCode = Utils::patchRequest(url, payload, headers, response);
523+
524+
if (statusCode == HttpStatus::OK) {
525+
return response;
526+
} else {
527+
throw AppwriteException(
528+
"Error updating push message. Status code: " + std::to_string(statusCode) +
529+
"\n\nResponse: " + response);
530+
}
445531
}

0 commit comments

Comments
 (0)