Skip to content

Commit

Permalink
Merge pull request #10 from 1998code/revert-9-main
Browse files Browse the repository at this point in the history
Revert "Update project to support APNs updates"
  • Loading branch information
1998code committed Oct 27, 2022
2 parents 2aea6be + 33e0405 commit 14f723c
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 76 deletions.
Empty file removed APNs/AUTHKEY.pem
Empty file.
27 changes: 0 additions & 27 deletions APNs/updateDelivery.sh

This file was deleted.

21 changes: 2 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ https://twitter.com/1998design/status/1552686498276814848?s=21&t=waceX8VvaP-VCGc
https://twitter.com/1998design/status/1570225193095933952?s=21&t=LoYk1Llj0cLpEhG0MBFZLw

## Environment 🔨
- iOS 16.1
- Xcode 14.1
- iOS 16.1 beta 1
- Xcode 14.1 beta 1

## Tutorial 🤔
Dynamic Island: https://1998design.medium.com/how-to-create-dynamic-island-widgets-on-ios-16-1-or-above-dca0a7dd1483 <br/>
Expand Down Expand Up @@ -212,23 +212,6 @@ Updating content state for activity DA288E1B-F6F5-4BF1-AA73-E43E0CC13150
Console: Pizza delivery details: DA288E1B-F6F5-4BF1-AA73-E43E0CC13150 -> PizzaDeliveryAttributes(numberOfPizzas: 1, totalAmount: "$99")
```

## How to push updates to a Live Activity using APNs
1. Create a new key by going to https://developer.apple.com/account/resources/authkeys/list
2. Press (+) to create a new key, give it a name and select [Apple Push Notifications service (APNs)]
3. Press [Register] and download the .p8 file while saving the Auth Key ID somewhere safe
4. Convert .p8 key to .pem key using ```openssl pkcs8 -nocrypt -in AuthKey_XXXXXXXXXX.p8 -out AuthKey_XXXXXXXXXX.pem```

5. Open updateDelivery.sh in some editor
6. Change ```TEAM_ID``` to your Apple Developer Team ID
7. Change ```TOKEN_KEY_FILE_NAME```to the .pem file generated in step 4
8. Change ```AUTH_KEY_ID``` to the Auth Key ID from step 3
9. Change ```TOPIC``` to the bundle ID of your app (MAKE SURE to keep ```.push-type.liveactivity``` at the end!)

10. Run ```./updateDelivery.sh '{LIVE_ACTIVITY_TOKEN}' '{DELIVERY_DRIVER}' {UNIX_TIME_DELIVERY}```
11. Example: ```./updateDelivery.sh 'ABCDEFGH' 'Alexandre' 1666857497```

12. There is no step 12.

## How to pass image data to the widget
1. YES. Use Local Assets Folder <br/>
a. Advantage: Easy to implement <br/>
Expand Down
2 changes: 2 additions & 0 deletions WidgetDemo/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
<key>NSExtensionPointIdentifier</key>
<string>com.apple.widgetkit-extension</string>
</dict>
<key>NSSupportsLiveActivities</key>
<true/>
</dict>
</plist>
8 changes: 4 additions & 4 deletions WidgetDemo/WidgetDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct PizzaDeliveryActivityWidget: Widget {
.foregroundColor(.white.opacity(0.5))
Image(systemName: "ellipsis")
.foregroundColor(.white.opacity(0.5))
Text(timerInterval: (Date.init(timeIntervalSince1970: context.attributes.orderTime))...(Date.init(timeIntervalSince1970: context.state.estimatedDeliveryTime)), countsDown: true)
Text(timerInterval: context.state.estimatedDeliveryTime, countsDown: true)
.bold()
.font(.caption)
.foregroundColor(.white.opacity(0.8))
Expand Down Expand Up @@ -77,7 +77,7 @@ struct PizzaDeliveryActivityWidget: Widget {
}
DynamicIslandExpandedRegion(.trailing) {
Label {
Text(timerInterval: (Date.init(timeIntervalSince1970: context.attributes.orderTime))...(Date.init(timeIntervalSince1970: context.state.estimatedDeliveryTime)), countsDown: true)
Text(timerInterval: context.state.estimatedDeliveryTime, countsDown: true)
.multilineTextAlignment(.trailing)
.frame(width: 50)
.monospacedDigit()
Expand Down Expand Up @@ -108,14 +108,14 @@ struct PizzaDeliveryActivityWidget: Widget {
}
.font(.caption2)
} compactTrailing: {
Text(timerInterval: (Date.init(timeIntervalSince1970: context.attributes.orderTime))...(Date.init(timeIntervalSince1970: context.state.estimatedDeliveryTime)), countsDown: true)
Text(timerInterval: context.state.estimatedDeliveryTime, countsDown: true)
.multilineTextAlignment(.center)
.frame(width: 40)
.font(.caption2)
} minimal: {
VStack(alignment: .center) {
Image(systemName: "timer")
Text(timerInterval: (Date.init(timeIntervalSince1970: context.attributes.orderTime))...(Date.init(timeIntervalSince1970: context.state.estimatedDeliveryTime)), countsDown: true)
Text(timerInterval: context.state.estimatedDeliveryTime, countsDown: true)
.multilineTextAlignment(.center)
.monospacedDigit()
.font(.caption2)
Expand Down
3 changes: 1 addition & 2 deletions iOS16-Live-Activities/ActivityAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ struct PizzaDeliveryAttributes: ActivityAttributes {

public struct ContentState: Codable, Hashable {
var driverName: String
var estimatedDeliveryTime: Double //UNIX time
var estimatedDeliveryTime: ClosedRange<Date>
}

var orderTime: Double //UNIX time
var numberOfPizzas: Int
var totalAmount: String
}
Expand Down
29 changes: 5 additions & 24 deletions iOS16-Live-Activities/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,29 +92,23 @@ struct ContentView: View {

// MARK: - Functions
func startDeliveryPizza() {
UIApplication.shared.registerForRemoteNotifications()
let pizzaDeliveryAttributes = PizzaDeliveryAttributes(orderTime: Date().timeIntervalSince1970, numberOfPizzas: 1, totalAmount:"$99")
let pizzaDeliveryAttributes = PizzaDeliveryAttributes(numberOfPizzas: 1, totalAmount:"$99")

let initialContentState = PizzaDeliveryAttributes.PizzaDeliveryStatus(driverName: "TIM 👨🏻‍🍳", estimatedDeliveryTime: Date().timeIntervalSince1970+1000*60*60)
let initialContentState = PizzaDeliveryAttributes.PizzaDeliveryStatus(driverName: "TIM 👨🏻‍🍳", estimatedDeliveryTime: Date()...Date().addingTimeInterval(15 * 60))

do {
let deliveryActivity = try Activity<PizzaDeliveryAttributes>.request(
attributes: pizzaDeliveryAttributes,
contentState: initialContentState,
pushType: .token)
pushType: nil)
print("Requested a pizza delivery Live Activity \(deliveryActivity.id)")
while deliveryActivity.pushToken == nil {
// This is strongly discouraged in a production app
// Instead watch for pushToken updates in some DispatchQueue or using Activity.PushTokenUpdates
}
print("Pizza delivery Live Activity APN token: \(deliveryActivity.pushToken!.hexString)")
} catch (let error) {
print("Error requesting pizza delivery Live Activity \(error.localizedDescription)")
}
}
func updateDeliveryPizza() {
Task {
let updatedDeliveryStatus = PizzaDeliveryAttributes.PizzaDeliveryStatus(driverName: "TIM 👨🏻‍🍳", estimatedDeliveryTime: Date().timeIntervalSince1970+1000*60*60)
let updatedDeliveryStatus = PizzaDeliveryAttributes.PizzaDeliveryStatus(driverName: "TIM 👨🏻‍🍳", estimatedDeliveryTime: Date()...Date().addingTimeInterval(60 * 60))

for activity in Activity<PizzaDeliveryAttributes>.activities{
await activity.update(using: updatedDeliveryStatus)
Expand All @@ -138,7 +132,6 @@ struct ContentView: View {

@MainActor
func startPizzaAd() {
UIApplication.shared.registerForRemoteNotifications()
// Fetch image from Internet and convert it to jpegData
let url = URL(string: "https://img.freepik.com/premium-vector/pizza-logo-design_9845-319.jpg?w=2000")!
let data = try! Data(contentsOf: url)
Expand All @@ -152,13 +145,8 @@ struct ContentView: View {
let deliveryActivity = try Activity<PizzaAdAttributes>.request(
attributes: pizzaAdAttributes,
contentState: initialContentState,
pushType: .token)
pushType: nil)
print("Requested a pizza ad Live Activity \(deliveryActivity.id)")
while deliveryActivity.pushToken == nil {
// This is strongly discouraged in a production app
// Instead watch for pushToken updates in some DispatchQueue or using Activity.PushTokenUpdates
}
print("Pizza ad Live Activity APN token: \(deliveryActivity.pushToken!.hexString)")
} catch (let error) {
print("Error requesting pizza ad Live Activity \(error.localizedDescription)")
}
Expand All @@ -170,10 +158,3 @@ struct ContentView_Previews: PreviewProvider {
ContentView()
}
}

extension Data {
var hexString: String {
let hexString = map { String(format: "%02.2hhx", $0) }.joined()
return hexString
}
}

0 comments on commit 14f723c

Please sign in to comment.