Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Sources/APNSCore/APNSPriority.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ public struct APNSPriority: Hashable, Encodable, Sendable {

/// Specifies that the notification should be send based on power considerations on the user’s device.
public static let consideringDevicePower = Self(rawValue: 5)

/// Specifies that the notification should be sent in a way that prioritizes the device’s power
/// considerations over all other factors, and prevents awakening the device.
public static let prioritizeDevicePower = Self(rawValue: 1)
}
7 changes: 7 additions & 0 deletions Sources/APNSCore/APNSPushType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public struct APNSPushType: Hashable, Sendable, CustomStringConvertible {
case liveactivity
case pushtotalk
case widgets
case controls
}

public var description: String {
Expand Down Expand Up @@ -115,4 +116,10 @@ public struct APNSPushType: Hashable, Sendable, CustomStringConvertible {
/// - Important: if you set this push type, the topic must use your app’s bundle ID with `.push-type.widgets` appended to the end.
///
public static let widgets = Self(configuration: .widgets)

/// Use the controls push type to reload or update a Control Center control.
///
/// - Important: if you set this push type, the topic must use your app’s bundle ID with `.push-type.controls` appended to the end.
///
public static let controls = Self(configuration: .controls)
}
20 changes: 18 additions & 2 deletions Sources/APNSCore/Alert/APNSAlertNotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ public struct APNSAlertNotification<Payload: Encodable & Sendable>: APNSMessage,
}
}

/// The criteria the system evaluates to determine if it displays the notification in the current Focus.
public var filterCriteria: String? {
get {
self.aps.filterCriteria
}
set {
self.aps.filterCriteria = newValue
}
}

