checkForMessages method
Implementation
Future<void> checkForMessages(String username) async {
try {
final response = await http.get(Uri.parse(
'https://apn1.leewayapi.in/messages.php?username=$username'));
if (response.statusCode == 200) {
// Check if the response body is empty
if (response.body.isEmpty) {
print("Received empty response body.");
return;
}
try {
final jsonResponse = jsonDecode(response.body);
print("Received jsonResponse ========> $jsonResponse");
if (jsonResponse['success'] == 1) {
List notifications = jsonResponse['notification'];
for (var notification in notifications) {
String fromJid = notification['from_jid'];
String msg = notification['msg'];
String type = notification['type'];
String redirectUrl = notification['redirect_url'];
String msgId = notification['msg_id'];
await readMessages(username, msgId);
switch (type) {
case "Text":
await notificationHelper.showTextNotification(
"New Notification",
msg,
msgId,
redirectUrl,
);
break;
case "URL":
await notificationHelper.showURLNotification(
"New Notification",
"Please Click The Link for More Details: ${msg.split("&&")[1]}",
msg.split("&&")[1],
msgId,
);
break;
case "Image":
await notificationHelper.showImageNotification(
msg, msgId, redirectUrl);
break;
case "Live Activities":
await notificationHelper.showLiveActivitiesNotification(
msg,
msgId,
redirectUrl,
);
break;
case "Rich Card":
await notificationHelper.showRichCard(msg, msgId);
break;
case "Suggestion":
await notificationHelper.showSuggestion(msg, msgId);
break;
default:
print("Unknown notification type: $type");
}
}
} else {
print("No notifications found or success flag is not 1");
}
} catch (e) {
print("Error parsing JSON: $e");
}
} else {
print("Failed to fetch messages: ${response.statusCode}");
}
} catch (e) {
print("Error checking for messages: $e");
}
}