Skip to content

Commit

Permalink
Hide shown notification by id. (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValdZX authored Feb 16, 2024
1 parent 967fc4e commit 0efc9d0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ internal class AndroidNotifier(
private val permissionUtil: PermissionUtil,
) : Notifier {

override fun notify(title: String, body: String) {
override fun notify(title: String, body: String): Int {
val notificationID = Random.nextInt()
notify(notificationID, title, body)
return notificationID
}

override fun notify(id: Int, title: String, body: String) {
permissionUtil.hasNotificationPermission {
if (it.not())
Log.w("AndroidNotifier", "You need to ask runtime " +
"notification permission (Manifest.permission.POST_NOTIFICATIONS) in your activity")
Log.w(
"AndroidNotifier", "You need to ask runtime " +
"notification permission (Manifest.permission.POST_NOTIFICATIONS) in your activity"
)
}
val notificationManager = context.notificationManager ?: return
val pendingIntent = getPendingIntent()
Expand All @@ -44,9 +52,12 @@ internal class AndroidNotifier(
}
}.build()

notificationManager.notify(id, notification)
}

val notificationID = Random.nextInt()
notificationManager.notify(notificationID, notification)
override fun hide(id: Int) {
val notificationManager = context.notificationManager ?: return
notificationManager.cancel(id)
}

private fun getPendingIntent(deepLink: String = ""): PendingIntent? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ public interface Notifier {
* Sends local notification to device
* @param title Title part
* @param body Body part
* @return notification id
*/
public fun notify(title: String, body: String)
public fun notify(title: String, body: String): Int

/**
* Sends local notification to device with id
* @param id notification id
* @param title Title part
* @param body Body part
*/
public fun notify(id:Int, title: String, body: String)



/**
* Hide notification by id
* @param id notification id
*/
public fun hide(id:Int)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@ import platform.UserNotifications.UNTimeIntervalNotificationTrigger
import platform.UserNotifications.UNUserNotificationCenter
import platform.UserNotifications.UNUserNotificationCenterDelegateProtocol
import platform.darwin.NSObject
import kotlin.random.Random

internal class IosNotifier(
private val permissionUtil: IosPermissionUtil,
private val notificationCenter: UNUserNotificationCenter,
) : Notifier {

override fun notify(title: String, body: String) {

override fun notify(title: String, body: String): Int {
val notificationID = Random.nextInt()
notify(notificationID, title, body)
return notificationID
}

override fun notify(id: Int, title: String, body: String) {
permissionUtil.askNotificationPermission {
val notificationContent = UNMutableNotificationContent().apply {
setTitle(title)
Expand All @@ -27,7 +35,7 @@ internal class IosNotifier(
}
val trigger = UNTimeIntervalNotificationTrigger.triggerWithTimeInterval(1.0, false)
val notificationRequest = UNNotificationRequest.requestWithIdentifier(
identifier = "general-notification-id",
identifier = id.toString(),
content = notificationContent,
trigger = trigger
)
Expand All @@ -36,7 +44,13 @@ internal class IosNotifier(
error?.let { println("Error showing notification: $error") }
}
}
}

override fun hide(id: Int) {
notificationCenter
.removeDeliveredNotificationsWithIdentifiers(
listOf(id.toString())
)
}

internal class NotificationDelegate : UNUserNotificationCenterDelegateProtocol, NSObject() {
Expand All @@ -60,7 +74,7 @@ internal class IosNotifier(
) {
// FIRMessaging.messaging()
// .appDidReceiveMessage(didReceiveNotificationResponse.notification.request.content.userInfo)
val userInfo =willPresentNotification.request.content.userInfo
val userInfo = willPresentNotification.request.content.userInfo
NotifierManager.onApplicationDidReceiveRemoteNotification(userInfo)
withCompletionHandler(IosPermissionUtil.NOTIFICATION_PERMISSIONS)
}
Expand Down
14 changes: 10 additions & 4 deletions sample/src/commonMain/kotlin/com/mmk/kmpnotifier/sample/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.mmk.kmpnotifier.notification.NotifierManager
import com.mmk.kmpnotifier.notification.PayloadData

@Composable
fun App() {
Expand All @@ -40,13 +39,20 @@ fun App() {
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
val notifier = remember { NotifierManager.getLocalNotifier() }
var notificationId by remember { mutableStateOf(0) }
Button(onClick = {
val notifier = NotifierManager.getLocalNotifier()
notifier.notify("Title", "bodyMessage")

notificationId = notifier.notify("Title", "bodyMessage")
}) {
Text("Send Local Notification")
}
if (notificationId != 0) {
Button(onClick = {
notifier.hide(notificationId)
}) {
Text("Hide Notification #$notificationId")
}
}
Text(
modifier = Modifier.padding(20.dp),
text = "FirebaseToken: $myPushNotificationToken",
Expand Down

0 comments on commit 0efc9d0

Please sign in to comment.