Skip to content

Commit

Permalink
fix: background service error
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Oct 31, 2023
1 parent 40f294f commit 4518758
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/bnyro/clock/receivers/AlarmReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.core.content.ContextCompat
import com.bnyro.clock.db.DatabaseHolder
import com.bnyro.clock.services.AlarmService
import com.bnyro.clock.util.AlarmHelper
Expand All @@ -23,7 +24,7 @@ class AlarmReceiver : BroadcastReceiver() {
if (currentDay - 1 in alarm.days) {
val playAlarm = Intent(context, AlarmService::class.java)
playAlarm.putExtra(AlarmHelper.EXTRA_ID, id)
context.startService(playAlarm)
ContextCompat.startForegroundService(context, playAlarm)
}

// re-enqueue the alarm for the next day
Expand Down
19 changes: 4 additions & 15 deletions app/src/main/java/com/bnyro/clock/services/AlarmService.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.bnyro.clock.services

import android.Manifest
import android.app.Notification
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.media.MediaPlayer
import android.media.RingtoneManager
import android.net.Uri
Expand All @@ -14,9 +13,7 @@ import android.os.IBinder
import android.os.Looper
import android.os.Vibrator
import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.net.toUri
import com.bnyro.clock.R
import com.bnyro.clock.db.DatabaseHolder
Expand Down Expand Up @@ -55,7 +52,7 @@ class AlarmService : Service() {
val alarm = runBlocking {
DatabaseHolder.instance.alarmsDao().findById(id)
}
showNotification(this, alarm)
startForeground(1, createNotification(this, alarm))
play(alarm)
currentAlarm = alarm
return START_STICKY
Expand Down Expand Up @@ -116,7 +113,7 @@ class AlarmService : Service() {
vibrator?.cancel()
}

private fun showNotification(context: Context, alarm: Alarm) {
private fun createNotification(context: Context, alarm: Alarm): Notification {
NotificationHelper.createAlarmNotificationChannel(context, alarm)

val pendingIntent = PendingIntent.getActivity(
Expand All @@ -139,14 +136,6 @@ class AlarmService : Service() {
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setFullScreenIntent(pendingIntent, true)
val notification = builder.build()

if (ActivityCompat.checkSelfPermission(
context,
Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED
) {
NotificationManagerCompat.from(context).notify(alarm.id.toInt(), notification)
}
return builder.build()
}
}

0 comments on commit 4518758

Please sign in to comment.