Skip to content

Commit fe90043

Browse files
committed
Fix background service: specialUse type, request notification permission on launch not at timer start
1 parent 832df09 commit fe90043

4 files changed

Lines changed: 14 additions & 9 deletions

File tree

.DS_Store

0 Bytes
Binary file not shown.

work_timer/android/app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<uses-permission android:name="android.permission.VIBRATE"/>
33
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
44
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
5-
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK"/>
5+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"/>
66
<application
77
android:label="Sift"
88
android:name="${applicationName}"
@@ -32,7 +32,11 @@
3232
<service
3333
android:name=".TimerService"
3434
android:exported="false"
35-
android:foregroundServiceType="mediaPlayback"/>
35+
android:foregroundServiceType="specialUse">
36+
<property
37+
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
38+
android:value="This foreground service keeps the work session timer running so the alarm can ring at each phase boundary even when the app is in the background."/>
39+
</service>
3640
<!-- Don't delete the meta-data below.
3741
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
3842
<meta-data

work_timer/android/app/src/main/kotlin/com/utsapoddar/sift/MainActivity.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ class MainActivity : FlutterActivity() {
3838
ch.setMethodCallHandler { call, result ->
3939
when (call.method) {
4040
"startTimerService" -> {
41-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
42-
ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
43-
!= PackageManager.PERMISSION_GRANTED) {
44-
ActivityCompat.requestPermissions(
45-
this, arrayOf(Manifest.permission.POST_NOTIFICATIONS), 1001)
46-
}
4741
val intent = Intent(this, TimerService::class.java)
4842
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
4943
startForegroundService(intent)
@@ -63,6 +57,13 @@ class MainActivity : FlutterActivity() {
6357

6458
override fun onCreate(savedInstanceState: Bundle?) {
6559
super.onCreate(savedInstanceState)
60+
// Request notification permission on launch so it's settled before the timer starts
61+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
62+
ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
63+
!= PackageManager.PERMISSION_GRANTED) {
64+
ActivityCompat.requestPermissions(
65+
this, arrayOf(Manifest.permission.POST_NOTIFICATIONS), 1001)
66+
}
6667
val filter = IntentFilter().apply {
6768
addAction(TimerService.ACTION_STOP)
6869
addAction(TimerService.ACTION_SILENCE)

work_timer/android/app/src/main/kotlin/com/utsapoddar/sift/TimerService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class TimerService : Service() {
2727
super.onCreate()
2828
createChannel()
2929
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
30-
startForeground(NOTIF_ID, buildNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
30+
startForeground(NOTIF_ID, buildNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE)
3131
} else {
3232
startForeground(NOTIF_ID, buildNotification())
3333
}

0 commit comments

Comments
 (0)