Flutter Inbox

Inbox Feature: Notification Handling

The Inbox feature within our platform manages push notifications in a manner designed to provide users with the most relevant and up-to-date information. One aspect of this functionality involves the handling of notifications sent within a specific time frame. Therefore, notifications sent five minutes ago and earlier will be deleted when a new notification is received. This ensures that only the latest and most pertinent information is retained in the user's inbox.

NetmeraInbox is the core class providing methods and properties needed for operations on push notifications like fetching push objects or updating push objects' status, but you can not directly initialize a NetmeraInbox instance. You get an instance from SDK, then operate on that instance for future inbox actions. Here is the common workflow to use inbox feature of Netmera.

You must first define filtering properties by creating a NetmeraInboxFilter instance. You determine which push notifications will be included in the fetched list by setting related properties of this NetmeraInboxFilter instance.

NetmeraInboxFilter class provides filtering according to the following options:

  • Inbox Status: Read / Unread / Deleted

  • Categories: Categories to which push notifications are belong.

  • Including expired push notifications or not.

  • Page Size: This is not to filter, but to determine the size of chunks which will be gathered during one request.

Netmera Inbox Examples

 String _currentStatus = Netmera.PUSH_OBJECT_STATUS_ALL.toString();
  String _count = "0";
  List<NetmeraPushInbox> _pushInboxList = List.empty(growable: true);

  List<DropdownMenuItem<String>> getInboxList() {
    List<DropdownMenuItem<String>> items = List.empty(growable: true);
    items.add(DropdownMenuItem(value: Netmera.PUSH_OBJECT_STATUS_ALL.toString(), child: const Text("ALL")));
    items.add(DropdownMenuItem(value: Netmera.PUSH_OBJECT_STATUS_READ.toString(), child: const Text("READ")));
    items.add(DropdownMenuItem(value: Netmera.PUSH_OBJECT_STATUS_UNREAD.toString(), child: const Text("UNREAD")));
    items.add(DropdownMenuItem(value: Netmera.PUSH_OBJECT_STATUS_DELETED.toString(), child: const Text("DELETED")));
    return items;
  }

  onInboxStatusChanged(String status) {
    setState(() {
      _currentStatus = status;
    });
  }

  String getStatusText(int status) {
    switch (status) {
      case Netmera.PUSH_OBJECT_STATUS_ALL:
        return "ALL";
      case Netmera.PUSH_OBJECT_STATUS_READ:
        return "READ";
      case Netmera.PUSH_OBJECT_STATUS_UNREAD:
        return "UNREAD";
      case Netmera.PUSH_OBJECT_STATUS_DELETED:
        return "DELETED";
    }
    return "";
  }

  fillInboxList(list) {
    setState(() {
      _pushInboxList = list;
    });
  }

  // Click Functions
  emptyAction() {}

  getInboxFilter() {
    NetmeraInboxFilter inboxFilter = NetmeraInboxFilter();
    inboxFilter.setPageSize(2);
    inboxFilter.setStatus(int.parse(_currentStatus));
    inboxFilter.setIncludeExpiredObjects(true);
    inboxFilter.setCategories(null);
    return inboxFilter;
  }

  fetchInbox() async {
    Netmera.fetchInbox(getInboxFilter()).then((list) {
      fillInboxList(list);
    }).catchError((error) {
      debugPrint(error);
    });
  }

  fetchNextPage() async {
    Netmera.fetchNextPage().then((list) {
      fillInboxList(list);
    }).catchError((error) {
      debugPrint(error);
    });
  }

  countForStatus() async {
    Netmera.countForStatus(int.parse(_currentStatus)).then((val) {
      setState(() {
        if (val != -1) {
          _count = val.toString();
        }
      });
    });
  }

  handlePushObject() async {
    if (_pushInboxList.isNotEmpty) {
      Netmera.handlePushObject(_pushInboxList[0].getPushId()!);
    }
  }

  handleInteractiveAction() async {
    if (_pushInboxList.isNotEmpty) {
      for (var element in _pushInboxList) {
        if (element.getInteractiveActions() != null && element.getInteractiveActions()!.isNotEmpty) {
          Netmera.handleInteractiveAction(element.getInteractiveActions()![0]);
          return;
        }
      }
    }
  }

  inboxUpdateStatus() {
    List<String> selectedPushList = List.empty(growable: true);
    if (_pushInboxList.length > 1) {
      selectedPushList.add(_pushInboxList[0].getPushId()!);
      selectedPushList.add(_pushInboxList[1].getPushId()!);
    }
    int status = Netmera.PUSH_OBJECT_STATUS_UNREAD;
    Netmera.inboxUpdateStatus(selectedPushList, status).then((netmeraError) {
      if (netmeraError != null) {
        debugPrint(netmeraError);
      }
    }).catchError((error) {
      debugPrint(error);
    });
  }

  updateAll() async {
    if (_pushInboxList.isNotEmpty) {
      var updateStatus = int.parse(_currentStatus);
      if (updateStatus == Netmera.PUSH_OBJECT_STATUS_ALL) {
        debugPrint("Please select different status than all!!");
        return;
      }

      Netmera.updateAll(updateStatus).then((netmeraError) {
        fetchInbox();
      }).catchError((error) {
        debugPrint(error);
      });
    }
  }

  inboxCountForStatus() async {
    NMInboxStatusCountFilter filter = NMInboxStatusCountFilter();
    filter.setStatus(int.parse(_currentStatus));
    filter.setIncludeExpired(true);
    Netmera.getInboxCountForStatus(filter).then((map) {
      String countStatusText = "ALL: " +
          map[Netmera.PUSH_OBJECT_STATUS_ALL.toString()].toString() +
          ", " +
          "READ: " +
          map[Netmera.PUSH_OBJECT_STATUS_READ.toString()].toString() +
          ", " +
          "UNREAD: " +
          map[Netmera.PUSH_OBJECT_STATUS_UNREAD.toString()].toString() +
          ", " +
          "DELETED: " +
          map[Netmera.PUSH_OBJECT_STATUS_DELETED.toString()].toString();
      setState(() {
        _count = countStatusText;
      });
    }).catchError((error) {
      debugPrint(error);
    });
  }

