Skip to content

Commit

Permalink
fix(38): POST_NOTIFICATIONS for foreground services
Browse files Browse the repository at this point in the history
  • Loading branch information
mcatta committed Oct 18, 2023
1 parent e09d90b commit c9c32db
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
~ limitations under the License.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<manifest xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<application
android:name="dev.marcocattaneo.sleep.di.SleepifyApplication"
Expand Down
27 changes: 18 additions & 9 deletions app/src/main/java/dev/marcocattaneo/sleep/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@

package dev.marcocattaneo.sleep

import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import dagger.hilt.android.AndroidEntryPoint
import dev.marcocattaneo.sleep.ui.SleepApp
import dev.marcocattaneo.sleep.ui.notification.PlayerNotificationService
import dev.marcocattaneo.sleep.ui.notification.playerServiceIntent

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
Expand All @@ -32,17 +31,27 @@ class MainActivity : ComponentActivity() {
super.onCreate(savedInstanceState)

// Start NotificationService
Intent(this, PlayerNotificationService::class.java)
.let {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(it)
} else {
startService(it)
}
with(playerServiceIntent()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(this)
} else {
startService(this)
}
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
requestPermissions(arrayOf(android.Manifest.permission.POST_NOTIFICATIONS), 0)
}

setContent {
SleepApp()
}
}

override fun onDestroy() {
super.onDestroy()

stopService(playerServiceIntent())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class PlayerNotificationManager @Inject constructor(

companion object {
const val CHANNEL_ID = "Player_Notification_Channel_ID"

const val NOTIFICATION_ID = 93
}

Expand All @@ -60,7 +59,7 @@ class PlayerNotificationManager @Inject constructor(
context,
0,
Intent(context, PlayerNotificationService::class.java).setAction(action.key),
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0
PendingIntent.FLAG_IMMUTABLE
)

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package dev.marcocattaneo.sleep.ui.notification

import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.IBinder
import dagger.hilt.android.AndroidEntryPoint
Expand Down Expand Up @@ -97,4 +98,10 @@ class PlayerNotificationService : Service() {
job.cancel()
}

}
}

/**
* Helper method to start the [PlayerNotificationService]
* @receiver Context
*/
fun Context.playerServiceIntent() = Intent(this, PlayerNotificationService::class.java)

0 comments on commit c9c32db

Please sign in to comment.