showSuggestion method
Implementation
Future<void> showSuggestion(String msg, String msgid) async {
await createNotificationChannel();
// Clean and decode JSON string
String jsonString = msg;
if (jsonString.startsWith('"') && jsonString.endsWith('"')) {
jsonString = jsonString.substring(1, jsonString.length - 1);
}
// Decode JSON string
jsonString = jsonString
.replaceAll(r'\"', '"')
.replaceAll(r'\\', '\\')
.replaceAll(r'\/', '/')
.replaceAll(r'\b', '\b')
.replaceAll(r'\f', '\f')
.replaceAll(r'\n', '\n')
.replaceAll(r'\r', '\r')
.replaceAll(r'\t', '\t');
late String suggestionMsg;
late List<String> optionsArray;
try {
final Map<String, dynamic> jsonObject = json.decode(jsonString);
suggestionMsg = jsonObject['message'] ?? '';
String suggestionOptions = jsonObject['option'] ?? '';
optionsArray =
suggestionOptions.split(',').map((option) => option.trim()).toList();
// print("Parsed Options: $optionsArray");
} catch (e) {
print("Error parsing JSON: $e");
return; // Exit if JSON parsing fails
}
// Create notification channel
const String channelId = "suggestion_channel";
const AndroidNotificationChannel channel = AndroidNotificationChannel(
channelId,
'Suggestion Notifications',
description: 'Channel for suggestion notifications',
importance: Importance.high,
playSound: true,
);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
// Create a list of actions for each option
/* List<AndroidNotificationAction> actions = [];
int maxActions = optionsArray.length > 3 ? 3 : optionsArray.length;
for (int i = 0; i < maxActions; i++) {
String trimmedOption = optionsArray[i];
actions.add(AndroidNotificationAction(
'action_$i',
trimmedOption,
));
}*/
// Create notification details with actions
/* NotificationDetails platformChannelSpecifics = NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channelDescription: channel.description,
importance: Importance.high,
priority: Priority.high,
icon: '@mipmap/notify_adaptive_fore',
actions: actions,
// Add actions here
styleInformation: BigTextStyleInformation(
suggestionMsg,
htmlFormatBigText: true,
contentTitle: 'New Suggestion',
summaryText: 'Tap to see options',
),
),
);*/
NotificationDetails platformChannelSpecifics = NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channelDescription: channel.description,
importance: Importance.high,
priority: Priority.high,
icon: '@mipmap/notify_adaptive_fore',
styleInformation: BigTextStyleInformation(
suggestionMsg,
htmlFormatBigText: true,
contentTitle: 'New Suggestion',
summaryText: 'Tap to see options',
),
),
);
// Store msgid and type in the payload for later retrieval
/*String payload =
jsonEncode({'msg': jsonString, 'msgid': msgid, 'type': 'Suggestion'});
*/
String payload = jsonEncode({
'msg': jsonString,
'msgid': msgid,
'options': optionsArray,
'type': 'Suggestion'
});
await flutterLocalNotificationsPlugin.show(
msgid.hashCode,
"New Suggestion",
suggestionMsg,
platformChannelSpecifics,
payload: payload, // Pass the entire JSON string as payload if needed
);
}