【问题标题】:Update content of delivered local notification SwiftUI更新发送的本地通知内容 SwiftUI
【发布时间】:2023-01-05 18:49:20
【问题描述】:

Xcode 和 Swift 的新手。我的应用程序有一个倒计时的计时器。我希望从锁定屏幕上可以看到倒计时作为通知,但我不知道如何(甚至可能)更新现有本地通知的内容。

到目前为止我找到的唯一解决方案是取消当前通知并每秒显示一个新通知,这并不理想。

代码:

struct TimerApp: View {
    private let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() 
    @State private var isActive: Bool = true    // whether not timer is active 
    @State private var timeRemaining: Int = 600     // 60 seconds * 10 mins = 10 min-countdown timer

    var body: some View {
        // body stuff
        // toggle isActive if user stops/starts timer

    }.onReceive(timer, perform: { _ in 
        guard isActive else { return }

        if timeRemaining > 0 { 
            // would like to update current notification here
            // *******

            // instead, removing and adding a new one right now
            UNUserNotificationCenter.current().removeAllDeliveredNotifications()
            UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
            addNotification()
 
            timeRemaining -= 1
        } else {
            isActive = false
            timeRemaining = 0
        }
    }

    func addNotification() {
        
        let center = UNUserNotificationCenter.current()

        let addRequest = {
            let content = UNMutableNotificationContent()
            content.title = "App Title"
            content.body = "Time: \(timeFormatted())"
            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.0001, repeats: false)

            let request = UNNotificationRequest(identifier: "onlyNotification", content: content, trigger: trigger)
            center.add(request)            
            
        }
        
        center.getNotificationSettings { settings in
            if settings.authorizationStatus == .authorized {
                addRequest()
            } else {
                center.requestAuthorization(options: [.alert, .badge]) { success, error in
                    if success {
                        addRequest()
                    } else if let error = error {
                        print("error :( \(error.localizedDescription)")
                    }
                }
            }
        }
    }

    func timeFormatted() -> String {
        // converts timeRemaining to 00:00 format and returns string
    }
}

这就是现在最糟糕的解决方案的样子。

【问题讨论】:

  • 您可能想查看新的锁定屏幕小部件,它们可能会提供更好的解决方案。我认为不可能修改通知。
  • 会做的,谢谢你的提示!

标签: ios swift xcode swiftui


【解决方案1】:

目前无法更新待处理或已发送的本地通知请求。

就像您猜到的那样,为了传递具有不同内容的通知,您需要使用 UNNotificationCenter 的方法 removePendingNotificationRequests(withIdentifiers:)removeAllPendingNotificationRequests() 删除待处理的请求,并添加一个包含更新内容的新请求。

【讨论】:

  • 跛脚,来吧苹果!谢谢你的回答:)
【解决方案2】:

我不能发表评论,所以我在这里写回复。

如果无法修改通知,音乐应用程序如何在播放歌曲时显示已播放的曲目?我认为应该有办法还是通过其他一些方法来完成?

【讨论】:

    猜你喜欢
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    相关资源
    最近更新 更多