-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Progress within notification
- Loading branch information
Showing
6 changed files
with
148 additions
and
0 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
46 changes: 46 additions & 0 deletions
46
app/src/main/java/com/asemlab/samples/NotificationProgressActivity.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,46 @@ | ||
package com.asemlab.samples | ||
|
||
import android.os.Bundle | ||
import androidx.activity.enableEdgeToEdge | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.core.view.ViewCompat | ||
import androidx.core.view.WindowInsetsCompat | ||
import androidx.lifecycle.coroutineScope | ||
import com.asemlab.samples.utils.NotificationsUtils | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
|
||
class NotificationProgressActivity : AppCompatActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
enableEdgeToEdge() | ||
setContentView(R.layout.activity_notification_progress) | ||
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> | ||
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) | ||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) | ||
insets | ||
} | ||
|
||
with(NotificationsUtils) { | ||
createNotificationsChannel(this@NotificationProgressActivity) | ||
var builder = createNotificationBuilder(this@NotificationProgressActivity) | ||
|
||
lifecycle.coroutineScope.launch { | ||
var progress = 0 | ||
while (progress < 10) { | ||
progress += 1 | ||
delay(1000) | ||
|
||
// TODO How to display progress within notification | ||
builder.setProgress(10, progress, false) | ||
sendNotification(1024, builder.build()) | ||
} | ||
|
||
builder = createNotificationBuilderDone(this@NotificationProgressActivity) | ||
sendNotification(1024, builder.build()) | ||
} | ||
|
||
} | ||
|
||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
app/src/main/java/com/asemlab/samples/utils/NotificationsUtils.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,56 @@ | ||
package com.asemlab.samples.utils | ||
|
||
import android.app.Notification | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.content.Context | ||
import android.os.Build | ||
import androidx.core.app.NotificationCompat | ||
import com.asemlab.samples.R | ||
|
||
object NotificationsUtils { | ||
|
||
private lateinit var notificationManager: NotificationManager | ||
|
||
fun createNotificationsChannel(context: Context) { | ||
notificationManager = | ||
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
val channel = NotificationChannel( | ||
"ProgressChannel", | ||
"Progress Channel", | ||
NotificationManager.IMPORTANCE_LOW | ||
) | ||
notificationManager.createNotificationChannel(channel) | ||
} | ||
} | ||
|
||
fun createNotificationBuilder(context: Context): NotificationCompat.Builder { | ||
return NotificationCompat.Builder(context, "ProgressChannel") | ||
.setSmallIcon(R.drawable.ic_download) | ||
.setContentTitle("File Download") | ||
.setContentText("Downloading...") | ||
.setPriority(NotificationCompat.PRIORITY_LOW) | ||
.setOngoing(true) | ||
} | ||
|
||
fun createNotificationBuilderDone(context: Context): NotificationCompat.Builder { | ||
return NotificationCompat.Builder(context, "ProgressChannel") | ||
.setSmallIcon(R.drawable.ic_download_done) | ||
.setContentTitle("Download Complete") | ||
.setContentText("Test.jpg downloaded successfully") | ||
.setPriority(NotificationCompat.PRIORITY_LOW) | ||
.setProgress(0, 0, false) | ||
} | ||
|
||
fun cancelNotification(notificationId: Int) { | ||
notificationManager.cancel(notificationId) | ||
} | ||
|
||
fun sendNotification(notificationId: Int, notification: Notification) { | ||
notificationManager.notify(notificationId, notification) | ||
} | ||
|
||
|
||
} |
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,13 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:autoMirrored="true" | ||
android:tint="#000000" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24"> | ||
|
||
<path | ||
android:fillColor="@android:color/white" | ||
android:pathData="M16.59,9H15V4c0,-0.55 -0.45,-1 -1,-1h-4c-0.55,0 -1,0.45 -1,1v5H7.41c-0.89,0 -1.34,1.08 -0.71,1.71l4.59,4.59c0.39,0.39 1.02,0.39 1.41,0l4.59,-4.59c0.63,-0.63 0.19,-1.71 -0.7,-1.71zM5,19c0,0.55 0.45,1 1,1h12c0.55,0 1,-0.45 1,-1s-0.45,-1 -1,-1H6c-0.55,0 -1,0.45 -1,1z" /> | ||
|
||
</vector> |
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,13 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:autoMirrored="true" | ||
android:tint="#000000" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24"> | ||
|
||
<path | ||
android:fillColor="@android:color/white" | ||
android:pathData="M6,18h12c0.55,0 1,0.45 1,1s-0.45,1 -1,1L6,20c-0.55,0 -1,-0.45 -1,-1s0.45,-1 1,-1zM11.01,13.9c-0.78,0.77 -2.04,0.77 -2.82,-0.01L6,11.7c-0.55,-0.55 -0.54,-1.44 0.03,-1.97 0.54,-0.52 1.4,-0.5 1.92,0.02L9.6,11.4l6.43,-6.43c0.54,-0.54 1.41,-0.54 1.95,0l0.04,0.04c0.54,0.54 0.54,1.42 -0.01,1.96l-7,6.93z" /> | ||
|
||
</vector> |
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,9 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:id="@+id/main" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".NotificationProgressActivity"> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |