PushKeychainStorage

public final class PushKeychainStorage : PushStorage, @unchecked Sendable

Keychain-based storage implementation for Push credentials and notifications. Uses iOS Keychain Services for secure credential storage.

This implementation stores each item as JSON data in the iOS Keychain with unique identifiers as keys. Credentials and device tokens are stored securely and are never exposed in memory longer than necessary.

Note

This class is thread-safe and handles concurrent access properly using DispatchQueue.

Initializers

  • Creates a new keychain storage instance.

    Declaration

    Swift

    public init(
        credentialService: String = "com.pingidentity.push.credentials",
        notificationService: String = "com.pingidentity.push.notifications",
        tokenService: String = "com.pingidentity.push.tokens",
        logger: Logger? = nil,
        accessGroup: String? = nil,
        accessibility: CFString = kSecAttrAccessibleWhenUnlockedThisDeviceOnly
    )

    Parameters

    credentialService

    The keychain service identifier for credentials. Defaults to “com.pingidentity.push.credentials”.

    notificationService

    The keychain service identifier for notifications. Defaults to “com.pingidentity.push.notifications”.

    tokenService

    The keychain service identifier for device tokens. Defaults to “com.pingidentity.push.tokens”.

    logger

    Optional logger for storage operations.

    accessGroup

    Optional keychain access group for shared access.

    accessibility

    Keychain accessibility level. Defaults to kSecAttrAccessibleWhenUnlockedThisDeviceOnly.

Credential Operations

  • Store a push credential.

    Declaration

    Swift

    public func storePushCredential(_ credential: PushCredential) async throws

    Parameters

    credential

    The push credential to store.

  • Retrieve all stored push credentials.

    Declaration

    Swift

    public func getAllPushCredentials() async throws -> [PushCredential]

    Return Value

    An array of all stored push credentials.

  • Retrieve a specific push credential by ID.

    Declaration

    Swift

    public func retrievePushCredential(credentialId: String) async throws -> PushCredential?

    Parameters

    credentialId

    The identifier of the push credential to retrieve.

    Return Value

    The push credential if found, otherwise nil.

  • Remove a push credential by its ID.

    Declaration

    Swift

    public func removePushCredential(credentialId: String) async throws -> Bool

    Parameters

    credentialId

    The identifier of the push credential to remove.

    Return Value

    true if the credential was removed, false if not found.

  • clearPushCredentials() Asynchronous

    Clear all Push credentials from the storage.

    Declaration

    Swift

    public func clearPushCredentials() async throws
  • Retrieve a push credential by issuer and account name.

    Throws

    PushStorageError.storageFailure if keychain operations fail.

    Declaration

    Swift

    public func getCredentialByIssuerAndAccount(issuer: String, accountName: String) async throws -> PushCredential?

    Parameters

    issuer

    The issuer of the credential.

    accountName

    The account name of the credential.

    Return Value

    The credential if found, nil otherwise.

  • Store a push notification.

    Declaration

    Swift

    public func storePushNotification(_ notification: PushNotification) async throws

    Parameters

    notification

    The push notification to store.

  • Update a push notification.

    Declaration

    Swift

    public func updatePushNotification(_ notification: PushNotification) async throws

    Parameters

    notification

    The push notification to update.

  • Retrieve all stored push notifications.

    Declaration

    Swift

    public func getAllPushNotifications() async throws -> [PushNotification]

    Return Value

    An array of all stored push notifications sorted by createdAt in descending order (newest first).

  • Retrieve all pending push notifications.

    Declaration

    Swift

    public func getPendingPushNotifications() async throws -> [PushNotification]

    Return Value

    An array of pending push notifications.

  • Retrieve a specific push notification by ID.

    Declaration

    Swift

    public func retrievePushNotification(notificationId: String) async throws -> PushNotification?

    Parameters

    notificationId

    The identifier of the push notification to retrieve.

    Return Value

    The push notification if found, otherwise nil.

  • Retrieve a push notification by message ID.

    Declaration

    Swift

    public func getNotificationByMessageId(messageId: String) async throws -> PushNotification?

    Parameters

    messageId

    The message ID of the push notification to retrieve.

    Return Value

    The push notification if found, otherwise nil.

  • Remove a push notification by its ID.

    Declaration

    Swift

    public func removePushNotification(notificationId: String) async throws -> Bool

    Parameters

    notificationId

    The identifier of the push notification to remove.

    Return Value

    true if the notification was removed, false if not found.

  • Remove all push notifications associated with a credential.

    Declaration

    Swift

    public func removePushNotificationsForCredential(credentialId: String) async throws -> Int

    Parameters

    credentialId

    The identifier of the push credential.

    Return Value

    The number of notifications removed.

  • Clear all Push notifications from the storage.

    Declaration

    Swift

    public func clearPushNotifications() async throws
  • Store a push device token.

    Declaration

    Swift

    public func storePushDeviceToken(_ token: PushDeviceToken) async throws

    Parameters

    token

    The push device token to store.

  • Retrieve the current push device token.

    Declaration

    Swift

    public func getCurrentPushDeviceToken() async throws -> PushDeviceToken?

    Return Value

    The current push device token if found, otherwise nil.

  • Clear all Push device tokens from the storage.

    Declaration

    Swift

    public func clearPushDeviceTokens() async throws
  • Count the number of push notifications.

    Declaration

    Swift

    public func countPushNotifications(credentialId: String?) async throws -> Int

    Parameters

    credentialId

    Optional credential identifier to filter notifications.

    Return Value

    The count of push notifications.

  • Retrieve the oldest push notifications.

    Declaration

    Swift

    public func getOldestPushNotifications(limit: Int, credentialId: String?) async throws -> [PushNotification]

    Parameters

    limit

    The maximum number of oldest notifications to retrieve.

    credentialId

    Optional credential identifier to filter notifications.

    Return Value

    An array of the oldest push notifications.

  • Purge push notifications by age.

    Declaration

    Swift

    public func purgePushNotificationsByAge(maxAgeDays: Int, credentialId: String?) async throws -> Int

    Parameters

    maxAgeDays

    The maximum age in days for notifications to keep.

    credentialId

    Optional credential identifier to filter notifications.

    Return Value

    The number of notifications purged.

  • Purge push notifications by count.

    Declaration

    Swift

    public func purgePushNotificationsByCount(maxCount: Int, credentialId: String?) async throws -> Int

    Parameters

    maxCount

    The maximum number of notifications to keep.

    credentialId

    Optional credential identifier to filter notifications.

    Return Value

    The number of notifications purged.