-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for android push notifications
- Loading branch information
Showing
30 changed files
with
599 additions
and
174 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
24 changes: 0 additions & 24 deletions
24
androidApp/src/androidMain/kotlin/me/sujanpoudel/playdeals/PushNotificationTopic.kt
This file was deleted.
Oops, something went wrong.
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
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
57 changes: 57 additions & 0 deletions
57
shared/src/androidMain/kotlin/me/sujanpoudel/playdeals/common/AndroidPermissionManager.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,57 @@ | ||
package me.sujanpoudel.playdeals.common | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.net.Uri | ||
import android.provider.Settings | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.State | ||
import androidx.compose.runtime.derivedStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.platform.LocalContext | ||
import com.google.accompanist.permissions.ExperimentalPermissionsApi | ||
import com.google.accompanist.permissions.PermissionState | ||
import com.google.accompanist.permissions.rememberPermissionState | ||
import com.google.accompanist.permissions.shouldShowRationale | ||
|
||
@OptIn(ExperimentalPermissionsApi::class) | ||
class AndroidPermissionManager | ||
private constructor( | ||
private val context: Context, | ||
private val state: PermissionState, | ||
) : PermissionManager { | ||
|
||
override val permissionState: State<PermissionStatus> = derivedStateOf { | ||
when (state.status) { | ||
is com.google.accompanist.permissions.PermissionStatus.Denied -> if (state.status.shouldShowRationale) { | ||
PermissionStatus.Denied | ||
} else { | ||
PermissionStatus.NotAsked | ||
} | ||
|
||
com.google.accompanist.permissions.PermissionStatus.Granted -> PermissionStatus.Granted | ||
} | ||
} | ||
|
||
override fun askForPermission() = state.launchPermissionRequest() | ||
|
||
override fun showRationale() = context.openAppSettingScreen() | ||
|
||
companion object { | ||
@Composable | ||
fun rememberPermissionManager(permission: String): PermissionManager { | ||
val state = rememberPermissionState(permission = permission) | ||
val context = LocalContext.current | ||
return remember { | ||
AndroidPermissionManager(context, state) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun Context.openAppSettingScreen() { | ||
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) | ||
intent.data = Uri.parse("package:$packageName") | ||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | ||
startActivity(intent) | ||
} |
25 changes: 24 additions & 1 deletion
25
shared/src/androidMain/kotlin/me/sujanpoudel/playdeals/common/App.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,8 +1,31 @@ | ||
package me.sujanpoudel.playdeals.common | ||
|
||
import android.Manifest | ||
import android.os.Build | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.platform.LocalContext | ||
import me.sujanpoudel.playdeals.common.pushNotification.AndroidNotificationManager | ||
import me.sujanpoudel.playdeals.common.pushNotification.LocalNotificationManager | ||
import me.sujanpoudel.playdeals.common.pushNotification.LocalPushNotificationPermissionManager | ||
|
||
@Composable | ||
fun PlayDealsAppAndroid() { | ||
PlayDealsApp() | ||
val permissionManager = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
AndroidPermissionManager.rememberPermissionManager(Manifest.permission.POST_NOTIFICATIONS) | ||
} else { | ||
PermissionManager.ALWAYS_GRANTED_PERMISSION | ||
} | ||
|
||
val context = LocalContext.current | ||
|
||
val notificationManager = remember { AndroidNotificationManager(context) } | ||
|
||
CompositionLocalProvider( | ||
LocalPushNotificationPermissionManager provides permissionManager, | ||
LocalNotificationManager provides notificationManager, | ||
) { | ||
PlayDealsApp() | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...ain/kotlin/me/sujanpoudel/playdeals/common/pushNotification/AndroidNotificationManager.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,32 @@ | ||
package me.sujanpoudel.playdeals.common.pushNotification | ||
|
||
import android.app.NotificationChannel | ||
import android.content.Context | ||
import com.google.firebase.messaging.FirebaseMessaging | ||
import com.google.firebase.messaging.FirebaseMessagingService | ||
|
||
class AndroidNotificationManager(context: Context) : NotificationManager { | ||
|
||
init { | ||
context.registerNotificationChannels() | ||
} | ||
|
||
override fun subscribeToTopic(topic: String) { | ||
FirebaseMessaging.getInstance().subscribeToTopic(topic) | ||
} | ||
|
||
override fun unSubscribeFromTopic(topic: String) { | ||
FirebaseMessaging.getInstance().unsubscribeFromTopic(topic) | ||
} | ||
} | ||
|
||
fun Context.registerNotificationChannels() { | ||
val notificationManager = | ||
getSystemService(FirebaseMessagingService.NOTIFICATION_SERVICE) as android.app.NotificationManager | ||
|
||
PushNotificationTopic.entries | ||
.forEach { | ||
val channel = NotificationChannel(it.identifier, it.label, android.app.NotificationManager.IMPORTANCE_DEFAULT) | ||
notificationManager.createNotificationChannel(channel) | ||
} | ||
} |
Oops, something went wrong.