Skip to content

Commit

Permalink
Implementing removeAll and remove functionality for notificaiton (#12)
Browse files Browse the repository at this point in the history
* Hide shown notification by id. (#10)

* Android and iOS setting min sdk (android minSdk 21, ios deploymentTarget 14.1) (#11)

* Implementing remove all notifications functionality

* Updating api

---------

Co-authored-by: ValdZX <[email protected]>
  • Loading branch information
mirzemehdi and ValdZX authored Feb 17, 2024
1 parent 967fc4e commit 43b1413
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 19 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ You can check out [Documentation](https://mirzemehdi.github.io/KMPNotifier) for
## Installation
Before starting you need to setup basic setup using Firebase official guideline (like initializing project in Firebase, adding `google-services.json` to android, `GoogleService-Info.plist` to iOS).

## Minimum Requirements

- **Android:** `minSdkVersion 21`
- **iOS:** `iOS 14.1`


### Gradle Setup
KMPNotifier is available on Maven Central. In your root project `build.gradle.kts` file (or `settings.gradle` file) add `mavenCentral()` to repositories, and add `google-services` plugin to plugins.

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ plugins {

allprojects {
group = "io.github.mirzemehdi"
version = "0.2.0"
version = "0.3.0"
val sonatypeUsername = gradleLocalProperties(rootDir).getProperty("sonatypeUsername")
val sonatypePassword = gradleLocalProperties(rootDir).getProperty("sonatypePassword")
val gpgKeySecret = gradleLocalProperties(rootDir).getProperty("gpgKeySecret")
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ compose = "1.5.4"
compose-plugin = "1.5.11"
compose-compiler = "1.5.4"
agp = "8.1.3"
android-minSdk = "24"
android-minSdk = "21"
android-compileSdk = "34"
android-targetSdk = "34"
androidx-activityCompose = "1.8.0"
Expand Down
4 changes: 2 additions & 2 deletions iosApp/iosApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@
"$(SRCROOT)/../sample/build/xcode-frameworks/$(CONFIGURATION)/$(SDK_NAME)",
);
INFOPLIST_FILE = iosApp/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
IPHONEOS_DEPLOYMENT_TARGET = 14.1;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -368,7 +368,7 @@
"$(SRCROOT)/../sample/build/xcode-frameworks/$(CONFIGURATION)/$(SDK_NAME)",
);
INFOPLIST_FILE = iosApp/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
IPHONEOS_DEPLOYMENT_TARGET = 14.1;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
5 changes: 4 additions & 1 deletion kmpnotifier/api/kmpnotifier.api
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ public final class com/mmk/kmpnotifier/extensions/NotifierManagerExtKt {
}

public abstract interface class com/mmk/kmpnotifier/notification/Notifier {
public abstract fun notify (Ljava/lang/String;Ljava/lang/String;)V
public abstract fun notify (ILjava/lang/String;Ljava/lang/String;)V
public abstract fun notify (Ljava/lang/String;Ljava/lang/String;)I
public abstract fun remove (I)V
public abstract fun removeAll ()V
}

public final class com/mmk/kmpnotifier/notification/NotifierManager {
Expand Down
2 changes: 1 addition & 1 deletion kmpnotifier/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ kotlin {


cocoapods {
ios.deploymentTarget = "11.0"
ios.deploymentTarget = "14.1"
framework {
baseName = "KMPNotifier"
isStatic = true
Expand Down
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(0,Int.MAX_VALUE)
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,17 @@ internal class AndroidNotifier(
}
}.build()

notificationManager.notify(id, notification)
}

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

override fun removeAll() {
val notificationManager = context.notificationManager ?: return
notificationManager.cancelAll()
}

private fun getPendingIntent(deepLink: String = ""): PendingIntent? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@ 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)



/**
* Remove notification by id
* @param id notification id
*/
public fun remove(id:Int)

/**
* Removes all previously shown notifications
* @see remove(id) for removing specific notification.
*/
public fun removeAll()
}
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(0, Int.MAX_VALUE)
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,14 @@ internal class IosNotifier(
error?.let { println("Error showing notification: $error") }
}
}
}

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

override fun removeAll() {
notificationCenter.removeAllDeliveredNotifications()
}

internal class NotificationDelegate : UNUserNotificationCenterDelegateProtocol, NSObject() {
Expand All @@ -60,7 +75,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
2 changes: 2 additions & 0 deletions sample/api/sample.api
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ public final class com/mmk/kmpnotifier/sample/AppKt {
public final class com/mmk/kmpnotifier/sample/ComposableSingletons$AppKt {
public static final field INSTANCE Lcom/mmk/kmpnotifier/sample/ComposableSingletons$AppKt;
public static field lambda-1 Lkotlin/jvm/functions/Function3;
public static field lambda-2 Lkotlin/jvm/functions/Function3;
public fun <init> ()V
public final fun getLambda-1$sample_release ()Lkotlin/jvm/functions/Function3;
public final fun getLambda-2$sample_release ()Lkotlin/jvm/functions/Function3;
}

public final class com/mmk/kmpnotifier/sample/ComposableSingletons$MainActivityKt {
Expand Down
18 changes: 14 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,24 @@ 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")
}
Button(onClick = {notifier.removeAll()}) {
Text("Remove all notifications")
}

Button(enabled = notificationId != 0, onClick = {
notifier.remove(notificationId)
notificationId = 0
}) {
Text("Remove NotificationID #$notificationId")
}

Text(
modifier = Modifier.padding(20.dp),
text = "FirebaseToken: $myPushNotificationToken",
Expand Down

0 comments on commit 43b1413

Please sign in to comment.