
Streamlines creation and scheduling of local notifications for Android and iOS, enhancing user engagement by alerting users at specified times or intervals.
The KLocalNotification package provides a streamlined way to create and schedule local notifications for both Android and iOS platforms within Kotlin Multiplatform projects. It allows developers to set up notifications that inform or alert users at specified times or intervals, enhancing user engagement and facilitating reminders or updates.
KLocalNotification is available on mavenCentral().
implementation("io.github.the-best-is-best:klocal-notification:1.2.2")You can install KIOSNotification directly using Swift Package Manager (SPM) from:
π KIOSNotification-iOS on GitHub
listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach {
it.binaries.framework {
baseName = "ComposeApp"
isStatic = true
export("io.github.the-best-is-best:klocal-notification") // Export KLocalNotification so it's available in the framework
}
}
...
iosMain.dependencies {
...
api("io.github.the-best-is-best:klocal-notification")
}
AppActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
....
AndroidKMessagingChannel.initialization(this)
AndroidKMessagingChannel().initChannel("reminder", "reminder")
AndroidKMessagingChannel().initChannel("reminder1", "reminder1")
...
setContent { App() }
// add this code after setContent
val data = intent.getStringExtra("data")
LocalNotification.notifyPayloadListeners(data)
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
// Handle new intent when activity is already in the background or foreground
val data = intent.getStringExtra("data")
LocalNotification.notifyPayloadListeners(data)
}
}import UserNotifications
@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
// add this
LocalNotification.shared.doInit(userNotificationCenterDelegate: self)
...
return true
}
// Handle notifications while the app is in the foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// Show notifications while app is in the foreground
let userInfo = notification.request.content.userInfo
LocalNotification.shared.notifyNotification(data: userInfo)
completionHandler([.alert, .sound, .badge])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
// i think don't need add this
let userInfo = response.notification.request.content.userInfo
// Do something based on the type of the notification
LocalNotification.shared.notifyPayloadListeners(data: userInfo)
// Always call the completion handler
completionHandler()
}
} var dataNotification by remember { mutableStateOf<Map<Any?, *>>(mapOf("" to "")) }
LaunchedEffect(Unit) {
LocalNotification.payloadFlow.collect {
println("notification received is $it")
dataNotification = it
}
}
val localNotificationRequest
LaunchedEffect(Unit) {
localNotificationRequest.launch()
}
var notificationId: Int? = null
Column(
modifier = Modifier
.fillMaxSize()
.windowInsetsPadding(WindowInsets.safeDrawing)
.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Button(onClick = {
notificationId = Random.nextInt().absoluteValue
println("id is $notificationId")
val currentDateTme =
Clock.System.now().plus(10.seconds).toLocalDateTime(TimeZone.currentSystemDefault())
LocalNotification.showNotification(
config = NotificationConfig(
id = notificationId!!,
idChannel = "reminder",
title = "Test title",
message = "Test Message",
smallIcon = "ic_notification",
data = mapOf("test" to 1),
schedule = true,
dateTime = currentDateTme,
)
)
}) {
Text("Show notification")
}
Button(onClick = {
println("id is removed $notificationId")
if (notificationId != null)
LocalNotification.removeNotification(notificationId!!)
}) {
Text("remove notification")
}
Text("notification received is $dataNotification")
}
The KLocalNotification package provides a streamlined way to create and schedule local notifications for both Android and iOS platforms within Kotlin Multiplatform projects. It allows developers to set up notifications that inform or alert users at specified times or intervals, enhancing user engagement and facilitating reminders or updates.
KLocalNotification is available on mavenCentral().
implementation("io.github.the-best-is-best:klocal-notification:1.2.2")You can install KIOSNotification directly using Swift Package Manager (SPM) from:
π KIOSNotification-iOS on GitHub
listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach {
it.binaries.framework {
baseName = "ComposeApp"
isStatic = true
export("io.github.the-best-is-best:klocal-notification") // Export KLocalNotification so it's available in the framework
}
}
...
iosMain.dependencies {
...
api("io.github.the-best-is-best:klocal-notification")
}
AppActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
....
AndroidKMessagingChannel.initialization(this)
AndroidKMessagingChannel().initChannel("reminder", "reminder")
AndroidKMessagingChannel().initChannel("reminder1", "reminder1")
...
setContent { App() }
// add this code after setContent
val data = intent.getStringExtra("data")
LocalNotification.notifyPayloadListeners(data)
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
// Handle new intent when activity is already in the background or foreground
val data = intent.getStringExtra("data")
LocalNotification.notifyPayloadListeners(data)
}
}import UserNotifications
@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
// add this
LocalNotification.shared.doInit(userNotificationCenterDelegate: self)
...
return true
}
// Handle notifications while the app is in the foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// Show notifications while app is in the foreground
let userInfo = notification.request.content.userInfo
LocalNotification.shared.notifyNotification(data: userInfo)
completionHandler([.alert, .sound, .badge])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
// i think don't need add this
let userInfo = response.notification.request.content.userInfo
// Do something based on the type of the notification
LocalNotification.shared.notifyPayloadListeners(data: userInfo)
// Always call the completion handler
completionHandler()
}
} var dataNotification by remember { mutableStateOf<Map<Any?, *>>(mapOf("" to "")) }
LaunchedEffect(Unit) {
LocalNotification.payloadFlow.collect {
println("notification received is $it")
dataNotification = it
}
}
val localNotificationRequest
LaunchedEffect(Unit) {
localNotificationRequest.launch()
}
var notificationId: Int? = null
Column(
modifier = Modifier
.fillMaxSize()
.windowInsetsPadding(WindowInsets.safeDrawing)
.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Button(onClick = {
notificationId = Random.nextInt().absoluteValue
println("id is $notificationId")
val currentDateTme =
Clock.System.now().plus(10.seconds).toLocalDateTime(TimeZone.currentSystemDefault())
LocalNotification.showNotification(
config = NotificationConfig(
id = notificationId!!,
idChannel = "reminder",
title = "Test title",
message = "Test Message",
smallIcon = "ic_notification",
data = mapOf("test" to 1),
schedule = true,
dateTime = currentDateTme,
)
)
}) {
Text("Show notification")
}
Button(onClick = {
println("id is removed $notificationId")
if (notificationId != null)
LocalNotification.removeNotification(notificationId!!)
}) {
Text("remove notification")
}
Text("notification received is $dataNotification")
}