PushDeviceToken
public struct PushDeviceToken : Codable, Identifiable, Sendable, Equatable
Represents a device token for push notifications.
A device token is a unique identifier assigned by the Apple Push Notification service (APNs) to a specific device and application combination. This token is required for sending push notifications to the device.
Token Management
Device tokens should be updated whenever:
- The app is installed or reinstalled
- The device is restored from backup
- The user updates iOS to a new major version
- The token changes (APNs may regenerate tokens periodically)
Security Considerations
- Tokens are device and app-specific
- Tokens should be stored securely in the Keychain
- Tokens should be sent to the server over secure connections only
- Expired or invalid tokens should be removed from the server
Usage Example
// When receiving a device token from APNs
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenString = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
let pushDeviceToken = PushDeviceToken(token: tokenString)
// Store and register with server
}
-
The unique device token string from APNs.
This is typically a 64-character hexadecimal string representing the device token provided by Apple Push Notification service.
Declaration
Swift
public let token: String -
The date when this token was created or updated.
This timestamp helps track when the token was last registered and can be used to determine if the token needs to be refreshed.
Declaration
Swift
public let createdAt: Date -
Unique identifier for this token record.
Declaration
Swift
public let id: String
-
Creates a new device token instance.
Declaration
Swift
public init(id: String = UUID().uuidString, token: String, createdAt: Date = Date())Parameters
idUnique identifier. Defaults to a new UUID.
tokenThe device token string from APNs.
createdAtThe timestamp when the token was created (defaults to current date).
-
Declaration
Swift
public init(from decoder: Decoder) throws -
Declaration
Swift
public func encode(to encoder: Encoder) throws
-
Declaration
Swift
public static func == (lhs: PushDeviceToken, rhs: PushDeviceToken) -> Bool
View on GitHub