How the Bark Notification Push Integration Works in SmsForwarder
The Bark notification push integration in SmsForwarder converts incoming messages into HTTP POST requests using the Bark API, with support for automatic verification code detection, optional AES encryption, and Basic Authentication.
The Bark notification push integration is implemented in the open-source Android application SmsForwarder (pppscn/SmsForwarder) as a core sender channel. When a forwarding rule triggers, the framework deserializes the Bark-specific configuration and constructs a compliant HTTP request to your Bark server endpoint. This implementation follows a request-builder pattern shared across all sender types in the application.
Architecture Overview
The Bark integration operates through a centralized dispatcher that routes messages to the appropriate sender utility. When the sender type TYPE_BARK is detected in SendUtils.sendMsgSender, the system instantiates a BarkSetting object from the rule's JSON configuration and delegates transmission to BarkUtils.sendMsg. This utility handles request construction, optional encryption, network transmission via the XHttp library, and response parsing using BarkResult.
Step-by-Step Implementation
Sender Selection and Configuration Parsing
In SendUtils.kt (lines 38–41), the framework iterates through the rule's senderList. When encountering TYPE_BARK, it deserializes the stored JSON into a BarkSetting object defined in BarkSetting.kt. This data class contains all configurable parameters including the server URL, encryption keys, and notification styling options.
Building the HTTP Request
The BarkUtils.sendMsg function (lines 31–63 in BarkUtils.kt) prepares the transmission by:
- Extracting the final title and body from the incoming
MsgInfoobject and any applied templates - Parsing the
setting.serverURL to detect embedded credentials - Adding a Basic Authentication interceptor if the URL contains a
user:passwordcomponent (e.g.,https://user:pass@host/...)
Payload Construction
The utility constructs a mutable map (lines 64–78) that maps to the Bark API specification:
title– The processed message titlebody– The final content stringisArchive– Hardcoded to1to ensure message history retention- Optional fields –
group,icon,level,sound,badge,url, andcall(included only when present in the settings)
The map is then serialized to JSON using Gson().toJson(msgMap).
Automatic Verification Code Detection
If the autoCopy field is empty (lines 80–94), the system applies a comprehensive regular expression to detect verification codes within the message content. Upon detection, it automatically injects two additional fields into the payload:
"copy"– The extracted verification code"autoCopy": 1– Triggers the Bark client to copy the code to the device clipboard
Optional Payload Encryption
For security-sensitive deployments (lines 98–117), the integration supports algorithmic transformation of the payload. When setting.transformation specifies AES, AES/GCM, or similar algorithms:
- The JSON payload is encrypted using the provided
keyand initialization vector (iv) - The encrypted string is transmitted as the
ciphertextparameter - The
Content-Typeheader switches toapplication/x-www-form-urlencoded
Network Request Execution
The actual transmission uses the XHttp library (lines 119–126) with specific configurations:
- Certificate handling –
ignoreHttpsCert()bypasses HTTPS validation errors for self-hosted servers - Response format –
keepJson(true)preserves the raw JSON response - Retry logic – Respects global
retryCountandretryDelaysettings - Logging –
LoggingInterceptorcaptures request/response data for the internal log database
Response Handling
The response processing logic (lines 128–144) distinguishes between success and failure states:
- Success – Parses the JSON into a
BarkResultobject; ifcode == 200, sets log status to 2 (success) - Failure – Non-200 codes or
ApiExceptiontriggers set status to 0 (failure) and logs the error message
In both cases, SendUtils.updateLogs persists the outcome, and SendUtils.senderLogic determines whether to proceed to the next sender in the rule chain.
Key Data Structures
The integration relies on three primary entities:
BarkSetting.kt (lines 5–33) defines the configuration schema:
server: Target Bark API endpointtransformation,key,iv: Encryption parametersgroup,icon,sound,level,badge,url,call: Notification metadataautoCopy: Clipboard automation setting
BarkResult.kt (lines 3–5) models the server response:
code: HTTP status equivalent (200 for success)message: Server response textrequestId: Unique request identifier
Practical Implementation Example
Below is a complete example demonstrating how to manually trigger a Bark push with automatic verification code detection:
import cn.ppps.forwarder.utils.sender.BarkUtils
import cn.ppps.forwarder.entity.setting.BarkSetting
import cn.ppps.forwarder.entity.MsgInfo
// Configure the Bark sender
val barkSetting = BarkSetting(
server = "https://api.example.com/push",
group = "SecurityAlerts",
icon = "https://example.com/icon.png",
sound = "default",
badge = "1",
url = "https://example.com/detail",
level = "active",
title = "SmsForwarder",
transformation = "none", // Use "AES" for encryption
key = "",
iv = "",
call = "",
autoCopy = "" // Empty string triggers automatic code detection
)
// Create a message containing a verification code
val msgInfo = MsgInfo(
type = "sms",
from = "+8613800000000",
content = "Your verification code is 1234",
time = System.currentTimeMillis(),
simInfo = "SIM1",
simSlot = 0,
subId = 0L
)
// Execute the push (normally called automatically by SendUtils)
BarkUtils.sendMsg(barkSetting, msgInfo)
This execution will:
- Detect the verification code "1234" and add
"copy":"1234"to the payload - POST to the specified server with Basic Auth if credentials are embedded in the URL
- Record the transmission result in the SmsForwarder log database
Summary
- The Bark notification push integration routes messages through
SendUtils.sendMsgSenderwhen the sender type isTYPE_BARK, deserializing configuration intoBarkSettingobjects. - Request construction in
BarkUtils.sendMsgsupports Basic Authentication, automatic verification code extraction via regex, and optional AES/GCM encryption. - Payload delivery uses the XHttp library with disabled certificate validation, JSON response preservation, and configurable retry logic.
- Response handling parses
BarkResultobjects to set explicit status codes (2 for success, 0 for failure) and maintains comprehensive transmission logs.
Frequently Asked Questions
How does SmsForwarder detect verification codes automatically for Bark notifications?
When the autoCopy field in BarkSetting is empty, BarkUtils.sendMsg applies a comprehensive regular expression to scan the message content for verification codes (lines 80–94). Upon detection, it injects the "copy" field containing the extracted code and "autoCopy": 1 into the JSON payload, instructing the Bark iOS client to automatically copy the value to the system clipboard.
What encryption methods does the Bark integration support?
The integration supports symmetric encryption algorithms specified in the transformation field of BarkSetting, including AES and AES/GCM (lines 98–117). When enabled, the utility encrypts the entire JSON payload using the provided key and iv, then transmits the ciphertext as a form-urlencoded parameter rather than raw JSON.
How does the Bark integration handle authentication and HTTPS certificates?
The system extracts credentials from the server URL (e.g., https://user:pass@host/...) and attaches a Basic Authentication interceptor to the request (lines 31–63). For HTTPS endpoints, the XHttp configuration explicitly calls ignoreHttpsCert() (line 119) to bypass certificate validation errors, enabling compatibility with self-hosted Bark servers using private certificates.
Where is the Bark sender configuration stored in the SmsForwarder source code?
Configuration logic resides in BarkSetting.kt (lines 5–33), which defines the data class storing all user-configurable parameters. The UI for user input is implemented in BarkFragment.kt, while the core transmission logic exists in BarkUtils.kt. Response parsing uses BarkResult.kt to map server responses to internal status codes.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →