Android API reference
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
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
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
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
Channels/Push Notifications
Knock.shared.getUserChannelData()
Returns the channel data for the current user on the channel specified with channelId
.
Params
Returns: ChannelData
Knock.shared.updateUserChannelData()
Updates the channel data for the current user on the channel specified with channelId
.
Params
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
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
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
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
Returns: PreferenceSet
Knock.shared.setUserPreferences()
Updates the preference set specified by the preferenceId
with the new preferenceSet
.
Params
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
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
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
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
Returns [KnockMessage]
FeedManager
Knock.FeedManager.init()
Creates a new instance of a FeedManager
for interacting with a user's in-app notification feed.
Params
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
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.
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
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
Returns BulkOperation
FeedClientOptions
Used to scope a feed request.
Params