Android API reference

The complete API reference for the Knock Android SDK.

In this section, you'll find the documentation for the classes and methods available in the Android 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

publishableKeyStringThe public API key for the Knock environment.
pushChannelIdString (optional)The Knock APNS channel id that you plan to use within your app.
optionsKnockStartupOptions (optional)Optional startup options to configure your Knock instance.

KnockMessagingService & KnockActivity

These classes serves as optional base classes designed to streamline the integration of Knock into your application. By inheriting from KnockMessagingService in your FirebaseMessagingService class and the KnockActivity in your MainActivity class, you gain automatic handling of FCM Push Notification registration and device token management, simplifying the initial setup process for Knock's functionalities.

These classes also provide a set of open helper functions that are intended to facilitate the handling of different Push Notification events such as delivery in the foreground and taps. 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 Android, 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, and user taps.
  • 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

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: KnockUser

Knock.shared.updateUser()

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

Returns: KnockUser

Params

userKnockUserThe 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: 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: ChannelData

Knock.shared.getCurrentDeviceToken()

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

Returns String?

Knock.shared.registerTokenForFCM()

Registers an FCM 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 FCM here.

Params

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

Returns: 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 FCM channel id to associate the device token to.
tokenString OR Datathe FCM device token.

Returns: ChannelData

Knock.shared.isPushPermissionGranted()

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

Returns: Boolean

Knock.shared.requestNotificationPermission()

Requests push notification permissions to the user.

Params

requestCodeIntApplication specific request code to match with a result reported to. Default is `1`.

Preferences

Knock.shared.getAllUserPreferences()

Returns all of the preference sets for the current user.

Returns: 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: PreferenceSet

Knock.shared.setUserPreferences()

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

Params

preferenceIdStringThe ID of the preference set
preferenceSetPreferenceSetThe preferences to update for the preference set

Returns: 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 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 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 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
statusKnockMessageStatusUpdateTypeThe status to set on the messages

Returns [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.
optionsFeedClientOptionsFeed options to apply as defaults.

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

optionsFeedClientOptions (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

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

Returns Feed

Knock.shared.feedManager.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

typeKnockMessageStatusUpdateTypeThe type of update to make in bulk.
optionsFeedClientOptions (optional)Feed options to scope the bulk update by.

Returns BulkOperation

FeedClientOptions

Used to scope a feed request.

Params

beforeString (optional)A cursor to return records before, used for pagination
afterString (optional)A cursor to return records after, used for pagination
page_sizeInt (optional)The maximum number of items to return per page
statusFeedItemScope (optional)One of either `all`, `unread`, `read`, `unseen`, `seen`
sourceString (optional)Scope to a single workflow source for the feed items
tenantString (optional)Scope to a single tenant
has_tenantBoolean (optional)Scope to whether the feed items have or do not have a tenant set
archivedFeedItemArchivedScope (optional)Scope by archive status. One of `include`, `exclude`, or `only`
trigger_dataMap<String, Any> (optional)Match a set of trigger data on the generated feed messages