Skip to content

Commit 57aea03

Browse files
committed
android foregroudn notification
1 parent c554019 commit 57aea03

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

android-app/app/src/main/java/com/nervosnetwork/ckblightclient/LightClientService.kt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class LightClientService : Service() {
6767

6868
private fun createNotificationChannel() {
6969
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
70+
Log.d(TAG, "Creating notification channel for Android O+")
7071
val channel = NotificationChannel(
7172
CHANNEL_ID,
7273
"CKB Light Client Service",
@@ -76,16 +77,29 @@ class LightClientService : Service() {
7677
}
7778
val notificationManager = getSystemService(NotificationManager::class.java)
7879
notificationManager.createNotificationChannel(channel)
80+
Log.d(TAG, "Notification channel created successfully")
81+
} else {
82+
Log.d(TAG, "Android version < O, no notification channel needed")
7983
}
8084
}
8185

8286
private fun startForegroundService() {
8387
val notification = createNotification("CKB Light Client is starting...")
8488

85-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
86-
startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)
87-
} else {
88-
startForeground(NOTIFICATION_ID, notification)
89+
Log.d(TAG, "Starting foreground service with notification")
90+
appendLog("Starting foreground service...")
91+
92+
try {
93+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
94+
startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)
95+
} else {
96+
startForeground(NOTIFICATION_ID, notification)
97+
}
98+
Log.d(TAG, "Foreground service started successfully")
99+
appendLog("Foreground service started - notification should be visible")
100+
} catch (e: Exception) {
101+
Log.e(TAG, "Failed to start foreground service", e)
102+
appendLog("ERROR starting foreground service: ${e.message}")
89103
}
90104
}
91105

android-app/app/src/main/java/com/nervosnetwork/ckblightclient/MainActivity.kt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package com.nervosnetwork.ckblightclient
22

3+
import android.Manifest
4+
import android.content.pm.PackageManager
5+
import android.os.Build
36
import android.os.Bundle
47
import android.util.Log
58
import androidx.appcompat.app.AppCompatActivity
9+
import androidx.core.app.ActivityCompat
10+
import androidx.core.content.ContextCompat
611
import androidx.fragment.app.Fragment
712
import com.google.android.material.bottomnavigation.BottomNavigationView
813
import com.nervosnetwork.ckblightclient.fragments.LogsFragment
@@ -15,6 +20,7 @@ class MainActivity : AppCompatActivity() {
1520

1621
companion object {
1722
const val CONFIG_NAME = "mainnet.toml"
23+
private const val NOTIFICATION_PERMISSION_REQUEST_CODE = 1001
1824
}
1925

2026
override fun onCreate(savedInstanceState: Bundle?) {
@@ -23,6 +29,9 @@ class MainActivity : AppCompatActivity() {
2329

2430
bottomNavigation = findViewById(R.id.bottom_navigation)
2531

32+
// Request notification permission for Android 13+
33+
requestNotificationPermission()
34+
2635
// Setup binary and config
2736
setupBinaryAndConfig()
2837

@@ -47,6 +56,39 @@ class MainActivity : AppCompatActivity() {
4756
}
4857
}
4958

59+
private fun requestNotificationPermission() {
60+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
61+
if (ContextCompat.checkSelfPermission(
62+
this,
63+
Manifest.permission.POST_NOTIFICATIONS
64+
) != PackageManager.PERMISSION_GRANTED
65+
) {
66+
ActivityCompat.requestPermissions(
67+
this,
68+
arrayOf(Manifest.permission.POST_NOTIFICATIONS),
69+
NOTIFICATION_PERMISSION_REQUEST_CODE
70+
)
71+
}
72+
}
73+
}
74+
75+
override fun onRequestPermissionsResult(
76+
requestCode: Int,
77+
permissions: Array<out String>,
78+
grantResults: IntArray
79+
) {
80+
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
81+
when (requestCode) {
82+
NOTIFICATION_PERMISSION_REQUEST_CODE -> {
83+
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
84+
Log.d(TAG, "Notification permission granted")
85+
} else {
86+
Log.w(TAG, "Notification permission denied - foreground service notification will not show")
87+
}
88+
}
89+
}
90+
}
91+
5092
private fun loadFragment(fragment: Fragment) {
5193
supportFragmentManager.beginTransaction()
5294
.replace(R.id.fragment_container, fragment)

0 commit comments

Comments
 (0)