-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from Joe3112/feature/notifications
Add local notification permission
- Loading branch information
Showing
8 changed files
with
137 additions
and
37 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
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
52 changes: 52 additions & 0 deletions
52
...Main/kotlin/com.mohamedrejeb.calf/permissions/helper/LocalNotificationPermissionHelper.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,52 @@ | ||
package com.mohamedrejeb.calf.permissions.helper | ||
|
||
import com.mohamedrejeb.calf.permissions.ExperimentalPermissionsApi | ||
import com.mohamedrejeb.calf.permissions.PermissionStatus | ||
import platform.UserNotifications.UNAuthorizationOptionAlert | ||
import platform.UserNotifications.UNAuthorizationOptionBadge | ||
import platform.UserNotifications.UNAuthorizationOptionSound | ||
import platform.UserNotifications.UNAuthorizationStatusAuthorized | ||
import platform.UserNotifications.UNAuthorizationStatusEphemeral | ||
import platform.UserNotifications.UNAuthorizationStatusNotDetermined | ||
import platform.UserNotifications.UNAuthorizationStatusProvisional | ||
import platform.UserNotifications.UNUserNotificationCenter | ||
|
||
internal class LocalNotificationPermissionHelper : PermissionHelper { | ||
|
||
override fun launchPermissionRequest(onPermissionResult: (Boolean) -> Unit) { | ||
val notificationCenter = UNUserNotificationCenter.currentNotificationCenter() | ||
|
||
notificationCenter.getNotificationSettingsWithCompletionHandler { settings -> | ||
when (settings?.authorizationStatus) { | ||
UNAuthorizationStatusAuthorized, | ||
UNAuthorizationStatusProvisional, | ||
UNAuthorizationStatusEphemeral -> onPermissionResult(true) | ||
UNAuthorizationStatusNotDetermined -> { | ||
notificationCenter.requestAuthorizationWithOptions( | ||
UNAuthorizationOptionSound.or(UNAuthorizationOptionAlert).or(UNAuthorizationOptionBadge) | ||
) { isOk, error -> | ||
if (isOk && error == null) { | ||
onPermissionResult(true) | ||
} else { | ||
onPermissionResult(false) | ||
} | ||
} | ||
} | ||
else -> onPermissionResult(false) | ||
} | ||
} | ||
} | ||
|
||
@ExperimentalPermissionsApi | ||
override fun getPermissionStatus(onPermissionResult: (PermissionStatus) -> Unit) { | ||
val notificationCenter = UNUserNotificationCenter.currentNotificationCenter() | ||
notificationCenter.getNotificationSettingsWithCompletionHandler { settings -> | ||
when (settings?.authorizationStatus) { | ||
UNAuthorizationStatusAuthorized, | ||
UNAuthorizationStatusProvisional, | ||
UNAuthorizationStatusEphemeral -> onPermissionResult(PermissionStatus.Granted) | ||
else -> onPermissionResult(PermissionStatus.Denied(false)) | ||
} | ||
} | ||
} | ||
} |
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
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