The iOS app's icon count, pushed by ipx (#433)
Background refresh runs when iOS chooses, and for this app that has been rarely, so the icon's unread count lagged until the app was opened (#421). Now ipx sends the count: - ipx: a push_devices table and POST/DELETE /api/push/devices. With [push] set to an APNs key, a task compares each phone's All Subscriptions unread count with the last sent, once a minute, and sends a changed one to Apple as a badge-only push: no alert, no sound. A token Apple says is gone is forgotten. The signing token is ES256 over the team and time, made again every forty minutes, inside Apple's limits. - iOS: the aps-environment entitlement. The app registers for push once badge permission is given, each launch, and hands its token to the page. - Page: registers the token with ipx as the person signed in, since the player's address (ipxapi) lets through only the player's paths, and removes it on signing out. jsonwebtoken here is built without PEM support, so the .p8 key's base64 is decoded in push.rs rather than turning on a feature and changing Cargo.lock for one file. cargo test -p ipx on macOS: 182 pass; the 3 that fail read /proc and fail there without this change too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -17,6 +17,17 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
Offline.shared.backgroundCompletion = completionHandler
|
||||
Offline.shared.resume()
|
||||
}
|
||||
|
||||
/// Apple's address for this phone's copy of the app (#433), handed to the page to register.
|
||||
func application(_ application: UIApplication,
|
||||
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
|
||||
Push.token = deviceToken.map { String(format: "%02x", $0) }.joined()
|
||||
}
|
||||
|
||||
func application(_ application: UIApplication,
|
||||
didFailToRegisterForRemoteNotificationsWithError error: Error) {
|
||||
NSLog("ipx: push registration: %@", error.localizedDescription)
|
||||
}
|
||||
}
|
||||
|
||||
/// The one window. iOS will not run an app that has not adopted scenes, and a CarPlay app would
|
||||
|
||||
@@ -35,7 +35,11 @@ enum Badge {
|
||||
// `-ipx.noBadgePrompt YES` on the launch arguments: a simulator run that drives the page
|
||||
// has nobody to tap Allow, and the dialog covers it.
|
||||
if UserDefaults.standard.bool(forKey: "ipx.noBadgePrompt") { return }
|
||||
UNUserNotificationCenter.current().requestAuthorization(options: [.badge]) { _, _ in }
|
||||
UNUserNotificationCenter.current().requestAuthorization(options: [.badge]) { granted, _ in
|
||||
// Each launch, not once: Apple can change the token, and says to ask every time.
|
||||
guard granted else { return }
|
||||
Task { @MainActor in UIApplication.shared.registerForRemoteNotifications() }
|
||||
}
|
||||
}
|
||||
|
||||
/// The page's own count, sent as its list changes (#421).
|
||||
|
||||
@@ -43,6 +43,7 @@ final class Bridge: NSObject, WKScriptMessageHandler {
|
||||
playback.onEnded = { [weak self] in self?.send(["t": "ended"]) }
|
||||
playback.onError = { [weak self] msg in self?.send(["t": "error", "message": msg]) }
|
||||
playback.onSlept = { [weak self] in self?.send(["t": "slept"]) }
|
||||
Push.onToken = { [weak self] _ in self?.sendPush() }
|
||||
Offline.shared.onChange = { [weak self] in self?.sendKept() }
|
||||
Offline.shared.onFail = { [weak self] path, msg in
|
||||
self?.send(["t": "keepFailed", "url": path, "message": msg])
|
||||
@@ -55,6 +56,12 @@ final class Bridge: NSObject, WKScriptMessageHandler {
|
||||
"keeping": Offline.shared.downloading.sorted()])
|
||||
}
|
||||
|
||||
/// For the page to register with ipx (#433), at ready and whenever Apple gives a new one.
|
||||
private func sendPush() {
|
||||
guard let token = Push.token else { return }
|
||||
send(["t": "push", "token": token, "sandbox": Push.sandbox])
|
||||
}
|
||||
|
||||
// MARK: - page to host
|
||||
|
||||
func userContentController(_ controller: WKUserContentController,
|
||||
@@ -69,6 +76,7 @@ final class Bridge: NSObject, WKScriptMessageHandler {
|
||||
// A page reloaded while the timer runs is told how long is left.
|
||||
if let at = playback.sleepAt { send(["t": "sleep", "left": max(0, at.timeIntervalSinceNow)]) }
|
||||
sendKept()
|
||||
sendPush()
|
||||
case "load":
|
||||
guard let path = m["url"] as? String, let url = Server.resolveForPlayer(path),
|
||||
let enc = m["enc"] as? Int,
|
||||
|
||||
23
ios/Sources/Push.swift
Normal file
23
ios/Sources/Push.swift
Normal file
@@ -0,0 +1,23 @@
|
||||
import Foundation
|
||||
|
||||
/// This phone's push token (#433). ipx sends the icon's count to it while the app is closed,
|
||||
/// which background refresh only guessed at. The page registers it with ipx, signed in as the
|
||||
/// page is, since the player's address lets through only the player's own paths.
|
||||
@MainActor
|
||||
enum Push {
|
||||
/// Told when the token arrives or changes, to pass it to the page.
|
||||
static var onToken: ((String) -> Void)?
|
||||
|
||||
static var token: String? {
|
||||
didSet { if let token, token != oldValue { onToken?(token) } }
|
||||
}
|
||||
|
||||
/// A debug build is served by Apple's sandbox, which ipx has to send to instead.
|
||||
static var sandbox: Bool {
|
||||
#if DEBUG
|
||||
true
|
||||
#else
|
||||
false
|
||||
#endif
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user