قالب وردپرس درنا توس
Home / IOS Development / ios – Displays scheduled alerts in a table viewer

ios – Displays scheduled alerts in a table viewer



I'm trying to list all the notifications a user has created and planned in my app, similar to the list of alarms in the "Clock" app from apple. Every time I get a lot of messages and try to show them, they aren't properly displayed all the time.

Each alert is repeated every day at the same time, so I use UNUserNotificationCenter.current () .getPendingNotificationRequests to get a series of messages. With this order of alerts, I reflect on each alert, create a new custom "Reminder" item, and add it to my & # 39; Reminders & # 39; that I use when showing the alerts in the table view.

I do this every time you use the viewWillAppear feature.

Here is the code:

  override func viewWillAppear (_ animated: Bool) {
super.viewWillAppear (animated)

generateReminders ()

tableView.reloadData ()
}

func generateReminders ()
{
la center = UNUserNotificationCenter.current ()
center.getPendingNotificationRequests {(notifications) i
for item in alerts {
if let triggers = item.trigger like? UNCalendarNotificationTrigger,
la triggerDate = trigger.nextTriggerDate () {
var withSound = true
if (item.content.sound! = UNNotificationSound.default)
{
withSound = false
}
self.reminders.append (reminder (identifier: item.identifier, time: triggerDate, message: item.content.body, withSound: withSound, isAPendingNotification: true))
}
}
self.remindersCount = notifications.count
}
}

When the cells are about to appear in the table view control, I use the & # 39; Reminder & # 39; array to customize each cell to display the information in the notification. This is all done in the & # 39; cellForRowAt & # 39; function code below.

  override func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell (withIdentifier: "Reminder" for: indexPath)
var text = ""
var detailText = ""

if (indexPath.row <remindersCount) {
leave reminder = reminders [indexPath.row]
la formatter = DateFormatter ()
formats.dateFormat = "HH: mm"

text = formats.string (from: reminder.Time)
detailText = reminder.Message
}

cell.textLabel? .text = text
cell.detailTextLabel? .text = detailText

return cell
}

When a user selects a different category to display, I reset the reminder object to be empty so that when they return to this category, an updated range of messages appears, the code below.

  overrides func viewWillDisappear (_ animated: Bool) {
super.viewWillDisappear (animated)
reminders = [Reminder] ()
remindersCount = 0
tableView.setNeedsDisplay ()
}

The problem I am facing is this is extremely inconsistent, sometimes all the alerts appear, sometimes only some are displayed, and sometimes none of them appear. Each time I print the number of notifications in UNUserNotificationCenter.current (). The GetPendingNotificationRequests method is always the correct number. Furthermore, when I click on a cell that will contain information about an alert, the information is there, it just isn't displayed.

Here is a short video of these issues.

Displaying Notifications in Swift

I'm unsure how to fix this, I've tried to run the code on the main queue and on the global queue of the quality of the service set to & # 39; .userInteractive & # 39; as shown below, but still no dice.

  la center = UNUserNotificationCenter.current ()
la dq = DispatchQueue.global (qos: .userInteractive)
dq.async {
center.getPendingNotificationRequests {(notifications) i
for item in alerts {
if let triggers = item.trigger like? UNCalendarNotificationTrigger,
la triggerDate = trigger.nextTriggerDate () {
var withSound = true
if (item.content.sound! = UNNotificationSound.default)
{
withSound = false
}
self.reminders.append (reminder (identifier: item.identifier, time: triggerDate, message: item.content.body, withSound: withSound, isAPendingNotification: true))
}
}
self.remindersCount = notifications.count
}
}

A small application of this problem can be downloaded from this Github repository.

https://github.com/AlexMarchant98/LitstingNotificationsIssue


Source link