Make separate channel for finished notifications
Still aren't showing on lock screen, or waking the device sadly. Ive got to finish reading https://github.com/giorgosneokleous93/fullscreenintentexample/tree/main/app/src/main/java/com/giorgosneokleous/fullscreenintentexample
This commit is contained in:
parent
9e3f2fea78
commit
eea6c96e8e
|
@ -13,6 +13,7 @@ class TimerDone : AppCompatActivity() {
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_timer_done)
|
setContentView(R.layout.activity_timer_done)
|
||||||
|
Log.d("TimerDone", "Rendered.")
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.O)
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
|
@ -21,7 +22,7 @@ class TimerDone : AppCompatActivity() {
|
||||||
Log.d("TimerDone", "Stopping...")
|
Log.d("TimerDone", "Stopping...")
|
||||||
applicationContext.stopService(Intent(applicationContext, TimerService::class.java))
|
applicationContext.stopService(Intent(applicationContext, TimerService::class.java))
|
||||||
val manager = NotificationManagerCompat.from(this)
|
val manager = NotificationManagerCompat.from(this)
|
||||||
manager.cancel(TimerService.CHANNEL_ID)
|
manager.cancel(TimerService.ONGOING_ID)
|
||||||
val intent = Intent(applicationContext, MainActivity::class.java)
|
val intent = Intent(applicationContext, MainActivity::class.java)
|
||||||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||||
applicationContext.startActivity(intent)
|
applicationContext.startActivity(intent)
|
||||||
|
|
|
@ -74,7 +74,8 @@ class TimerService : Service() {
|
||||||
secondsLeft = (intent?.getIntExtra("milliseconds", 0) ?: 0) / 1000
|
secondsLeft = (intent?.getIntExtra("milliseconds", 0) ?: 0) / 1000
|
||||||
currentDescription = intent?.getStringExtra("description").toString()
|
currentDescription = intent?.getStringExtra("description").toString()
|
||||||
secondsTotal = secondsLeft
|
secondsTotal = secondsLeft
|
||||||
startForeground(CHANNEL_ID, getProgress(secondsLeft).build())
|
startForeground(ONGOING_ID, getProgress(secondsLeft).build())
|
||||||
|
battery()
|
||||||
Log.d("TimerService", "onStartCommand seconds=$secondsLeft")
|
Log.d("TimerService", "onStartCommand seconds=$secondsLeft")
|
||||||
|
|
||||||
timerRunnable = object : Runnable {
|
timerRunnable = object : Runnable {
|
||||||
|
@ -173,8 +174,8 @@ class TimerService : Service() {
|
||||||
private fun getProgress(timeLeftInSeconds: Int): NotificationCompat.Builder {
|
private fun getProgress(timeLeftInSeconds: Int): NotificationCompat.Builder {
|
||||||
val notificationText = formatTime(timeLeftInSeconds)
|
val notificationText = formatTime(timeLeftInSeconds)
|
||||||
val notificationChannelId = "timer_channel"
|
val notificationChannelId = "timer_channel"
|
||||||
val notificationIntent = Intent(this, TimerService::class.java)
|
val notificationIntent = Intent(this, MainActivity::class.java)
|
||||||
val pendingIntent = PendingIntent.getActivity(
|
val contentPending = PendingIntent.getActivity(
|
||||||
this,
|
this,
|
||||||
0,
|
0,
|
||||||
notificationIntent,
|
notificationIntent,
|
||||||
|
@ -182,7 +183,7 @@ class TimerService : Service() {
|
||||||
)
|
)
|
||||||
val stopBroadcast = Intent(STOP_BROADCAST)
|
val stopBroadcast = Intent(STOP_BROADCAST)
|
||||||
stopBroadcast.setPackage(applicationContext.packageName)
|
stopBroadcast.setPackage(applicationContext.packageName)
|
||||||
val pendingStop =
|
val stopPending =
|
||||||
PendingIntent.getBroadcast(
|
PendingIntent.getBroadcast(
|
||||||
applicationContext,
|
applicationContext,
|
||||||
0,
|
0,
|
||||||
|
@ -191,7 +192,7 @@ class TimerService : Service() {
|
||||||
)
|
)
|
||||||
val addBroadcast =
|
val addBroadcast =
|
||||||
Intent(ADD_BROADCAST).apply { setPackage(applicationContext.packageName) }
|
Intent(ADD_BROADCAST).apply { setPackage(applicationContext.packageName) }
|
||||||
val pendingAdd =
|
val addPending =
|
||||||
PendingIntent.getBroadcast(
|
PendingIntent.getBroadcast(
|
||||||
applicationContext,
|
applicationContext,
|
||||||
0,
|
0,
|
||||||
|
@ -204,15 +205,15 @@ class TimerService : Service() {
|
||||||
.setContentText(notificationText)
|
.setContentText(notificationText)
|
||||||
.setSmallIcon(R.drawable.ic_baseline_timer_24)
|
.setSmallIcon(R.drawable.ic_baseline_timer_24)
|
||||||
.setProgress(secondsTotal, timeLeftInSeconds, false)
|
.setProgress(secondsTotal, timeLeftInSeconds, false)
|
||||||
.setContentIntent(pendingIntent)
|
.setContentIntent(contentPending)
|
||||||
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
|
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
|
||||||
.setAutoCancel(false)
|
.setAutoCancel(false)
|
||||||
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
||||||
.setPriority(NotificationCompat.PRIORITY_LOW)
|
.setPriority(NotificationCompat.PRIORITY_LOW)
|
||||||
.setOngoing(true)
|
.setOngoing(true)
|
||||||
.setDeleteIntent(pendingStop)
|
.setDeleteIntent(stopPending)
|
||||||
.addAction(R.drawable.ic_baseline_stop_24, "Stop", pendingStop)
|
.addAction(R.drawable.ic_baseline_stop_24, "Stop", stopPending)
|
||||||
.addAction(R.drawable.ic_baseline_stop_24, "Add 1 min", pendingAdd)
|
.addAction(R.drawable.ic_baseline_stop_24, "Add 1 min", addPending)
|
||||||
|
|
||||||
val notificationManager = NotificationManagerCompat.from(this)
|
val notificationManager = NotificationManagerCompat.from(this)
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
@ -246,23 +247,49 @@ class TimerService : Service() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun notifyFinished() {
|
private fun notifyFinished() {
|
||||||
val builder = getProgress(0)
|
val channelId = "finished_channel"
|
||||||
|
val notificationManager = NotificationManagerCompat.from(this)
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
val channel =
|
||||||
|
NotificationChannel(channelId, "Timer Finished Channel", NotificationManager.IMPORTANCE_HIGH)
|
||||||
|
channel.setSound(null, null)
|
||||||
|
channel.setBypassDnd(true)
|
||||||
|
channel.enableVibration(false)
|
||||||
|
channel.description = "Plays an alarm when a rest timer completes."
|
||||||
|
channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
|
||||||
|
notificationManager.createNotificationChannel(channel)
|
||||||
|
}
|
||||||
|
|
||||||
val fullIntent = Intent(applicationContext, TimerDone::class.java)
|
val fullIntent = Intent(applicationContext, TimerDone::class.java)
|
||||||
val fullPending = PendingIntent.getActivity(
|
val fullPending = PendingIntent.getActivity(
|
||||||
applicationContext, 0, fullIntent, PendingIntent.FLAG_IMMUTABLE
|
applicationContext, 0, fullIntent, PendingIntent.FLAG_MUTABLE
|
||||||
)
|
)
|
||||||
val finishIntent = Intent(applicationContext, StopAlarm::class.java)
|
val finishIntent = Intent(applicationContext, StopAlarm::class.java)
|
||||||
val finishPending = PendingIntent.getActivity(
|
val finishPending = PendingIntent.getActivity(
|
||||||
applicationContext, 0, finishIntent, PendingIntent.FLAG_IMMUTABLE
|
applicationContext, 0, finishIntent, PendingIntent.FLAG_IMMUTABLE
|
||||||
)
|
)
|
||||||
builder.setPriority(NotificationCompat.PRIORITY_HIGH)
|
val stopBroadcast = Intent(STOP_BROADCAST)
|
||||||
|
stopBroadcast.setPackage(applicationContext.packageName)
|
||||||
|
val pendingStop =
|
||||||
|
PendingIntent.getBroadcast(
|
||||||
|
applicationContext,
|
||||||
|
0,
|
||||||
|
stopBroadcast,
|
||||||
|
PendingIntent.FLAG_IMMUTABLE
|
||||||
|
)
|
||||||
|
|
||||||
|
val builder = NotificationCompat.Builder(this, channelId)
|
||||||
|
.setContentTitle("Timer finished")
|
||||||
|
.setContentText(currentDescription)
|
||||||
|
.setSmallIcon(R.drawable.ic_baseline_timer_24)
|
||||||
|
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
||||||
.setCategory(NotificationCompat.CATEGORY_ALARM)
|
.setCategory(NotificationCompat.CATEGORY_ALARM)
|
||||||
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
||||||
.setContentIntent(finishPending)
|
.setContentIntent(finishPending)
|
||||||
.setFullScreenIntent(fullPending, true)
|
.setFullScreenIntent(fullPending, true)
|
||||||
.setOngoing(false)
|
.setAutoCancel(true)
|
||||||
.setProgress(0, 0, false)
|
.setDeleteIntent(pendingStop)
|
||||||
val notificationManager = NotificationManagerCompat.from(this)
|
|
||||||
if (ActivityCompat.checkSelfPermission(
|
if (ActivityCompat.checkSelfPermission(
|
||||||
this,
|
this,
|
||||||
Manifest.permission.POST_NOTIFICATIONS
|
Manifest.permission.POST_NOTIFICATIONS
|
||||||
|
@ -270,7 +297,7 @@ class TimerService : Service() {
|
||||||
) {
|
) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
notificationManager.notify(CHANNEL_ID, builder.build())
|
notificationManager.notify(FINISHED_ID, builder.build())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateNotification(seconds: Int) {
|
private fun updateNotification(seconds: Int) {
|
||||||
|
@ -283,7 +310,7 @@ class TimerService : Service() {
|
||||||
) {
|
) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
notificationManager.notify(CHANNEL_ID, notification.build())
|
notificationManager.notify(ONGOING_ID, notification.build())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun formatTime(timeInSeconds: Int): String {
|
private fun formatTime(timeInSeconds: Int): String {
|
||||||
|
@ -295,6 +322,7 @@ class TimerService : Service() {
|
||||||
companion object {
|
companion object {
|
||||||
const val STOP_BROADCAST = "stop-timer-event"
|
const val STOP_BROADCAST = "stop-timer-event"
|
||||||
const val ADD_BROADCAST = "add-timer-event"
|
const val ADD_BROADCAST = "add-timer-event"
|
||||||
const val CHANNEL_ID = 1
|
const val ONGOING_ID = 1
|
||||||
|
const val FINISHED_ID = 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user