Skip to content

Commit

Permalink
Implement Progress within notification
Browse files Browse the repository at this point in the history
  • Loading branch information
AsemLab committed Sep 25, 2024
1 parent 41ae740 commit a6eefd0
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />

Expand Down Expand Up @@ -61,6 +62,16 @@
</activity>
<activity android:name=".RegisterForResultActivity" />

<activity
android:name=".NotificationProgressActivity"
android:exported="true">
<!-- <intent-filter>-->
<!-- <action android:name="android.intent.action.MAIN" />-->

<!-- <category android:name="android.intent.category.LAUNCHER" />-->
<!-- </intent-filter>-->
</activity>

<receiver
android:name=".download.DownloadCompleteReceiver"
android:enabled="true"
Expand Down
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 app/src/main/java/com/asemlab/samples/utils/NotificationsUtils.kt
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)
}


}
13 changes: 13 additions & 0 deletions app/src/main/res/drawable/ic_download.xml
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>
13 changes: 13 additions & 0 deletions app/src/main/res/drawable/ic_download_done.xml
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>
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_notification_progress.xml
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>

0 comments on commit a6eefd0

Please sign in to comment.