我有一个实时活动,需要经常更新它,例如每分钟或每隔几分钟,就像 Uber 应用程序更新其实时活动以向您显示行程进度一样。我知道推送通知是一种通用的替代方案,但我想首先了解本地路径,因为当前的文档(从 iOS 16.4 / Xcode 14.3 开始)似乎建议我使用staleDate+ 后台任务是 Apple 推荐的一种方法即使文档没有具体说明更新之间的频率/时间间隔,也要保持您的活动更新。
首先,我像这样设置我的活动:
if ActivityAuthorizationInfo().areActivitiesEnabled {
let state = MyAttributes.ContentState(info: info)
let attributes = MyAttributes(name: "test")
let content = ActivityContent(state: state, staleDate: Date(timeIntervalSinceNow: Self.activityUpdateTimeInterval)) // e.g. 60 seconds
currentActivity = try Activity.request(attributes: attributes, content: content)
observeActivityStateUpdates()
}
Run Code Online (Sandbox Code Playgroud)
在第二个函数中,observeActivityStateUpdates我试图观察activityStateUpdates流的变化:
private func observeActivityStateUpdates() {
Task { [weak self] in
guard let activityStateUpdates = self?.currentActivity?.activityStateUpdates else { return }
for await update in activityStateUpdates {
print("LiveActivity: new …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种每秒或每 2 秒更新实时活动的方法,而不使用推送通知,同时将应用程序保持在后台。
你有什么建议吗?我尝试过类似的操作,但几秒钟后它就停止工作了:
var bgTask = UIBackgroundTaskIdentifier(rawValue: 1324)
bgTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
UIApplication.shared.endBackgroundTask(bgTask)
})
let timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(updateInfo), userInfo: nil, repeats: true)
RunLoop.current.add(timer, forMode: .default)
Run Code Online (Sandbox Code Playgroud) 我尝试在 Swift 中向我的应用程序添加实时活动。但是,当我尝试运行该应用程序时,出现错误消息“尾随闭包传递给不接受闭包的‘NSManagedObjectContext’类型的参数”。我的应用程序使用 Core Data 来保存我的应用程序的数据。
class HabitManager: ObservableObject {
private var activity: Activity<HabitsWidgetAttributes>? = nil
func startHabit(name: String, symbol: String, currentTask: String) {
let attributes = HabitsWidgetAttributes(name: name, symbol: symbol)
let state = HabitsWidgetAttributes.ContentState(currentTask: currentTask)
activity = try? Activity<HabitsWidgetAttributes>.request(attributes: attributes, contentState: state)
}
func updateActivity(nextTask: String) {
let state = HabitsWidgetAttributes.ContentState(currentTask: nextTask)
Task { // Line where the error is generated
await activity?.update(using: state)
}
}
func stopActivity() {
let state = HabitsWidgetAttributes.ContentState(currentTask: "Finished")
Task { // Line where …Run Code Online (Sandbox Code Playgroud) 我在实时活动中添加了Text(Date().addingTimeInterval(2400), style: .timer),但是当倒计时为零并且通知尚未发送时,它就开始计数。
可以修复吗?
我尝试使用普通计时器,但在实时活动中它不起作用
当出于调试目的实现锁屏实时活动小部件时,我有一个运行startLiveActivity()功能的按钮,应用程序正常运行小部件看起来完全正常工作。
但是,每当应用程序被杀死或在后台时收到远程推送通知时,我都需要显示此活动小部件,但它仅在应用程序位于前台时才出现,这对这里的情况没有真正的帮助。
class LiveActivityHelper: ObservableObject {
static var shared = LiveActivityHelper()
@Published var activity: Activity<Attributes>? = nil
func startLiveActivity() {
let state = Attributes.ContentState()
activity = try? Activity<Attributes>.request(attributes: Attributes(), contentState: state, pushType: nil)
}
Run Code Online (Sandbox Code Playgroud)
我尝试startLiveActivity()从 appDelegate 的 didReceiveRemoteNotification 运行该函数:
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse) async {
LiveActivityHelper.shared.startLiveActivity()
}
Run Code Online (Sandbox Code Playgroud)
从通知扩展中我有:
class NotificationService: UNNotificationServiceExtension, UNUserNotificationCenterDelegate {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { …Run Code Online (Sandbox Code Playgroud)