handleNotificationClick method

Future<void> handleNotificationClick(
  1. String? jsonPayload
)

Implementation

Future<void> handleNotificationClick(String? jsonPayload) async {
  if (jsonPayload == null) return;

  // Decode the JSON payload to retrieve URL, msgid, and type
  Map<String, dynamic> payloadData = jsonDecode(jsonPayload);

  // Retrieve values with null checks
  final String? url = payloadData['url'];
  final String? msgid = payloadData['msgid'];
  final String? type = payloadData['type'];
  final String? action = payloadData['action']; // Get the action

  // Debugging: Log the values
  // print("PAYLOADDATA+++++++++++++" + payloadData.toString());
  // print("URL: $url, MsgID: $msgid, Type: $type, Action: $action");

  // Check for null values
  if (msgid == null || type == null) {
    debugPrint("Error: One or more required fields are null.");
    return; // Exit the function if any required field is null
  }

  // Check if the notification is of type "Suggestion"
  if (type == "Suggestion") {


    List<String> options = List<String>.from(
        payloadData['options']); // Get all options from payload
    String? selectedOption;

    // Show alert dialog with all options
    showDialog(
      context: navigatorKey.currentContext!,
      builder: (BuildContext context) {
        return Dialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(16.0), // Softer corners
          ),
          elevation: 30, // Increased elevation for a more pronounced shadow
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return Container(
                padding: const EdgeInsets.symmetric(
                    vertical: 20.0, horizontal: 24.0),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    const Text(
                      'Select an Option',
                      style: TextStyle(
                        fontSize: 24,
                        fontWeight: FontWeight.bold,
                        color: Colors.black, // Black text for contrast
                      ),
                    ),
                    const SizedBox(height: 20),
                    SingleChildScrollView(
                      child: Column(
                        children: options.map((option) {
                          return CheckboxListTile(
                            title: Text(
                              option,
                              style: const TextStyle(
                                  color: Colors.black87, fontSize: 20),
                            ),
                            value: selectedOption == option,
                            onChanged: (bool? value) {
                              // Update the selected option
                              setState(() {
                                selectedOption = value! ? option : null;
                              });
                            },
                            controlAffinity: ListTileControlAffinity
                                .leading, // Checkbox on the left
                          );
                        }).toList(),
                      ),
                    ),
                    const SizedBox(height: 20),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        TextButton(
                          style: TextButton.styleFrom(
                            textStyle: const TextStyle(
                              fontSize: 20,
                              fontWeight: FontWeight.bold,
                            ),
                            foregroundColor: Colors.redAccent,
                            backgroundColor: Colors.white,
                            padding: const EdgeInsets.symmetric(
                                vertical: 12.0, horizontal: 24.0),
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(
                                  12.0), // Rounded corners for button
                            ),
                          ),
                          child: const Text('Cancel'),
                          onPressed: () {
                            Navigator.of(context).pop(); // Close the dialog
                          },
                        ),
                        TextButton(
                          style: TextButton.styleFrom(
                            textStyle: const TextStyle(
                              fontSize: 20,
                              fontWeight: FontWeight.bold,
                            ),
                            foregroundColor: Colors.green,
                            backgroundColor: Colors.white,
                            padding: const EdgeInsets.symmetric(
                                vertical: 12.0, horizontal: 24.0),
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(
                                  12.0), // Rounded corners for button
                            ),
                          ),
                          child: const Text(
                            'OK',
                          ),
                          onPressed: () async {
                            if (selectedOption != null) {
                              Navigator.of(context).pop(); // Close the dialog
                              showSelectedOptionDialog(
                                  selectedOption!); // Show selected option dialog

                              // Concatenate type and selectedOption
                              String concatenatedTypeAndOption =
                                  '"suggestion_click": $selectedOption';

                              // Call the method to insert click count data
                              await _clickCount(
                                  msgid, concatenatedTypeAndOption, "");
                            } else {
                              // Optionally, you can show a message if no option is selected
                              ScaffoldMessenger.of(context).showSnackBar(
                                const SnackBar(
                                    content:
                                        Text('Please select an option.')),
                              );
                            }
                          },
                        ),
                      ],
                    ),
                  ],
                ),
              );
            },
          ),
        );
      },
    );
  } else {
    if (url != null && action == 'click_here') {
      await _openUrlAndCountClick(url, msgid, type);
    } else {
      debugPrint("Error: URL is null for type $type.");
    }
  }
}