iOS SDK API reference

The complete API reference for the Knock iOS SDK.

In this section, you'll find the documentation for the classes and methods available in the iOS SDK.

Knock

The top-level Knock class. This is a shared instance to interact with the SDK's API methods.

Knock.shared.setup()

Sets up the shared Knock instance. Make sure to call this as soon as you can. Preferrably in your AppDelegate.

Params

publishableKeystring*The public API key for the Knock environment.
pushChannelIdstring (optional)The Knock APNS channel id that you plan to use within your app.
optionsKnock.KnockStartupOptions (optional)Optional startup options to configure your Knock instance.

KnockAppDelegate

This class serves as an optional base class designed to streamline the integration of Knock into your application. By inheriting from KnockAppDelegate in your AppDelegate, you gain automatic handling of Push Notification registration and device token management, simplifying the initial setup process for Knock's functionalities.

The class also provides a set of open helper functions that are intended to facilitate the handling of different Push Notification events such as delivery in the foreground, taps, and dismissals. These helper methods offer a straightforward approach to customizing your app's response to notifications, ensuring that you can tailor the behavior to fit your specific needs.

Override any of the provided methods to achieve further customization, allowing you to control how your application processes and reacts to Push Notifications. Additionally, by leveraging this class, you ensure that your app adheres to best practices for managing device tokens and interacting with the notification system on iOS, enhancing the overall reliability and user experience of your app's notification features.

Key Features:

  • Automatic registration for remote notifications, ensuring your app is promptly set up to receive and handle Push Notifications.
  • Simplified device token management, with automatic storage of the device token, facilitating easier access and use in Push Notification payloads.
  • Customizable notification handling through open helper functions, allowing for bespoke responses to notification events such as foreground delivery, user taps, and dismissal actions.
  • Automatic message status updates, based on Push Notification interaction.

Authentication

Knock.shared.isAuthenticated()

Convienience method to determine if a user is currently authenticated for the Knock instance.

Returns: Bool

Params

checkUserTokenBoolWhether Knock should also check to make sure user has a user token. Only required when using a Knock prod environment.

Knock.shared.signIn()

Sets the userId and userToken for the current Knock instance. If the device token and pushChannelId were set previously, this will also attempt to register the token to the user that is being signed in. This does not get the user from the database nor does it return the full User object. You should consider using this in areas where you update your local user's state.

Params

userIdStringThe Knock user ID to make requests against.
userTokenString (optional)A JWT that identifies the authenticated user, signed with the private key provided in the Knock dashboard. Required to secure your production environment. Learn more.

Example

Knock.shared.signOut()

Sets the userId and userToken for the current Knock instance back to nil. If the device token and pushChannelId were set previously, this will also attempt to unregister the token to the user that is being signed out so they don't receive pushes they shouldn't get. You should call this when your user signs out

  • Note: This will not clear the device token so that it can be accessed for the next user to login.

Example

User Management

Knock.shared.getUserId()

Fetch the userId that was set from the Knock.shared.signIn method.

Returns: String?

Knock.shared.getUser()

Returns the current user's profile stored in Knock. https://docs.knock.app/reference#get-user#get-user

Returns: Knock.User

Knock.shared.updateUser()

Updates the current user's profile in Knock. https://docs.knock.app/reference#get-user#get-user

Returns: Knock.User

Params

userKnock.UserThe User object that you want to set for the current user.

Channels/Push Notifications

Knock.shared.getUserChannelData()

Returns the channel data for the current user on the channel specified with channelId.

Params

channelIdStringThe channel ID to get channel data for

Returns: Knock.ChannelData

Knock.shared.updateUserChannelData()

Updates the channel data for the current user on the channel specified with channelId.

Params

channelIdStringThe channel ID to update the channel data for
dataAnyEncodableThe data to update for the channel data

Returns: Knock.ChannelData

Knock.shared.getApnsDeviceToken()

Returns the apnsDeviceToekn that was set from the Knock.shared.registerTokenForAPNS. If you use our KnockAppDelegate, the token registration will be handled for you automatically.

Returns String?

Knock.shared.registerTokenForAPNS()

Registers an Apple Push Notification Service token so that the device can receive remote push notifications. This is a convenience method that internally gets the channel data and searches for the token. If it exists, then it's already registered and it returns. If the data does not exists or the token is missing from the array, it's added. If the new token differs from the last token that was used on the device, the old token will be unregistered.

You can learn more about APNS here.

Params

channelIdStringThe Knock APNS channel id to associate the device token to.
tokenString OR Datathe APNS device token.

Returns: Knock.ChannelData

Knock.shared.unregisterTokenForAPNS()

Unregisters the current deviceId associated to the user so that the device will no longer receive remote push notifications for the provided channelId.

Params

channelIdStringThe Knock APNS channel id to associate the device token to.
tokenString OR Datathe APNS device token.

