Add notification lifecycle test helper

This commit is contained in:
ajp_anton
2026-07-23 10:27:20 +00:00
parent 8570f65d09
commit 2709c2e92d
4 changed files with 23 additions and 0 deletions
+1
View File
@@ -16,3 +16,4 @@ dependencyResolutionManagement {
rootProject.name = "NotificationLog" rootProject.name = "NotificationLog"
include(":app") include(":app")
include(":test-helper")
+7
View File
@@ -0,0 +1,7 @@
plugins { alias(libs.plugins.android.application); alias(libs.plugins.kotlin.android) }
android { namespace = "se.ajpanton.notificationlog.helper"; compileSdk = 36
defaultConfig { applicationId = "se.ajpanton.notificationlog.helper"; minSdk = 34; targetSdk = 36; versionCode = 1; versionName = "0.1" }
compileOptions { sourceCompatibility = JavaVersion.VERSION_17; targetCompatibility = JavaVersion.VERSION_17 }
kotlin { compilerOptions { jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17) } }
}
dependencies { implementation(libs.appcompat); implementation(libs.material) }
+1
View File
@@ -0,0 +1 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"><uses-permission android:name="android.permission.POST_NOTIFICATIONS"/><application android:theme="@style/Theme.AppCompat.DayNight"><activity android:name=".MainActivity" android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></intent-filter></activity></application></manifest>
@@ -0,0 +1,14 @@
package se.ajpanton.notificationlog.helper
import android.app.*
import android.graphics.Bitmap
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private val manager by lazy { getSystemService(NotificationManager::class.java) }
override fun onCreate(state: Bundle?) { super.onCreate(state); manager.createNotificationChannel(NotificationChannel("test", "Test", NotificationManager.IMPORTANCE_DEFAULT)); setContentView(LinearLayout(this).apply { orientation=LinearLayout.VERTICAL; listOf("Post text" to { post("Text message") }, "Edit text" to { post("Edited message") }, "Post image" to { image() }, "Cancel" to { manager.cancel(1) }).forEach { (label, action) -> addView(Button(this@MainActivity).apply { text=label; setOnClickListener { action() } }) } }) }
private fun post(text:String)=manager.notify(1, Notification.Builder(this,"test").setSmallIcon(android.R.drawable.ic_dialog_info).setContentTitle("Helper").setContentText(text).build())
private fun image(){ val b=Bitmap.createBitmap(20,20,Bitmap.Config.ARGB_8888).apply{eraseColor(0xffff0000.toInt())}; manager.notify(2,Notification.Builder(this,"test").setSmallIcon(android.R.drawable.ic_menu_gallery).setStyle(Notification.BigPictureStyle().bigPicture(b)).build()) }
}