Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private fun createNotificationChannel() {
- val name = "Re"
- val descriptionText = "Yours Good"
- val importance = NotificationManager.IMPORTANCE_DEFAULT
- val channel = NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance).apply {
- description = descriptionText
- }
- val notificationManager: NotificationManager =
- getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
- notificationManager.createNotificationChannel(channel)
- }
- private fun checkPermissionsAndShowNotification(score: Int) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
- if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
- ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.POST_NOTIFICATIONS), POST_NOTIFICATIONS_REQUEST_CODE)
- } else {
- showNotification(score)
- }
- } else {
- showNotification(score)
- }
- }
- @SuppressLint("MissingPermission")
- private fun showNotification(score: Int) {
- createNotificationChannel() // Ensure the channel is created before showing a notification
- val builder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
- .setSmallIcon(R.drawable.images) // Ensure you have a drawable named `images` in your drawable resources
- .setContentTitle(" Result")
- .setContentText("You $s") // Incorporate the score into the notification text
- .setPriority(NotificationCompat.PRIORITY_DEFAULT)
- with(NotificationManagerCompat.from(this)) {
- notify(1, builder.build()) // The first parameter is a unique ID for the notification
- }
- }
- override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults)
- if (requestCode == POST_NOTIFICATIONS_REQUEST_CODE && grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- showNotification(tempScore) // Use tempScore here
- } else {
- Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment