Skip to content

Commit 09a6033

Browse files
committed
-Local Notifications Added
1 parent 84478cf commit 09a6033

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

alert-kmp/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ mavenPublishing {
7777
coordinates(
7878
groupId = "io.github.khubaibkhan4",
7979
artifactId = "alert-kmp",
80-
version = "1.0.8"
80+
version = "2.0.0"
8181
)
8282

8383
// Configure POM metadata for the published artifact

alert-kmp/src/androidMain/kotlin/NativeNotify.android.kt

+22-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import android.app.AlertDialog
44
import android.app.NotificationChannel
55
import android.app.NotificationManager
66
import android.content.Context
7+
import android.content.Intent
78
import android.os.Build
9+
import android.provider.Settings
810
import android.util.Log
911
import android.widget.Toast
1012
import androidx.core.app.NotificationCompat
13+
import androidx.core.app.NotificationManagerCompat
1114

1215
actual fun Notify(message: String, duration: NotificationDuration) {
1316
val context = AppContext.get()
@@ -40,6 +43,16 @@ actual fun createNotification(type: NotificationType): Notification = when (type
4043
val context = AppContext.get()
4144
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
4245

46+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
47+
NotificationManagerCompat.from(context).areNotificationsEnabled().also { areEnabled ->
48+
if (!areEnabled) {
49+
Log.e("Notification", "Notifications are disabled. Requesting permission...")
50+
openNotificationSettings(context)
51+
return
52+
}
53+
}
54+
}
55+
4356
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
4457
val channel = NotificationChannel(
4558
"top_notification_channel",
@@ -53,7 +66,7 @@ actual fun createNotification(type: NotificationType): Notification = when (type
5366

5467
val notification = NotificationCompat.Builder(context, "top_notification_channel")
5568
.setSmallIcon(android.R.drawable.ic_dialog_info)
56-
.setContentTitle("Top Notification")
69+
.setContentTitle(title ?: "Top Notification")
5770
.setContentText(message)
5871
.setPriority(NotificationCompat.PRIORITY_HIGH)
5972
.setDefaults(NotificationCompat.DEFAULT_ALL)
@@ -97,4 +110,12 @@ actual fun createNotification(type: NotificationType): Notification = when (type
97110
}
98111
}
99112
else -> throw IllegalArgumentException("Unsupported notification type")
113+
}
114+
115+
private fun openNotificationSettings(context: Context) {
116+
val intent = Intent().apply {
117+
action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
118+
putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
119+
}
120+
context.startActivity(intent)
100121
}

alert-kmp/src/iosMain/kotlin/NativeNotify.ios.kt

+49
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import kotlinx.cinterop.ExperimentalForeignApi
22
import platform.CoreGraphics.CGRectMake
33
import platform.Foundation.NSLog
44
import platform.Foundation.NSTimer
5+
import platform.Foundation.NSUUID
56
import platform.UIKit.NSTextAlignmentCenter
67
import platform.UIKit.UIAlertAction
78
import platform.UIKit.UIAlertActionStyleCancel
@@ -18,6 +19,13 @@ import platform.UIKit.UILabel
1819
import platform.UIKit.UIView
1920
import platform.UIKit.UIViewAnimationOptionCurveEaseInOut
2021
import platform.UIKit.UIViewContentMode
22+
import platform.UserNotifications.UNAuthorizationOptionAlert
23+
import platform.UserNotifications.UNAuthorizationOptionSound
24+
import platform.UserNotifications.UNMutableNotificationContent
25+
import platform.UserNotifications.UNNotificationRequest
26+
import platform.UserNotifications.UNNotificationSound
27+
import platform.UserNotifications.UNTimeIntervalNotificationTrigger
28+
import platform.UserNotifications.UNUserNotificationCenter
2129
import platform.darwin.dispatch_async
2230
import platform.darwin.dispatch_get_main_queue
2331

@@ -52,6 +60,47 @@ actual fun Notify(message: String, duration: NotificationDuration) {
5260

5361
@OptIn(ExperimentalForeignApi::class)
5462
actual fun createNotification(type: NotificationType): Notification = when (type) {
63+
NotificationType.TOP -> object: Notification(){
64+
override fun show(message: String, title: String?, duration: NotificationDuration) {
65+
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(
66+
options = UNAuthorizationOptionAlert or UNAuthorizationOptionSound
67+
) { granted, error ->
68+
if (granted) {
69+
println("Permission granted")
70+
71+
val content = UNMutableNotificationContent().apply {
72+
this.setTitle(title ?: "Notification")
73+
this.setBody(message)
74+
this.setSound(UNNotificationSound.defaultSound)
75+
}
76+
77+
val trigger = UNTimeIntervalNotificationTrigger.triggerWithTimeInterval(
78+
timeInterval = 5.0,
79+
repeats = false
80+
)
81+
82+
val request = UNNotificationRequest.requestWithIdentifier(
83+
identifier = NSUUID().UUIDString,
84+
content = content,
85+
trigger = trigger
86+
)
87+
88+
89+
UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest(
90+
request
91+
) { error ->
92+
error?.let {
93+
println("Error scheduling notification: ${it.localizedDescription}")
94+
} ?: println("Notification scheduled")
95+
}
96+
} else {
97+
error?.let {
98+
println("Permission error: ${it.localizedDescription}")
99+
} ?: println("Permission denied")
100+
}
101+
}
102+
}
103+
}
55104
NotificationType.ALERT -> object : Notification() {
56105
override fun show(message: String, title: String?, duration: NotificationDuration) {
57106
val viewController = UIApplication.sharedApplication.keyWindow?.rootViewController

0 commit comments

Comments
 (0)