Returns: Knock.ChannelData

Knock.shared.getNotificationPermissionStatus()

Convenience method to determine whether or not the user is allowing Push Notifications for the app.

Returns: UNAuthorizationStatus

Knock.shared.requestNotificationPermission()

Requests push notification permissions to the user.

Params

optionsUNAuthorizationOptionsThe type of push notification permissions you want. Defaults to `[.sound, .badge, .alert]`

Returns: UNAuthorizationStatus

Knock.shared.requestAndRegisterForPushNotifications()

Convenience method to request Push Notification permissions for the app, and then, if successfull, registerForRemoteNotifications in order to get a device token.

Preferences

Knock.shared.getAllUserPreferences()

Returns all of the preference sets for the current user.

Returns: Knock.PreferenceSet

Knock.shared.getUserPreferences()

Returns a single preference set for the current user, specified by the preferenceId. For the default preference set, set the preferenceId to be default.

Params

preferenceIdStringThe ID of the preference set to retrieve

Returns: Knock.PreferenceSet

Knock.shared.setUserPreferences()

Updates the preference set specified by the preferenceId with the new preferenceSet.

Params

preferenceIdStringThe ID of the preference set
preferenceSetKnock.PreferenceSetThe preferences to update for the preference set

Returns: Knock.PreferenceSet

Messages

Knock.shared.getMessage()

Returns information about a single message specified by the messageId. https://docs.knock.app/reference#get-a-message

Params

messageIdStringThe ID of the message to retrieve

Returns Knock.KnockMessage

Knock.shared.updateMessageStatus()

Updates the status of a single message specified by the message or messageId. https://docs.knock.app/reference#undo-message-status

Params

messageIdStringThe ID of the message to update
messageKnockMessageA Knock message to update
statusKnockMessageStatusUpdateTypeThe status to set on the message

Returns Knock.KnockMessage

Knock.shared.deleteMessageStatus()

Un-marks the given status on a message specified by the message or messageId, recording an event in the process. https://docs.knock.app/reference#undo-message-status

Params

messageIdStringThe ID of the message to update
messageKnockMessageA Knock message to update
statusKnockMessageStatusUpdateTypeThe status to set on the message

Returns Knock.KnockMessage

batchUpdateStatuses

Updates up to 50 messages with the given status in a single request specified by messageIds or messages. https://docs.knock.app/reference#batch-update-message-status

Params

messageIds[String]A list of message IDs
message[Knock.KnockMessage]A list of messages
statusKnock.KnockMessageStatusBatchUpdateTypeThe status to set on the messages

Returns [Knock.KnockMessage]

FeedManager

Knock.FeedManager.init()

Creates a new instance of a FeedManager for interacting with a user's in-app notification feed.

Params

feedIdStringThe UUID of your Knock in-app feed channel ID.
optionsKnock.FeedClientOptionsFeed options to apply as defaults.

Example

Knock.shared.feedManager.connectToFeed()

Connects the feed instance to the real-time socket so that any new items published to the feed are received over the websocket.

Params

optionsKnock.FeedClientOptions (optional)Feed options to apply. Will override any options specified in the FeedManager constructor.

Knock.shared.feedManager.disconnectFromFeed()

Disconnects a connected real-time instance.

Knock.shared.feedManager.on()

Binds an event listener for incoming web socket events. Must have called connectToFeed first.

eventNameStringThe event name to listen for. Currently only `new-message` is supported.

Example

Knock.shared.feedManager.getUserFeedContent()

Retrieves the user's feed content for the feed. Can be scoped by passing options, which also allows for paginating the contents of the feed using the before and after cursors.

Params

optionsKnock.FeedClientOptions*Feed options to apply to the fetch. Will override any options specified in the FeedManager constructor.

Returns Knock.Feed

Example


makeBulkStatusUpdate

Updates all of the items within the feed with the given status. Can be passed options to scope the request further. Note: this method returns a BulkUpdate via the Knock API, which is an async operation.

Params

typeKnock.BulkChannelMessageStatusUpdateTypeThe type of update to make in bulk.
optionsKnock.FeedClientOptionsFeed options to scope the bulk update by.

Returns Knock.BulkOperation

Knock.FeedClientOptions

Used to scope a feed request.

Params

beforeStringA cursor to return records before, used for pagination
afterStringA cursor to return records after, used for pagination
page_sizeIntThe maximum number of items to return per page
statusFeedItemScopeOne of either `all`, `unread`, `read`, `unseen`, `seen`
sourceStringScope to a single workflow source for the feed items
tenantStringScope to a single tenant
has_tenantBoolScope to whether the feed items have or do not have a tenant set
archivedFeedItemArchivedScopeScope by archive status. One of `include`, `exclude`, or `only`
trigger_data[String: AnyCodable]Match a set of trigger data on the generated feed messages