/// A canonical UUID that identifies the notification. If there is an error sending the notification,
/// APNs uses this value to identify the notification to your server. The canonical form is 32 lowercase hexadecimal digits,
/// displayed in five groups separated by hyphens in the form 8-4-4-4-12. An example UUID is as follows:
Expand Down Expand Up @@ -180,6 +190,7 @@ public struct APNSAlertNotification<Payload: Encodable & Sendable>: APNSMessage,
/// - targetContentID: The identifier of the window brought forward.
/// - interruptionLevel: A string that indicates the importance and delivery timing of a notification.
/// - relevanceScore: The relevance score, a number between `0` and `1`, that the system uses to sort the notifications from your app.
/// - filterCriteria: The criteria the system evaluates to determine if it displays the notification in the current Focus.
/// - apnsID: A canonical UUID that identifies the notification.
public init(
alert: APNSAlertNotificationContent,
Expand All @@ -195,6 +206,7 @@ public struct APNSAlertNotification<Payload: Encodable & Sendable>: APNSMessage,
targetContentID: String? = nil,
interruptionLevel: APNSAlertNotificationInterruptionLevel? = nil,
relevanceScore: Double? = nil,
filterCriteria: String? = nil,
apnsID: UUID? = nil
) {
self.aps = APNSAlertNotificationAPSStorage(
Expand All @@ -206,7 +218,8 @@ public struct APNSAlertNotification<Payload: Encodable & Sendable>: APNSMessage,
mutableContent: mutableContent,
targetContentID: targetContentID,
interruptionLevel: interruptionLevel,
relevanceScore: relevanceScore
relevanceScore: relevanceScore,
filterCriteria: filterCriteria
)
self.apnsID = apnsID
self.expiration = expiration
Expand Down Expand Up @@ -243,6 +256,7 @@ extension APNSAlertNotification where Payload == EmptyPayload {
/// - targetContentID: The identifier of the window brought forward.
/// - interruptionLevel: A string that indicates the importance and delivery timing of a notification.
/// - relevanceScore: The relevance score, a number between `0` and `1`, that the system uses to sort the notifications from your app.
/// - filterCriteria: The criteria the system evaluates to determine if it displays the notification in the current Focus.
/// - apnsID: A canonical UUID that identifies the notification.
public init(
alert: APNSAlertNotificationContent,
Expand All @@ -257,6 +271,7 @@ extension APNSAlertNotification where Payload == EmptyPayload {
targetContentID: String? = nil,
interruptionLevel: APNSAlertNotificationInterruptionLevel? = nil,
relevanceScore: Double? = nil,
filterCriteria: String? = nil,
apnsID: UUID? = nil
) {
self.aps = APNSAlertNotificationAPSStorage(
Expand All @@ -268,7 +283,8 @@ extension APNSAlertNotification where Payload == EmptyPayload {
mutableContent: mutableContent,
targetContentID: targetContentID,
interruptionLevel: interruptionLevel,
relevanceScore: relevanceScore
relevanceScore: relevanceScore,
filterCriteria: filterCriteria
)
self.apnsID = apnsID
self.expiration = expiration
Expand Down
7 changes: 6 additions & 1 deletion Sources/APNSCore/Alert/APNSAlertNotificationAPSStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct APNSAlertNotificationAPSStorage: Encodable, Sendable {
case targetContentID = "target-content-id"
case interruptionLevel = "interruption-level"
case relevanceScore = "relevance-score"
case filterCriteria = "filter-criteria"
}

var alert: APNSAlertNotificationContent
Expand All @@ -49,6 +50,8 @@ struct APNSAlertNotificationAPSStorage: Encodable, Sendable {
}
}

var filterCriteria: String?

init(
alert: APNSAlertNotificationContent,
badge: Int? = nil,
Expand All @@ -58,7 +61,8 @@ struct APNSAlertNotificationAPSStorage: Encodable, Sendable {
mutableContent: Double? = nil,
targetContentID: String? = nil,
interruptionLevel: APNSAlertNotificationInterruptionLevel? = nil,
relevanceScore: Double? = nil
relevanceScore: Double? = nil,
filterCriteria: String? = nil
) {
if let relevanceScore = relevanceScore {
precondition(relevanceScore >= 0 && relevanceScore <= 1, "The relevance score can only be between 0 and 1")
Expand All @@ -72,5 +76,6 @@ struct APNSAlertNotificationAPSStorage: Encodable, Sendable {
self.targetContentID = targetContentID
self.interruptionLevel = interruptionLevel
self.relevanceScore = relevanceScore
self.filterCriteria = filterCriteria
}
}
43 changes: 43 additions & 0 deletions Sources/APNSCore/Controls/APNSClient+Controls.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the APNSwift open source project
//
// Copyright (c) 2025 the APNSwift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of APNSwift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//


extension APNSClientProtocol {
/// Sends a controls update notification to APNs.
///
/// - Parameters:
/// - notification: The notification to send.
///
/// - deviceToken: The hexadecimal bytes that identify the user’s device. Your app receives the bytes for this device token
/// when registering for remote notifications.
///
@discardableResult
@inlinable
public func sendControlsNotification(
_ notification: APNSControlsNotification,
deviceToken: String
) async throws -> APNSResponse {
let request = APNSRequest(
message: notification,
deviceToken: deviceToken,
pushType: .controls,
expiration: nil,
priority: nil,
apnsID: notification.apnsID,
topic: notification.topic,
collapseID: nil
)
return try await send(request)
}
}
81 changes: 81 additions & 0 deletions Sources/APNSCore/Controls/APNSControlsNotification.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the APNSwift open source project
//
// Copyright (c) 2022 the APNSwift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of APNSwift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

#if canImport(FoundationEssentials)
import struct FoundationEssentials.UUID
#else
import struct Foundation.UUID
#endif

/// A controls update notification.
public struct APNSControlsNotification: APNSMessage {
@usableFromInline
struct APS: Encodable, Sendable {
enum CodingKeys: String, CodingKey {
case contentChanged = "content-changed"
}

let contentChanged: Bool = true
}

@usableFromInline
enum CodingKeys: CodingKey {
case aps
}

/// The fixed content to indicate that this is a background notification.
@usableFromInline
internal let aps = APS()

/// A canonical UUID that identifies the notification. If there is an error sending the notification,
/// APNs uses this value to identify the notification to your server. The canonical form is 32 lowercase hexadecimal digits,
/// displayed in five groups separated by hyphens in the form 8-4-4-4-12. An example UUID is as follows:
/// `123e4567-e89b-12d3-a456-42665544000`.
///
/// If you omit this, a new UUID is created by APNs and returned in the response.
public var apnsID: UUID?

/// The topic for the notification. In general, the topic is your app’s bundle ID/app ID suffixed with `.push-type.controls`.
public var topic: String

/// Initializes a new ``APNSControlsNotification``.
///
/// - Parameters:
/// - appID: Your app’s bundle ID/app ID. This will be suffixed with `.push-type.controls`.
/// - apnsID: A canonical UUID that identifies the notification.
@inlinable
public init(
appID: String,
apnsID: UUID? = nil
) {
self.init(
topic: appID + ".push-type.controls",
apnsID: apnsID
)
}

/// Initializes a new ``APNSControlsNotification``.
///
/// - Parameters:
/// - topic: The topic for the notification. In general, the topic is your app’s bundle ID/app ID suffixed with `.push-type.controls`.
/// - apnsID: A canonical UUID that identifies the notification.
@inlinable
public init(
topic: String,
apnsID: UUID? = nil
) {
self.topic = topic
self.apnsID = apnsID
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the APNSwift open source project
//
// Copyright (c) 2022 the APNSwift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of APNSwift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

/// The mechanism a Live Activity started via push uses to receive subsequent updates (iOS 18+).
public struct APNSLiveActivityInputPushMethod: Sendable, Hashable {
internal enum Configuration: Sendable, Hashable {
case token
case channel(String)
}
internal var configuration: Configuration

/// The device returns a fresh update push token for the started activity (`input-push-token: 1`).
public static let token = Self(configuration: .token)

/// The started activity receives updates from the given broadcast channel (`input-push-channel`).
public static func channel(_ channelID: String) -> Self {
Self(configuration: .channel(channelID))
}
}
42 changes: 39 additions & 3 deletions Sources/APNSCore/LiveActivity/APNSLiveActivityNotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,36 @@ public struct APNSLiveActivityNotification<ContentState: Encodable & Sendable>:
}
}

/// Timestamp when the notification is marked as stale.
public var staleDate: Int? {
get {
return self.aps.staleDate
}
set {
self.aps.staleDate = newValue
}
}

/// An alert that will be sent along with the notification.
public var alert: APNSAlertNotificationContent? {
get {
return self.aps.alert
}
set {
self.aps.alert = newValue
}
}

/// The relevance score, a number that the system uses to sort the notifications from your app.
public var relevanceScore: Double? {
get {
return self.aps.relevanceScore
}
set {
self.aps.relevanceScore = newValue
}
}

/// A canonical UUID that identifies the notification. If there is an error sending the notification,
/// APNs uses this value to identify the notification to your server. The canonical form is 32 lowercase hexadecimal digits,
/// displayed in five groups separated by hyphens in the form 8-4-4-4-12. An example UUID is as follows:
Expand Down Expand Up @@ -109,6 +139,7 @@ public struct APNSLiveActivityNotification<ContentState: Encodable & Sendable>:
/// - dismissalDate: Timestamp when to dismiss live notification when sent with `end`, if in the past
/// dismiss immediately
/// - staleDate: Timestamp when the notification is marked as stale
/// - relevanceScore: The relevance score, a number that the system uses to sort the notifications from your app.
public init(
expiration: APNSNotificationExpiration,
priority: APNSPriority,
Expand All @@ -119,6 +150,7 @@ public struct APNSLiveActivityNotification<ContentState: Encodable & Sendable>:
timestamp: Int,
dismissalDate: APNSLiveActivityDismissalDate = .none,
staleDate: Int? = nil,
relevanceScore: Double? = nil,
apnsID: UUID? = nil
) {
self.init(
Expand All @@ -131,7 +163,8 @@ public struct APNSLiveActivityNotification<ContentState: Encodable & Sendable>:
alert: alert,
timestamp: timestamp,
dismissalDate: dismissalDate,
staleDate: staleDate
staleDate: staleDate,
relevanceScore: relevanceScore
)
}

Expand All @@ -152,6 +185,7 @@ public struct APNSLiveActivityNotification<ContentState: Encodable & Sendable>:
/// - dismissalDate: Timestamp when to dismiss live notification when sent with `end`, if in the past
/// dismiss immediately
/// - staleDate: Timestamp when the notification is marked as stale
/// - relevanceScore: The relevance score, a number that the system uses to sort the notifications from your app.
public init(
expiration: APNSNotificationExpiration,
priority: APNSPriority,
Expand All @@ -162,15 +196,17 @@ public struct APNSLiveActivityNotification<ContentState: Encodable & Sendable>:
alert: APNSAlertNotificationContent? = nil,
timestamp: Int,
dismissalDate: APNSLiveActivityDismissalDate = .none,
staleDate: Int? = nil
staleDate: Int? = nil,
relevanceScore: Double? = nil
) {
self.aps = APNSLiveActivityNotificationAPSStorage(
timestamp: timestamp,
event: event.rawValue,
contentState: contentState,
dismissalDate: dismissalDate.dismissal,
staleDate: staleDate,
alert: alert
alert: alert,
relevanceScore: relevanceScore
)
self.apnsID = apnsID
self.expiration = expiration
Expand Down
Loading