### Start Local Development Server Source: https://github.com/gojek/courier-android/blob/main/docs/README.md Starts a local development server for live preview. Changes are reflected without server restart. ```bash yarn start ``` -------------------------------- ### Apply Code Formatting with Spotless Source: https://github.com/gojek/courier-android/blob/main/CONTRIBUTING.md Run this Gradle task to automatically format your Kotlin code according to the project's style guide. This should be done before submitting a pull request. ```bash ./gradlew spotlessApply ``` -------------------------------- ### Install Dependencies Source: https://github.com/gojek/courier-android/blob/main/docs/README.md Installs project dependencies using Yarn. ```bash yarn ``` -------------------------------- ### Configure and Connect MqttClient Source: https://github.com/gojek/courier-android/blob/main/README.md Set up MqttConnectOptions with server details, client ID, authentication, and keep-alive settings. Then, establish a connection to the MQTT broker using the configured options. ```kotlin val connectOptions = MqttConnectOptions( serverUris = listOf(ServerUri(SERVER_URI, SERVER_PORT)), clientId = clientId, username = username, keepAlive = KeepAlive( timeSeconds = keepAliveSeconds ), isCleanSession = cleanSessionFlag, password = password ) mqttClient.connect(connectOptions) ``` -------------------------------- ### Courier Client Initialization Source: https://github.com/gojek/courier-android/blob/main/README.md Demonstrates how to initialize the Courier client with MQTT client, stream adapters, and message adapters. ```APIDOC ## Courier Client Initialization ### Description Initializes the Courier client with necessary configurations including the MQTT client, stream adapter factories, and message adapter factories. ### Code ```kotlin val mqttClient = MqttClientFactory.create( context = context, mqttConfiguration = MqttV3Configuration( authenticator = authenticator ) ) val courierConfiguration = Courier.Configuration( client = mqttClient, streamAdapterFactories = listOf(RxJava2StreamAdapterFactory()), messageAdapterFactories = listOf(GsonMessageAdapter.Factory()) ) val courier = Courier(courierConfiguration) val messageService = courier.create() ``` ``` -------------------------------- ### Create MqttClient Instance Source: https://github.com/gojek/courier-android/blob/main/docs/docs/ConnectionSetup.md Instantiate an MqttClient using the MqttClientFactory. Requires the application context and MQTT configuration. ```kotlin val mqttClient = MqttClientFactory.create( context = context, mqttConfiguration = mqttConfiguration ) ``` -------------------------------- ### Connect using MqttClient Source: https://github.com/gojek/courier-android/blob/main/docs/docs/ConnectionSetup.md Establish a connection to the MQTT broker using MqttClient. Configure server URIs, client ID, credentials, keep-alive interval, clean session flag, and ALPN protocols. ```kotlin val alpnProtocol = "mqtt" val connectOptions = MqttConnectOptions.Builder() .serverUris(listof(ServerUri(SERVER_URI, SERVER_PORT))) .clientId(clientId) .userName(username) .password(password) .keepAlive(KeepAlive(timeSeconds = keepAliveSeconds)) .cleanSession(cleanSessionFlag) .alpnProtocols(listOf(Protocol(alpnProtocol))) .build() mqttClient.connect(connectOptions) ``` -------------------------------- ### Create Courier Implementation Source: https://github.com/gojek/courier-android/blob/main/README.md Instantiate MqttClient and configure Courier with necessary adapters. Then, create an implementation of your message service interface using the Courier instance. ```kotlin val mqttClient = MqttClientFactory.create( context = context, mqttConfiguration = MqttV3Configuration( authenticator = authenticator ) ) val courierConfiguration = Courier.Configuration( client = mqttClient, streamAdapterFactories = listOf(RxJava2StreamAdapterFactory()), messageAdapterFactories = listOf(GsonMessageAdapter.Factory()) ) val courier = Courier(courierConfiguration) val messageService = courier.create() ``` -------------------------------- ### Send and Receive Messages using MqttClient Source: https://github.com/gojek/courier-android/blob/main/docs/docs/SendReceiveMessage.md Shows how to send a message with a specific QoS level and add a listener for incoming messages on a given topic using the MqttClient. Only subscribed topics can receive messages. ```kotlin mqttClient.send(message, topic, QoS.TWO) mqttClient.addMessageListener(topic, object : MessageListener { override fun onMessageReceived(mqttMessage: MqttMessage) { print(mqttMessage) } }) ``` -------------------------------- ### Build Static Website Source: https://github.com/gojek/courier-android/blob/main/docs/README.md Generates static website content into the 'build' directory for hosting. ```bash yarn build ``` -------------------------------- ### Create MqttClient Source: https://github.com/gojek/courier-android/blob/main/docs/docs/ConnectionSetup.md Instantiate an MqttClient using the MqttClientFactory. This client is essential for establishing and managing MQTT connections. ```APIDOC ## Create MqttClient ### Description An instance of `MqttClient` needs to be created in order to establish a Courier connection. ### Code Example ```kotlin val mqttClient = MqttClientFactory.create( context = context, mqttConfiguration = mqttConfiguration ) ``` ``` -------------------------------- ### Compile Project with Gradle Source: https://github.com/gojek/courier-android/blob/main/CONTRIBUTING.md Ensure your code compiles successfully by running this Gradle command. This is a prerequisite before submitting changes. ```bash ./gradlew assembleDebug ``` -------------------------------- ### Send/Receive using MqttClient Source: https://github.com/gojek/courier-android/blob/main/docs/docs/SendReceiveMessage.md Illustrates sending messages and setting up listeners for receiving messages using the MqttClient. ```APIDOC ## Send/Receive using MqttClient ### Description This section explains how to send messages directly using `MqttClient` and how to register a listener for receiving messages on a specific topic. ### Sending a Message ```kotlin mqttClient.send(message, topic, QoS.TWO) ``` ### Receiving Messages ```kotlin mqttClient.addMessageListener(topic, object : MessageListener { override fun onMessageReceived(mqttMessage: MqttMessage) { print(mqttMessage) } }) ``` ### Parameters #### Send Method Parameters - **message** (Message) - Required - The message to send. - **topic** (String) - Required - The topic to send the message to. - **qos** (QoS) - Required - The Quality of Service level. #### Add Message Listener Parameters - **topic** (String) - Required - The topic to listen for messages on. - **listener** (MessageListener) - Required - The listener to be invoked when a message is received. ``` -------------------------------- ### Send and Receive Messages using Service Interface Source: https://github.com/gojek/courier-android/blob/main/docs/docs/SendReceiveMessage.md Demonstrates how to send a message and set up a listener for receiving messages using the defined MessageService. Ensure the topic and identifier match the interface definition. ```kotlin messageService.send("user-id", message) messageService.receive("user-id") { message -> print(message) } ``` -------------------------------- ### Subscribe/Unsubscribe through MqttClient Source: https://github.com/gojek/courier-android/blob/main/docs/docs/SubscribeUnsubscribe.md Demonstrates how to subscribe and unsubscribe to topics directly using the MqttClient. ```APIDOC ## Subscribe to topics via MqttClient ### Description Subscribes to one or more topics with specified Quality of Service (QoS) levels using the MqttClient. ### Method subscribe ### Endpoint N/A (Method-based subscription) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```kotlin mqttClient.subscribe("topic1" to QoS.ZERO, "topic2" to QoS.ONE) ``` ### Response #### Success Response (200) Indicates successful subscription. #### Response Example None ## Unsubscribe from topics via MqttClient ### Description Unsubscribes from one or more specified topics using the MqttClient. ### Method unsubscribe ### Endpoint N/A (Method-based unsubscription) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```kotlin mqttClient.unsubscribe("topic1", "topic2") ``` ### Response #### Success Response (200) Indicates successful unsubscription. #### Response Example None ``` -------------------------------- ### Subscribe and Unsubscribe using MqttClient Source: https://github.com/gojek/courier-android/blob/main/docs/docs/SubscribeUnsubscribe.md Provides a direct way to subscribe and unsubscribe from topics using the MqttClient. This method is useful for lower-level control over MQTT connections. ```kotlin mqttClient.subscribe("topic1" to QoS.ZERO, "topic2" to QoS.ONE) mqttClient.unsubscribe("topic1", "topic2") ``` -------------------------------- ### Deploy Website (No SSH) Source: https://github.com/gojek/courier-android/blob/main/docs/README.md Deploys the website without SSH, requiring a GitHub username for deployment. ```bash GIT_USER= yarn deploy ``` -------------------------------- ### Connect using MqttClient Source: https://github.com/gojek/courier-android/blob/main/docs/docs/ConnectionSetup.md Connect to the MQTT broker using the MqttClient with detailed connection options, including server URIs, client ID, authentication, keep-alive interval, clean session flag, and ALPN protocols. ```APIDOC ## Connect using MqttClient ### Description Establishes a connection to the MQTT broker using the `MqttClient` with configurable `MqttConnectOptions`. ### Code Example ```kotlin val alpnProtocol = "mqtt" val connectOptions = MqttConnectOptions.Builder() .serverUris(listof(ServerUri(SERVER_URI, SERVER_PORT))) .clientId(clientId) .userName(username) .password(password) .keepAlive(KeepAlive(timeSeconds = keepAliveSeconds)) .cleanSession(cleanSessionFlag) .alpnProtocols(listOf(Protocol(alpnProtocol))) .build() mqttClient.connect(connectOptions) ``` ``` -------------------------------- ### Deploy Website (SSH) Source: https://github.com/gojek/courier-android/blob/main/docs/README.md Deploys the website using SSH, typically for pushing to a 'gh-pages' branch. ```bash USE_SSH=true yarn deploy ``` -------------------------------- ### Create WorkManagerPingSender Source: https://github.com/gojek/courier-android/blob/main/docs/docs/PingSender.md Instantiate the MQTT ping sender using the WorkPingSenderFactory. This method is suitable for background connection maintenance and requires specific WorkManager configurations. ```kotlin pingSender = WorkPingSenderFactory.createMqttPingSender( context, workManagerPingSenderConfig ) ``` -------------------------------- ### Use MessageService to Subscribe and Unsubscribe Source: https://github.com/gojek/courier-android/blob/main/docs/docs/SubscribeUnsubscribe.md Demonstrates how to use the MessageService interface to subscribe to single and multiple topics, and to unsubscribe. Ensure message formats are consistent or handled by an adapter when using subscribeMultiple. ```kotlin messageService.subscribe("user-id").subscribe { message -> print(message) } messageService.subscribeMultiple(mapOf("topic1" to QoS.ONE, "topic2" to QoS.TWO)).subscribe { message -> print(message) } messageService.unsubscribe("user-id") ``` -------------------------------- ### Publish Artifacts Locally Source: https://github.com/gojek/courier-android/blob/main/CONTRIBUTING.md Use this script to publish artifacts to your local Maven repository. This is useful for local testing of your changes. ```bash ./scripts/publishMavenLocal.sh ``` -------------------------------- ### Send/Receive using Service Interface Source: https://github.com/gojek/courier-android/blob/main/docs/docs/SendReceiveMessage.md Demonstrates sending and receiving messages using the MessageService interface, which handles topic subscriptions and message routing. ```APIDOC ## Send/Receive using Service Interface ### Description This section details how to send and receive messages using the `MessageService` interface. It leverages annotations like `@Receive` and `@Send` to define message handling logic. ### Interface Definition ```kotlin interface MessageService { @Receive(topic = "topic/{id}/receive") fun receive(@Path("id") identifier: String): Observable @Send(topic = "topic/{id}/send", qos = QoS.TWO) fun send(@Path("id") identifier: String, @Data message: Message) } ``` ### Usage Examples ```kotlin // Sending a message messageService.send("user-id", message) // Receiving messages messageService.receive("user-id") { message -> print(message) } ``` ### Parameters #### Path Parameters - **id** (String) - Required - Identifier for the topic. #### Request Body - **message** (Message) - Required - The message object to be sent. ``` -------------------------------- ### Create AlarmPingSender Source: https://github.com/gojek/courier-android/blob/main/docs/docs/PingSender.md Instantiate the MQTT ping sender using the AlarmPingSenderFactory. This is suitable for background connection maintenance, but be aware of the permission requirements for exact alarms on newer Android versions. ```kotlin pingSender = AlarmPingSenderFactory.createMqttPingSender( context, alarmPingSenderConfig ) ``` -------------------------------- ### Add MQTT Chuck Dependency Source: https://github.com/gojek/courier-android/blob/main/docs/docs/MqttChuck.md Add the MQTT Chuck dependency to your project's build.gradle file. ```kotlin dependencies { implementation "com.gojek.courier:chuck-mqtt:x.y.z" } ``` -------------------------------- ### Create Courier Service Implementation Source: https://github.com/gojek/courier-android/blob/main/docs/docs/CourierService.md Use Courier to create an implementation of a service interface by configuring the Courier client, stream adapter factories, and message adapter factories. The create method generates the service instance. ```kotlin val courierConfiguration = Courier.Configuration( client = mqttClient, streamAdapterFactories = listOf(RxJava2StreamAdapterFactory()), messageAdapterFactories = listOf(GsonMessageAdapter.Factory()) ) val courier = Courier(courierConfiguration) val messageService = courier.create() ``` -------------------------------- ### Create HttpAuthenticator Instance Source: https://github.com/gojek/courier-android/blob/main/docs/docs/Authenticator.md Instantiate HttpAuthenticator using the factory class, providing necessary dependencies like Retrofit, API URL, and event/retry handlers. ```kotlin httpAuthenticator = HttpAuthenticatorFactory.create( retrofit = retrofit, apiUrl = TOKEN_AUTH_API, responseHandler = responseHandler, eventHandler = eventHandler, authRetryPolicy = authRetryPolicy ) ``` -------------------------------- ### Create TimerPingSender Source: https://github.com/gojek/courier-android/blob/main/docs/docs/PingSender.md Create an instance of the TimerPingSender using its factory class. This sender is designed for keeping MQTT connections alive only when the application is actively in the foreground. ```kotlin pingSender = TimerPingSenderFactory.create() ``` -------------------------------- ### Subscribe/Unsubscribe through Service Interface Source: https://github.com/gojek/courier-android/blob/main/docs/docs/SubscribeUnsubscribe.md Demonstrates how to subscribe and unsubscribe to topics using the MessageService interface. ```APIDOC ## Subscribe to a topic via Service Interface ### Description Subscribes to a specific topic identified by an ID. ### Method @Subscribe ### Endpoint topic/{id}/receive ### Parameters #### Path Parameters - **id** (string) - Required - The identifier for the topic. #### Query Parameters None #### Request Body None ### Request Example ```kotlin messageService.subscribe("user-id") ``` ### Response #### Success Response (200) - **message** (Message) - The received message. #### Response Example ```json { "message": "Received message content" } ``` ## Subscribe to multiple topics via Service Interface ### Description Subscribes to multiple topics with specified Quality of Service (QoS) levels. ### Method @SubscribeMultiple ### Endpoint N/A (Method-based subscription) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **topics** (Map) - Required - A map where keys are topic names and values are their QoS levels. ### Request Example ```kotlin messageService.subscribeMultiple(mapOf("topic1" to QoS.ONE, "topic2" to QoS.TWO)) ``` ### Response #### Success Response (200) - **message** (Message) - The received message. #### Response Example ```json { "message": "Received message content" } ``` ## Unsubscribe from a topic via Service Interface ### Description Unsubscribes from a specific topic identified by an ID. ### Method @Unsubscribe ### Endpoint topic/{id}/receive ### Parameters #### Path Parameters - **id** (string) - Required - The identifier for the topic to unsubscribe from. #### Query Parameters None #### Request Body None ### Request Example ```kotlin messageService.unsubscribe("user-id") ``` ### Response #### Success Response (200) No content is typically returned upon successful unsubscription. #### Response Example None ``` -------------------------------- ### Apply Kotlin Style to IntelliJ IDEA Source: https://github.com/gojek/courier-android/blob/main/CONTRIBUTING.md Generate IntelliJ IDEA/Android Studio Kotlin style files to ensure your IDE adheres to the project's coding rules. This helps maintain consistent code formatting. ```bash ./gradlew ktlintApplyToIdea ``` -------------------------------- ### Add Courier Dependencies Source: https://github.com/gojek/courier-android/blob/main/docs/docs/Installation.md Declare the Courier library and its adapters in your app's build.gradle file. Ensure you use the latest version available on Maven Central. ```kotlin repositories { mavenCentral() } dependencies { implementation "com.gojek.courier:courier:x.y.z" implementation "com.gojek.courier:courier-message-adapter-gson:x.y.z" implementation "com.gojek.courier:courier-stream-adapter-rxjava2:x.y.z" } ``` -------------------------------- ### Generate API Dumps with Binary Compatibility Validator Source: https://github.com/gojek/courier-android/blob/main/CONTRIBUTING.md If your changes affect public APIs, use this command to generate updated API dumps. Commit these changes to track binary compatibility. ```bash ./gradlew apiDump ``` -------------------------------- ### Set UserProperties in MqttConnectionOptions Source: https://github.com/gojek/courier-android/blob/main/docs/docs/NonStandardOptions.md Use the userProperties builder to include custom key-value pairs in the CONNECT packet. This is a non-standard option for MQTT v3.1.1 and requires broker support. ```kotlin val connectOptions = MqttConnectOptions.Builder() .serverUris(listOf(ServerUri(SERVER_URI, SERVER_PORT))) .clientId(clientId) ... .userProperties( userProperties = mapOf( "key1" to "value1", "key2" to "value2" ) ) .build() mqttClient.connect(connectOptions) ``` -------------------------------- ### Set UserProperties in MqttConnectionOptions (Kotlin) Source: https://github.com/gojek/courier-android/blob/main/README.md Use this option to send user-properties in the CONNECT packet for MQTT v3.1.1. Note that this is a non-standard option and requires broker support. ```kotlin val connectOptions = MqttConnectOptions( serverUris = listOf(ServerUri(SERVER_URI, SERVER_PORT)), clientId = clientId, ... userPropertiesMap = mapOf( "key1" to "value1", "key2" to "value2" ) ) mqttClient.connect(connectOptions) ``` -------------------------------- ### Enable MQTT Chuck Interceptor Source: https://github.com/gojek/courier-android/blob/main/docs/docs/MqttChuck.md Pass the MqttChuckInterceptor to your MqttConfiguration to enable MQTT Chuck for your courier connection. ```kotlin mqttConfiguration = MqttV3Configuration( mqttInterceptorList = listOf(MqttChuckInterceptor(context, mqttChuckConfig)) ) ``` -------------------------------- ### Send and Receive Messages Source: https://github.com/gojek/courier-android/blob/main/README.md Utilize the message service to send messages to a specified topic and receive messages. The receive function can be used with a callback to process incoming messages. ```kotlin messageService.send("user-id", message) messageService.receive("user-id") { print(message) } ``` -------------------------------- ### Add TimerPingSender Dependency Source: https://github.com/gojek/courier-android/blob/main/docs/docs/PingSender.md Add this dependency to use the TimerPingSender for maintaining MQTT connections. This implementation is best suited for scenarios where the app is in the foreground. ```kotlin dependencies { implementation "com.gojek.courier:timer-pingsender:x.y.z" } ``` -------------------------------- ### Define MessageService Interface for Subscribing Source: https://github.com/gojek/courier-android/blob/main/docs/docs/SubscribeUnsubscribe.md Defines the interface for subscribing and unsubscribing to topics using annotations. Use this to declare your message handling methods. ```kotlin interface MessageService { @Subscribe(topic = "topic/{id}/receive", qos = QoS.ONE) fun subscribe(@Path("id") identifier: String): Observable @SubscribeMultiple fun subscribeMultiple(@TopicMap topics: Map): Observable @Unsubscribe(topics = ["topic/{id}/receive"]) fun unsubscribe(@Path("id") identifier: String) } ``` -------------------------------- ### Add Http Authenticator Dependency Source: https://github.com/gojek/courier-android/blob/main/docs/docs/Authenticator.md Include the courier-auth-http dependency to use the Http Authenticator. Replace 'x.y.z' with the actual version. ```kotlin dependencies { implementation "com.gojek.courier:courier-auth-http:x.y.z" } ``` -------------------------------- ### Implement Custom MessageAdapter Factory Source: https://github.com/gojek/courier-android/blob/main/docs/docs/MessageStreamAdapters.md Implement the MessageAdapter.Factory interface to create custom message adapters. This factory is responsible for creating instances of your custom MessageAdapter. ```kotlin class MyCustomMessageAdapterFactory : MessageAdapter.Factory { override fun create(type: Type, annotations: Array): MessageAdapter<*> { return MyCustomMessageAdapter() } } private class MyCustomMessageAdapter constructor() : MessageAdapter { override fun fromMessage(topic: String, message: Message): T { // convert message to custom type } override fun toMessage(topic: String, data: T): Message { // convert custom type to message } override fun contentType(): String { // content-type supported by this adapter. } } ``` -------------------------------- ### Implement Custom StreamAdapter Factory Source: https://github.com/gojek/courier-android/blob/main/docs/docs/MessageStreamAdapters.md Implement the StreamAdapter.Factory interface to create custom stream adapters. This factory is used to adapt streams to different stream types. ```kotlin class MyCustomStreamAdapterFactory : StreamAdapter.Factory { override fun create(type: Type): StreamAdapter { return MyCustomStreamAdapter() } } private class MyCustomStreamAdapter : StreamAdapter { override fun adapt(stream: Stream): Any { // convert stream to custom stream } } ``` -------------------------------- ### MessageService Interface Source: https://github.com/gojek/courier-android/blob/main/docs/docs/CourierService.md Defines the service interface for various messaging actions including Send, Receive, Subscribe, SubscribeMultiple, and Unsubscribe. Each method is annotated to specify the MQTT topic, QoS level, and data handling. ```APIDOC ## MessageService Interface ### Description Defines the service interface for messaging operations. ### Methods #### `receive` - **Purpose**: Receives messages from a specified topic. - **Annotation**: `@Receive(topic = "topic/{id}/receive")` - **Parameters**: - `identifier` (String) - `@Path("id")` - Identifies the specific topic path. - **Returns**: `Observable` - An observable stream of received messages. #### `send` - **Purpose**: Sends a message to a specified topic. - **Annotation**: `@Send(topic = "topic/{id}/send", qos = QoS.TWO)` - **Parameters**: - `identifier` (String) - `@Path("id")` - Identifies the specific topic path. - `message` (Message) - `@Data` - The message object to send. #### `subscribe` (single topic) - **Purpose**: Subscribes to a single topic for receiving messages. - **Annotation**: `@Subscribe(topic = "topic/{id}/receive", qos = QoS.ONE)` - **Parameters**: - `identifier` (String) - `@Path("id")` - Identifies the specific topic path. - **Returns**: `Observable` - An observable stream of messages from the subscribed topic. #### `subscribe` (multiple topics) - **Purpose**: Subscribes to multiple topics simultaneously. - **Annotation**: `@SubscribeMultiple` - **Parameters**: - `topicMap` (Map) - `@TopicMap` - A map where keys are topic names and values are their corresponding QoS levels. - **Returns**: `Observable` - An observable stream of messages from all subscribed topics. #### `unsubscribe` - **Purpose**: Unsubscribes from specified topics. - **Annotation**: `@Unsubscribe(topics = ["topic/{id}/receive"])` - **Parameters**: - `identifier` (String) - `@Path("id")` - Identifies the specific topic path to unsubscribe from. ``` -------------------------------- ### Add AlarmPingSender Dependency Source: https://github.com/gojek/courier-android/blob/main/docs/docs/PingSender.md Include this dependency to utilize the AlarmPingSender for maintaining MQTT connections in the background. Note that on Android 12 and above, scheduling exact alarms requires user permission. ```kotlin dependencies { implementation "com.gojek.courier:alarm-pingsender:x.y.z" } ``` -------------------------------- ### Define MessageService Interface Source: https://github.com/gojek/courier-android/blob/main/docs/docs/SendReceiveMessage.md Defines the interface for sending and receiving messages using the service interface. Use this to declare message sending and receiving methods with specified topics and quality of service. ```kotlin interface MessageService { @Receive(topic = "topic/{id}/receive") fun receive(@Path("id") identifier: String): Observable @Send(topic = "topic/{id}/send", qos = QoS.TWO) fun send(@Path("id") identifier: String, @Data message: Message) } ``` -------------------------------- ### Add WorkManagerPingSender Dependency Source: https://github.com/gojek/courier-android/blob/main/docs/docs/PingSender.md Include this dependency to use the WorkManagerPingSender for maintaining MQTT connections, especially when the app is in the background. Requires WorkManager version 2.7.0 and compileSdkVersion 31 or higher. ```kotlin dependencies { implementation "com.gojek.courier:workmanager-pingsender:x.y.z" } ``` -------------------------------- ### Add WorkManager-2.6.0 PingSender Dependency Source: https://github.com/gojek/courier-android/blob/main/docs/docs/PingSender.md Use this dependency for WorkManager-based ping sending compatible with apps targeting Android versions lower than 31. It helps maintain MQTT connections when the app is in the background. ```kotlin dependencies { implementation "com.gojek.courier:workmanager-2.6.0-pingsender:x.y.z" } ``` -------------------------------- ### Define Message Service Interface Source: https://github.com/gojek/courier-android/blob/main/docs/docs/CourierService.md Declare a service interface for various message actions using annotations like @Receive, @Send, @Subscribe, @SubscribeMultiple, and @Unsubscribe. Path and data parameters are annotated with @Path and @Data respectively. ```kotlin interface MessageService { @Receive(topic = "topic/{id}/receive") fun receive(@Path("id") identifier: String): Observable @Send(topic = "topic/{id}/send", qos = QoS.TWO) fun send(@Path("id") identifier: String, @Data message: Message) @Subscribe(topic = "topic/{id}/receive", qos = QoS.ONE) fun subscribe(@Path("id") identifier: String): Observable @SubscribeMultiple fun subscribe(@TopicMap topicMap: Map): Observable @Unsubscribe(topics = ["topic/{id}/receive"]) fun unsubscribe(@Path("id") identifier: String) } ``` -------------------------------- ### Disconnect MqttClient Source: https://github.com/gojek/courier-android/blob/main/docs/docs/ConnectionSetup.md Gracefully disconnect the MqttClient from the MQTT broker. ```kotlin mqttClient.disconnect() ``` -------------------------------- ### Subscribe and Unsubscribe Messages Source: https://github.com/gojek/courier-android/blob/main/README.md Use the generated message service to subscribe to messages on a specific topic and unsubscribe when no longer needed. The subscribe function returns an Observable that emits incoming messages. ```kotlin messageService.subscribe("user-id").subscribe { print(message) } messageService.unsubscribe("user-id") ``` -------------------------------- ### Define Message Service Interface Source: https://github.com/gojek/courier-android/blob/main/README.md Declare a service interface using Courier annotations for message operations like receive, send, subscribe, and unsubscribe. Use @Path for topic parameters and @Data for message payloads. ```kotlin interface MessageService { @Receive(topic = "topic/{id}/receive") fun receive(@Path("id") identifier: String): Observable @Send(topic = "topic/{id}/send", qos = QoS.TWO) fun send(@Path("id") identifier: String, @Data message: Message) @Subscribe(topic = "topic/{id}/receive", qos = QoS.ONE) fun subscribe(@Path("id") identifier: String): Observable @Unsubscribe(topics = ["topic/{id}/receive"]) fun unsubscribe(@Path("id") identifier: String) } ``` -------------------------------- ### Disconnect using MqttClient Source: https://github.com/gojek/courier-android/blob/main/docs/docs/ConnectionSetup.md Gracefully disconnects the MqttClient from the MQTT broker, terminating the active connection. ```APIDOC ## Disconnect using MqttClient ### Description Terminates the connection to the MQTT broker. ### Code Example ```kotlin mqttClient.disconnect() ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.