Netmera Inbox Category Examples

 String _currentStatus = Netmera.PUSH_OBJECT_STATUS_ALL.toString();
  List<dynamic> _categoryList = List.empty(growable: true);

  List<DropdownMenuItem<String>> getCategoryStatusList() {
    List<DropdownMenuItem<String>> items = List.empty(growable: true);
    items.add(DropdownMenuItem(value: Netmera.PUSH_OBJECT_STATUS_ALL.toString(), child: const Text("ALL")));
    items.add(DropdownMenuItem(value: Netmera.PUSH_OBJECT_STATUS_READ.toString(), child: const Text("READ")));
    items.add(DropdownMenuItem(value: Netmera.PUSH_OBJECT_STATUS_UNREAD.toString(), child: const Text("UNREAD")));
    items.add(DropdownMenuItem(value: Netmera.PUSH_OBJECT_STATUS_DELETED.toString(), child: const Text("DELETED")));
    return items;
  }

  onCategoryStatusChanged(String status) {
    setState(() {
      _currentStatus = status;
    });
  }

  String getStatusText(int status) {
    switch (status) {
      case Netmera.PUSH_OBJECT_STATUS_ALL:
        return "ALL";
      case Netmera.PUSH_OBJECT_STATUS_READ:
        return "READ";
      case Netmera.PUSH_OBJECT_STATUS_UNREAD:
        return "UNREAD";
      case Netmera.PUSH_OBJECT_STATUS_DELETED:
        return "DELETED";
    }
    return "";
  }

  // Click Functions
  emptyAction() {}

  getCategoryFilter() {
    NetmeraCategoryFilter categoryFilter = NetmeraCategoryFilter();
    categoryFilter.setPageSize(2);
    categoryFilter.setStatus(int.parse(_currentStatus));
    categoryFilter.setIncludeExpiredObjects(true);
    return categoryFilter;
  }

  fetchCategory() async {
    Netmera.fetchCategory(getCategoryFilter()).then((list) {
      fillCategoryList(list);
    }).catchError((error) {
      debugPrint(error);
    });
  }

  fetchNextCategoryPage() async {
    Netmera.fetchNextCategory().then((list) {
      fillCategoryList(list);
    }).catchError((error) {
      debugPrint(error);
    });
  }

  fillCategoryList(list) {
    setState(() {
      _categoryList = list;
    });
  }

  handleLastMessage() async {
    if (_categoryList.isNotEmpty) {
      Netmera.handleLastMessage(_categoryList[0]);
    }
  }

  updateStatusCategories() async {
    if (_categoryList.isNotEmpty) {
      List<String> selectedCategories = List.empty(growable: true);
      if (_categoryList.length == 1) {
        selectedCategories.add(_categoryList[0].getCategoryName()!);
      } else if (_categoryList.length > 1) {
        selectedCategories.add(_categoryList[0].getCategoryName()!);
        selectedCategories.add(_categoryList[1].getCategoryName()!);
      }

      Netmera.updateStatusByCategories(Netmera.PUSH_OBJECT_STATUS_READ, selectedCategories).then((netmeraError) {
        fetchCategory();
      }).catchError((error) {
        debugPrint(error);
      });
    }
  }

  getUserCategoryPreferenceList() async {
    Netmera.getUserCategoryPreferenceList().then((list) {
      fillCategoryList(list);
    }).catchError((error) {
      debugPrint(error);
    });
  }

  setUserCategoryPreference(NetmeraCategoryPreference item) async {
    Netmera.setUserCategoryPreference(item.getCategoryId()!, !item.getOptInStatus()!).then((value) {
      debugPrint("Successfully set user category preference list");
    }).catchError((error) {
      debugPrint(error);
    });
  }

Last updated