-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainActivity.kt
89 lines (76 loc) · 3.22 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.notification.man
import android.os.Build
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.notification.man.databinding.ActivityMainBinding
import com.notificationman.library.NotificationMan
import com.notificationman.library.config.NotificationManChannelConfig
import com.notificationman.library.model.NotificationImportanceLevel
import com.notificationman.library.model.NotificationTypes
import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {
companion object {
private const val THUMBNAIL_URL =
"https://storage.googleapis.com/gweb-uniblog-publish-prod/images/Android_robot.max-500x500.png"
}
private lateinit var binding: ActivityMainBinding
private val notificationPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) {}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
requestNotificationPermission()
binding.buttonFire.setOnClickListener {
val title = binding.editTextTitle.text.toString().trim()
val desc = binding.editTextDesc.text.toString().trim()
val timeInterval = binding.editTextTimeInterval.text.toString().trim().toLongOrNull()
fireNotificationMan(
title = title,
desc = desc,
timeInterval = timeInterval
)
}
binding.buttonCoolDownLast.setOnClickListener {
lifecycleScope.launch {
NotificationMan.coolDownLatestFire(this@MainActivity)
}
}
binding.buttonCoolDownAll.setOnClickListener {
lifecycleScope.launch {
NotificationMan.coolDownAllFires(this@MainActivity)
}
}
}
private fun requestNotificationPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
notificationPermissionLauncher.launch(
android.Manifest.permission.POST_NOTIFICATIONS
)
}
private fun fireNotificationMan(
title: String?,
desc: String?,
timeInterval: Long?,
) {
NotificationMan
.Builder(context = this, classPathWillBeOpen = "com.notification.man.MainActivity")
.setTitle(title = title)
.setDescription(desc = desc)
.setThumbnailUrl(thumbnailUrl = THUMBNAIL_URL)
.setTimeInterval(timeInterval = timeInterval)
.setNotificationType(type = NotificationTypes.IMAGE.type)
.setNotificationChannelConfig(config = createNotificationManChannelConfig())
.fire()
}
private fun createNotificationManChannelConfig() =
NotificationManChannelConfig
.Builder()
.setChannelId(id = "notification-man-channel")
.setChannelName(name = "custom-channel-name")
.setImportanceLevel(level = NotificationImportanceLevel.HIGH)
.setShowBadge(shouldShow = true)
.build()
}