ios - 无法向我的闹钟应用程序发送通知

标签 ios swift notifications alarm

我创建了一个闹钟应用程序,它看起来与 iOS 闹钟应用程序相似,并且单元格和表格 View 也与其相似。这些单元格有 2 个标签和开关按钮,因此当我打开按钮时,警报会在单元格的标签时间激活。

有一个 triggerAlarm() 函数跟踪时间并循环遍历类型为 [Item]()items 数组> (Item 是保存数据的 NSManagedObject 类型的类。它有 2 个属性 time: String & isOn: Bool)。

问题是 viewDidLoad() 中的 Timer 方法没有在 triggerAlarm() func 中运行循环,而是打印 实时。该循环应该将 realTime 与 items 数组进行比较,如果 isOn 属性为真但它没有发生,则调用 notificationTrigger() 函数。

Xcode 也没有显示任何错误。

代码如下:

import UIKit
import UserNotifications

class TableViewController: UITableViewController {

    var realTime  = String()
    var items     = [Item]()

    override func viewDidLoad() {
        super.viewDidLoad()
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow, error in})

    }

    override func viewWillAppear(_ animated: Bool) {

        getData()
        tableView.reloadData()

    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! TableViewCell

        let row = items[indexPath.row]

        cell.timeLbl.text   = row.time
        cell.switchBtn.isOn = row.isOn

        cell.callback = { newValue in
            row.isOn = newValue
            (UIApplication.shared.delegate as! AppDelegate).saveContext()
        }
        return cell
    }

    func getData() {
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        do {
             items = try context.fetch(Item.fetchRequest())
        }catch{
            print("\(error)")
        }
    }

    func triggerAlarm() {
        realTime = DateFormatter.localizedString(from: Date(), dateStyle: .none, timeStyle: .short)
        print(realTime)
        for i in 0..<items.count {
            if (items[i].time!) == realTime && items[i].isOn == true{
                print(time)
                self.notificationTrigger()
            }else {
                return
            }
        }
    }

    func notificationTrigger() {
        let content      = UNMutableNotificationContent()
        content.title    = "time is up \(time)"
        content.subtitle = "asdf"
        content.body     = "qwer"
        content.badge    = 0

        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
        let request = UNNotificationRequest(identifier: "customNotification", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

    }
}

最佳答案

@objc func triggerAlarm() {
    realTime = DateFormatter.localizedString(from: Date(), dateStyle: .none, timeStyle: .short)
    print(realTime)
    for i in 0..<items.count {
        if (items[i].time!) == realTime && items[i].isOn == true{
            print(time)
            self.notificationTrigger()
        }else {
            return
        }
    }
}

这里你在 else 中使用了 return。 因此,与其打破当前迭代的循环(我想你想实现这一点),不如从整个方法调用中返回。而是使用:-

continue

或者最好省略 else 部分

 @objc func triggerAlarm() {
    realTime = DateFormatter.localizedString(from: Date(), dateStyle: .none, timeStyle: .short)
    print(realTime)
    for i in 0..<items.count {
        if (items[i].time!) == realTime && items[i].isOn == true{
            print(time)
            self.notificationTrigger()
        }
    }
}

关于ios - 无法向我的闹钟应用程序发送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51000970/

相关文章:

ios - 将 UIBarButtonItem 与图像或系统预设一起用于 SplitView/Popover 控件

ios - 我是否必须将 iPhone 连接到我的 Mac 才能创建配置文件?

ios - 自动布局 UITableView 扩展父 View 但不大于安全区域

ios - 动画显示按钮

ios - 处于非事件应用程序状态时的本地通知

ios - Physics Bodies 不响应位掩码设置

ios - 如何为 sprite kit 节点组合图像

arrays - 在 Firebase 数据库中存储数组

android - 如何在不显示通知的情况下更新未读计数

重启后的android通知