generated from ajou4095/template-android
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/common/Constants.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
package ac.dnd.bookkeeping.android.presentation.common | ||
|
||
const val REGEX_EMAIL = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$" | ||
|
||
const val CHANNEL_1 = "1" | ||
const val CHANNEL_GROUP_1 = "1" |
72 changes: 72 additions & 0 deletions
72
...n/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/common/util/NotificationUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package ac.dnd.bookkeeping.android.presentation.common.util | ||
|
||
import ac.dnd.bookkeeping.android.presentation.R | ||
import android.Manifest | ||
import android.annotation.SuppressLint | ||
import android.app.NotificationManager | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Build | ||
import android.provider.Settings | ||
import androidx.annotation.DrawableRes | ||
import androidx.core.app.NotificationCompat | ||
import androidx.core.app.NotificationManagerCompat | ||
import com.gun0912.tedpermission.coroutine.TedPermission | ||
|
||
@SuppressLint("MissingPermission") | ||
suspend fun Context.showNotification( | ||
channelId: String, | ||
notificationId: Int = 0, | ||
title: String? = null, | ||
content: String? = null, | ||
@DrawableRes icon: Int = R.drawable.ic_launcher, | ||
group: String? = null, | ||
priority: Int = NotificationCompat.PRIORITY_DEFAULT | ||
): Boolean { | ||
if (!checkPermissionGranted() || !checkNotificationChannelEnabled(channelId)) return false | ||
|
||
val notification = NotificationCompat.Builder(this, channelId) | ||
.setSmallIcon(icon) | ||
.setContentTitle(title) | ||
.setContentText(content) | ||
.setPriority(priority) | ||
.setGroup(group) | ||
.build() | ||
|
||
NotificationManagerCompat.from(this) | ||
.notify(notificationId, notification) | ||
|
||
return true | ||
} | ||
|
||
private suspend fun checkPermissionGranted(): Boolean { | ||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
TedPermission | ||
.create() | ||
.setPermissions(Manifest.permission.POST_NOTIFICATIONS) | ||
.check().isGranted | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
private suspend fun Context.checkNotificationChannelEnabled( | ||
id: String | ||
): Boolean { | ||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
val manager = getSystemService(NotificationManager::class.java) | ||
val channel = manager.getNotificationChannel(id) ?: return false | ||
val isChannelEnabled = channel.importance != NotificationManager.IMPORTANCE_NONE | ||
|
||
if (!isChannelEnabled) { | ||
val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).apply { | ||
putExtra(Settings.EXTRA_APP_PACKAGE, packageName) | ||
putExtra(Settings.EXTRA_CHANNEL_ID, id) | ||
} | ||
startActivity(intent) | ||
} | ||
return isChannelEnabled | ||
} else { | ||
true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters