rao*_*did 1 xcode ios swift ios-background-mode activitykit
当出于调试目的实现锁屏实时活动小部件时,我有一个运行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) {
...
LiveActivityHelper.shared.startLiveActivity()
Run Code Online (Sandbox Code Playgroud)
所有这些方法都会产生相同的结果,小部件仅在应用程序打开时出现。
苹果的文档包括通过远程通知更新小部件,但没有介绍如何使用该方法启动它。
从 iOS 17.2(目前处于测试阶段)开始,您现在可以通过推送通知启动实时活动。
请求pushToStartToken并使用它将特定的有效负载发送到您的应用程序。这将启动实时活动并授予一些后台运行时间,以便您设置实时活动、获取更新令牌等。
| 归档时间: |
|
| 查看次数: |
1252 次 |
| 最近记录: |