Skip to content

Commit

Permalink
Android: VNCConnService: refactor Notification creation into function
Browse files Browse the repository at this point in the history
re #230
  • Loading branch information
bk138 committed Oct 17, 2023
1 parent 0bff892 commit 93a4fee
Showing 1 changed file with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,26 @@ class VNCConnService : Service() {
}
}
Log.d(TAG, "onStartCommand: notifying with " + getString(R.string.connected_to, hosts))
val notificationIntent = Intent(this, VncCanvasActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0)
val notificationBuilder = NotificationCompat.Builder(this, packageName)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.connected_to, hosts))
.setContentIntent(pendingIntent)
.setOnlyAlertOnce(true)
if (Build.VERSION.SDK_INT >= 31) {
notificationBuilder.foregroundServiceBehavior = Notification.FOREGROUND_SERVICE_IMMEDIATE
}
val notification = notificationBuilder.build()

startForeground(NOTIFICATION_ID, notification)
startForeground(NOTIFICATION_ID, getNotification(getString(R.string.connected_to, hosts)))
}
// stay until explicitly stopped
return START_STICKY
}

private fun getNotification(text: String): Notification {
val notificationIntent = Intent(this, VncCanvasActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0)
val notificationBuilder = NotificationCompat.Builder(this, packageName)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(text)
.setContentIntent(pendingIntent)
.setOnlyAlertOnce(true)
if (Build.VERSION.SDK_INT >= 31) {
notificationBuilder.foregroundServiceBehavior = Notification.FOREGROUND_SERVICE_IMMEDIATE
}
return notificationBuilder.build()
}

}

0 comments on commit 93a4fee

Please sign in to comment.