-
-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to start live activities #196
Add ability to start live activities #196
Conversation
Done in 93565bd. |
OK, I've verified that this is working in a test app so it's ready for review! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this. I left a few comments on the PR. Mostly this is a breaking API change so we have to do it slightly differently to avoid that breakage.
public struct APNSLiveActivityNotificationEvent: Hashable { | ||
|
||
/// The underlying raw value that is send to APNs. | ||
@usableFromInline | ||
internal let rawValue: String | ||
|
||
/// Specifies that live activity should be updated | ||
public static let update = Self(rawValue: "update") | ||
|
||
/// Specifies that live activity should be ended | ||
public static let end = Self(rawValue: "end") | ||
public protocol APNSLiveActivityNotificationEvent: Hashable, Encodable { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a breaking change and we can't just change from a struct to a protocol. We should try and continue to model it with the current struct type.
@@ -17,7 +17,9 @@ import struct Foundation.UUID | |||
/// A live activity notification. | |||
/// | |||
/// It is **important** that you do not encode anything with the key `aps`. | |||
public struct APNSLiveActivityNotification<ContentState: Encodable>: APNSMessage { | |||
public struct APNSLiveActivityNotification<ContentState: Encodable & Hashable & Sendable>: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't just add these requirements to the generic type. This is a breaking API change otherwise. We can make the conformance to those protocols conditional though and that should work.
return APNSLiveActivityNotificationEventEnd() | ||
case "update": | ||
return APNSLiveActivityNotificationEventUpdate() | ||
default: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be exhaustive here and avoid using default cases if possible.
@FranzBusch Thanks so much for the review! I've moved things around to just introduce a separate object for the additional data a
Fixed in 4671d0a. |
Actually this PR is wrong. I think we need to pass the ActivityAttributes as the |
Ok I've reworked things to support sending the correct |
@@ -152,7 +151,7 @@ public struct APNSLiveActivityNotification<ContentState: Encodable>: APNSMessage | |||
) { | |||
self.aps = APNSLiveActivityNotificationAPSStorage( | |||
timestamp: timestamp, | |||
event: event.rawValue, | |||
event: event.rawValue, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just before this gets merged, funky indentation here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 2d41546.
/// - attributes: The ActivityAttributes of the live activity to start | ||
/// - attributesString: The type name of the ActivityAttributes you want to send | ||
/// - alert: An alert that will be sent along with the notification | ||
public init( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need two inits?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No I don't think we do, removed in a142abe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Just one question
Can confirm this works, was able to start a live activity from a push notification with: let notification = APNSStartLiveActivityNotification(
expiration: .immediately,
priority: .immediately,
appID: "com.example.example",
contentState: TestContentState(),
timestamp: 1_672_680_658,
attributes: TestAttributes(),
attributesType: "TestAttributes",
alert: .init(title: .raw("Test"), body: .raw("Test"))
)
do {
let response = try await apnsClient.send(
APNSRequest(
message: notification,
deviceToken: kit.token,
pushType: .liveactivity,
expiration: notification.expiration,
priority: notification.priority,
apnsID: notification.apnsID,
topic: notification.topic,
collapseID: nil
)
)
// Success
} catch {
// Failure
} |
I know this is somewhat off topic, but while everyone is here and I can solicit advice, Apple's docs state:
Does anyone know what method is invoked or what happens when the start notification goes out? I'm severely doubting it's just |
I only recently added an app delegate (I’m in a SwiftUI app), so I’m not sure which handler gets called there yet, but in SwiftUI I noticed that the onChange handler for scenePhase was triggered with the .background value, where I’m iterating over ActivityKit’s static activities property to grab the push tokens and sending them to my backend. |
I believe this PR brings things in line with the JSON payload described in the docs.
This makes the
APNSLiveActivityNotificationEvent
type a bit more clunky to deal with, so I'd love to hear if anyone has any ideas on how to improve that.I also ran
swift-format --configuration .swift-format
on my changes which seems to have brought in some unrelated format changes. If this is a PR worth pursuing I'm happy to clean those up.See also #191.