如何从远程通知启动LiveActivity • Swift

huangapple go评论72阅读模式
英文:

how to start a liveActivity from a remote notification • Swift

问题

在实现用于调试目的的锁定屏幕实时活动小部件时,我有一个按钮,可以运行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)

    }

我尝试在appDelegatedidReceiveRemoteNotification中运行startLiveActivity()函数:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse) async {
        
        LiveActivityHelper.shared.startLiveActivity()
}

还有从通知扩展中:

class NotificationService: UNNotificationServiceExtension, UNUserNotificationCenterDelegate {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {

    ...
    LiveActivityHelper.shared.startLiveActivity()

所有这些方法都导致相同的结果,小部件只在应用程序打开时出现。

Apple的文档包括如何通过远程通知更新小部件,但没有说明如何使用这种方法启动它。

英文:

When implementing lock screen live activity widget for debugging purposes I had a button which runs startLiveActivity() function, app runs normaly widget appears totally functioning.

However I need this activity widget to appear whenever the app receives a remote push notification when the app is killed or in background, but it is appearing only when app is in foreground which doesn't really help the case here.

class LiveActivityHelper: ObservableObject {
    
    static var shared = LiveActivityHelper()
    
    @Published var activity: Activity&lt;Attributes&gt;? = nil

    func startLiveActivity() {
        
        let state = Attributes.ContentState()
        
        activity = try? Activity&lt;Attributes&gt;.request(attributes: Attributes(), contentState: state, pushType: nil)

    }

I tried running the startLiveActivity() function from appDelegate's didReceiveRemoteNotification:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse) async {
        
        LiveActivityHelper.shared.startLiveActivity()
}

And from a notification extension I have:

class NotificationService: UNNotificationServiceExtension, UNUserNotificationCenterDelegate {

    var contentHandler: ((UNNotificationContent) -&gt; Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -&gt; Void) {

    ...
    LiveActivityHelper.shared.startLiveActivity()

All these approaches lead to the same result, the widget appearing only when the app is opened.

Apple's documentation included updating the widget from a remote notification but not how to start it using that approach.

答案1

得分: 1

A Live Activity can only be started while the app is in foreground,因为其目的是跟踪用户发起的功能,根据文档。

发送您的推送通知,让用户使用它打开您的应用程序,然后启动活动。明确说明您期望的流程不受支持。

英文:

A Live Activity can only be started while the app is in foreground, because its purpose is to track a user-initiated feature, based on documentation.

Send your push notification, have your user open your app with it, then start the activity. Your desired flow is explicitly stated to be unsupported.

huangapple
  • 本文由 发表于 2023年7月27日 22:16:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定