Skip to content

Commit

Permalink
Make Service Notification Visible on SDK 31+ (#185)
Browse files Browse the repository at this point in the history
* Ask for notification permission

* Update notification style

* Bump `kotlin-ipv8` version
  • Loading branch information
InvictusRMC authored Feb 27, 2024
1 parent a058852 commit 3cda7fa
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@ import android.app.PendingIntent
import android.content.Intent
import androidx.core.app.NotificationCompat
import androidx.core.app.TaskStackBuilder
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import nl.tudelft.ipv8.android.IPv8Android
import nl.tudelft.ipv8.android.service.IPv8Service
import nl.tudelft.trustchain.app.R
import nl.tudelft.trustchain.app.ui.dashboard.DashboardActivity

class TrustChainService : IPv8Service() {
override fun onCreate() {
super.onCreate()
scope.launch {
while (true) {
updateNotification()
delay(1000)
}
}
}

override fun createNotification(): NotificationCompat.Builder {
val trustChainDashboardIntent = Intent(this, DashboardActivity::class.java)
val flags = PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
Expand All @@ -18,9 +31,11 @@ class TrustChainService : IPv8Service() {
.getPendingIntent(0, flags)

return NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_CONNECTION)
.setContentTitle("IPv8")
.setContentText("Running")
.setSmallIcon(R.drawable.ic_insert_link_black_24dp)
.setContentTitle("IPv8 Service")
.setContentText("Connected to ${IPv8Android.getInstance().network.verifiedPeers.size} peers")
.setSmallIcon(R.drawable.ic_device_hub_black_24dp)
.setColor(getColor(R.color.colorPrimary))
.setContentIntent(pendingIntent)
.setOnlyAlertOnce(true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class DashboardActivity : AppCompatActivity() {
// Handle init of IPv8 after requesting permissions; only if Android 12 or higher.
// onPermissionsDenied() is run until user has accepted permissions.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (!hasBluetoothPermissions()) {
requestBluetoothPermissions()
if (!hasBluetoothPermissions() || !hasNotificationPermissions()) {
requestPermissions()
} else {
// Only initialize IPv8 if it has not been initialized yet.
try {
Expand Down Expand Up @@ -71,14 +71,19 @@ class DashboardActivity : AppCompatActivity() {
checkSelfPermission(BLUETOOTH_PERMISSIONS_SCAN) == PackageManager.PERMISSION_GRANTED
}

private fun requestBluetoothPermissions() {
private fun hasNotificationPermissions(): Boolean {
return checkSelfPermission(NOTIFICATION_PERMISSION) == PackageManager.PERMISSION_GRANTED
}

private fun requestPermissions() {
requestPermissions(
arrayOf(
BLUETOOTH_PERMISSIONS_ADVERTISE,
BLUETOOTH_PERMISSIONS_CONNECT,
BLUETOOTH_PERMISSIONS_SCAN
BLUETOOTH_PERMISSIONS_SCAN,
NOTIFICATION_PERMISSION
),
BLUETOOTH_PERMISSIONS_REQUEST_CODE
PERMISSIONS_REQUEST_CODE
)
}

Expand All @@ -87,19 +92,17 @@ class DashboardActivity : AppCompatActivity() {
permissions: Array<out String>,
grantResults: IntArray
) {
when (requestCode) {
BLUETOOTH_PERMISSIONS_REQUEST_CODE -> {
if (hasBluetoothPermissions()) {
(application as TrustChainApplication).initIPv8()
} else {
onPermissionsDenied()
}
if (requestCode == PERMISSIONS_REQUEST_CODE) {
if (!hasBluetoothPermissions() || !hasNotificationPermissions()) {
onPermissionsDenied()
} else {
(application as TrustChainApplication).initIPv8()
}
else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults)
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
}

@Suppress("DEPRECATION")
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
Expand All @@ -113,12 +116,11 @@ class DashboardActivity : AppCompatActivity() {
onPermissionsDenied()
}
}

else -> {
@Suppress("DEPRECATION") // TODO: Fix deprecation issue.
super.onActivityResult(requestCode, resultCode, data)
}
}
@Suppress("DEPRECATION") // TODO: Fix deprecation issue.
super.onActivityResult(requestCode, resultCode, data)
}

Expand All @@ -141,10 +143,11 @@ class DashboardActivity : AppCompatActivity() {
}

companion object {
private const val BLUETOOTH_PERMISSIONS_REQUEST_CODE = 200
private const val PERMISSIONS_REQUEST_CODE = 200
private const val SETTINGS_INTENT_CODE = 1000
private const val BLUETOOTH_PERMISSIONS_SCAN = "android.permission.BLUETOOTH_SCAN"
private const val BLUETOOTH_PERMISSIONS_CONNECT = "android.permission.BLUETOOTH_CONNECT"
private const val BLUETOOTH_PERMISSIONS_ADVERTISE = "android.permission.BLUETOOTH_ADVERTISE"
private const val NOTIFICATION_PERMISSION = "android.permission.POST_NOTIFICATIONS"
}
}
2 changes: 1 addition & 1 deletion kotlin-ipv8

0 comments on commit 3cda7fa

Please sign in to comment.