Skip to content

Commit

Permalink
feat: 发送通知
Browse files Browse the repository at this point in the history
  • Loading branch information
xfqwdsj committed Jan 21, 2024
1 parent dd8a5cd commit 5dcedfd
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build-number.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
# You should have received a copy of the GNU General Public License along
# with Fhraise. If not, see <https://www.gnu.org/licenses/>.
#
buildNumber=28
buildNumber=30
1 change: 1 addition & 0 deletions composeApp/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />

<application
android:allowBackup="true"
Expand Down
27 changes: 27 additions & 0 deletions composeApp/src/androidMain/kotlin/Notification.android.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* This file is part of Fhraise.
* Copyright (c) 2024 HSAS Foodies. All Rights Reserved.
*
* Fhraise is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Fhraise is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with Fhraise. If not, see <https://www.gnu.org/licenses/>.
*/

import androidx.core.app.NotificationCompat

object Notification {
var send: (channel: String, title: String, message: String, priority: Int) -> Unit = { _, _, _, _ -> }
}

actual fun sendVerifyCodeNotification(code: String) {
Notification.send("verifyCode", "验证码", code, NotificationCompat.PRIORITY_HIGH)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

package xyz.xfqlittlefan.fhraise

import Notification
import Permission
import android.app.NotificationManager
import android.content.pm.PackageManager
import android.graphics.Color
import android.os.Build
Expand All @@ -32,6 +34,7 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import com.arkivanov.decompose.defaultComponentContext
Expand Down Expand Up @@ -67,7 +70,7 @@ class MainActivity : ComponentActivity() {
val verifyCodeChannelId = "verifyCode"
val verifyCodeChannelName = "模拟验证码"
val verifyCodeChannelDescription = "模拟验证码的通知"
val verifyCodeChannelImportance = android.app.NotificationManager.IMPORTANCE_HIGH
val verifyCodeChannelImportance = NotificationManager.IMPORTANCE_HIGH
val verifyCodeChannel = android.app.NotificationChannel(
verifyCodeChannelId, verifyCodeChannelName, verifyCodeChannelImportance
).apply {
Expand All @@ -85,13 +88,13 @@ class MainActivity : ComponentActivity() {
}

Permission.checkNotificationPermissionGranted = {
ContextCompat.checkSelfPermission(
Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU || ContextCompat.checkSelfPermission(
this, android.Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED
}

Permission.requestNotificationPermission = {
if (Permission.checkNotificationPermissionGranted() != true) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && Permission.checkNotificationPermissionGranted() != true) {
suspendCoroutine {
notificationPermissionRequestContinuation = it
notificationPermissionRequestLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS)
Expand All @@ -101,6 +104,21 @@ class MainActivity : ComponentActivity() {
}
}

Notification.send = { channel, title, message, priority ->
val notification = NotificationCompat.Builder(this, "verifyCode").apply {
setSmallIcon(R.drawable.ic_launcher_foreground)
setContentTitle(title)
setContentText(message)
setPriority(priority)
setCategory(NotificationCompat.CATEGORY_MESSAGE)
setDefaults(NotificationCompat.DEFAULT_SOUND or NotificationCompat.DEFAULT_VIBRATE)
}.build()
val notificationManager = getSystemService(android.app.NotificationManager::class.java)
val id =
(channel.hashCode() shl 48) or (title.hashCode() shl 32) or (message.hashCode() shl 16) or priority.hashCode()
notificationManager.notify(id, notification)
}

val rootComponent = AppRootComponent(componentContext = defaultComponentContext())

setContent {
Expand Down
19 changes: 19 additions & 0 deletions composeApp/src/commonMain/kotlin/Notification.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* This file is part of Fhraise.
* Copyright (c) 2024 HSAS Foodies. All Rights Reserved.
*
* Fhraise is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Fhraise is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with Fhraise. If not, see <https://www.gnu.org/licenses/>.
*/

expect fun sendVerifyCodeNotification(code: String)
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import data.componentScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import notificationPermissionGranted
import sendVerifyCodeNotification

interface SignInComponent : AppComponentContext {
val nextColorMode: ColorMode
Expand Down Expand Up @@ -170,13 +171,20 @@ class AppSignInComponent(
if (notificationPermissionGranted != true) {
componentScope.launch {
val result = requestAppNotificationPermission()
if (result == false) {
if (result != false) {
doSendVerifyCode()
} else {
verifyCode = "114514" // TODO
verifyCodeSentSnackbarJob?.cancel()
snackbarHostState.showSnackbar("通知权限未授予,验证码已自动填写", withDismissAction = true)
}
}
}
doSendVerifyCode()
}

private fun doSendVerifyCode() {
sendVerifyCodeNotification("114514") // TODO
verifyCodeSentSnackbarJob?.cancel()
verifyCodeSentSnackbarJob = componentScope.launch {
snackbarHostState.showSnackbar("验证码已发送", withDismissAction = true)
Expand Down
27 changes: 27 additions & 0 deletions composeApp/src/desktopMain/kotlin/Notification.desktop.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* This file is part of Fhraise.
* Copyright (c) 2024 HSAS Foodies. All Rights Reserved.
*
* Fhraise is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Fhraise is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with Fhraise. If not, see <https://www.gnu.org/licenses/>.
*/

import androidx.compose.ui.window.Notification.Type

object Notification {
var send: (title: String, message: String, type: Type) -> Unit = { _, _, _ -> }
}

actual fun sendVerifyCodeNotification(code: String) {
Notification.send("验证码", code, Type.Info)
}
15 changes: 12 additions & 3 deletions composeApp/src/desktopMain/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState
import androidx.compose.ui.window.*
import com.arkivanov.decompose.DefaultComponentContext
import com.arkivanov.decompose.ExperimentalDecomposeApi
import com.arkivanov.decompose.extensions.compose.lifecycle.LifecycleController
Expand All @@ -46,6 +44,17 @@ fun main() {

application {
val windowState = rememberWindowState(size = DpSize(width = 1000.dp, height = 800.dp))
val trayState = rememberTrayState()

Notification.send = { title, message, type ->
trayState.sendNotification(Notification(title, message, type))
}

Tray(
icon = painterResource(DrawableResource("drawable/fhraise_logo.xml")),
state = trayState,
tooltip = "Fhraise",
)

LifecycleController(lifecycleRegistry, windowState)

Expand Down
26 changes: 26 additions & 0 deletions composeApp/src/wasmJsMain/kotlin/Notification.wasmJs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* This file is part of Fhraise.
* Copyright (c) 2024 HSAS Foodies. All Rights Reserved.
*
* Fhraise is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Fhraise is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with Fhraise. If not, see <https://www.gnu.org/licenses/>.
*/

import org.w3c.notifications.Notification
import org.w3c.notifications.NotificationOptions

actual fun sendVerifyCodeNotification(code: String) {
Notification(
title = "验证码", options = NotificationOptions(body = code)
)
}
19 changes: 19 additions & 0 deletions shared/src/jvmMain/kotlin/Platform.jvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,22 @@ class JVMPlatform : Platform {
}

actual fun getPlatform(): Platform = JVMPlatform()

enum class DesktopPlatform {
Linux, Windows, MacOS, Unknown;

companion object {
/**
* Identify OS on which the application is currently running.
*/
val Current: DesktopPlatform by lazy {
val name = System.getProperty("os.name")
when {
name?.startsWith("Linux") == true -> Linux
name?.startsWith("Win") == true -> Windows
name == "Mac OS X" -> MacOS
else -> Unknown
}
}
}
}

0 comments on commit 5dcedfd

Please sign in to comment.