Skip to content

Commit

Permalink
Request Notification permission in Android API 33+ on startup. (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
tirkarthi committed Oct 1, 2023
1 parent 28386bc commit a7bf86e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ android {
storePassword keystoreProperties['storePassword']
}
}
compileSdk 31
compileSdk 33

defaultConfig {
applicationId "com.xtreak.notificationdictionary"
minSdk 24
targetSdk 33
versionCode 19
versionName "0.0.19"
versionCode 20
versionName "0.0.20"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="com.xtreak.notificationdictionary">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package com.xtreak.notificationdictionary

import android.Manifest
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.ProgressDialog
Expand All @@ -24,8 +25,10 @@ import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.*
import android.widget.TextView.OnEditorActionListener
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.view.MenuItemCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -46,6 +49,8 @@ class MainActivity : AppCompatActivity() {

private lateinit var progress_dialog: ProgressDialog
private val CHANNEL_ID = "Dictionary"
private val NOTIFICATION_REQUEST_CODE = 11


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -137,7 +142,11 @@ class MainActivity : AppCompatActivity() {
done in my free time apart from my day job along with download costs for database files
from CDN. If you find the app useful please leave a review in Play store and share the
app with your friends. It will help and encourage me in maintaining the app and adding more features.
Thanks."""
Please grant notification permission since the app requires notification permission in
Android 13+ to show meanings through notification.
Thanks for your support.
"""
)
), this
)
Expand All @@ -146,6 +155,10 @@ class MainActivity : AppCompatActivity() {
initialize_spinner(database_name)
// show_changelog()
onNewIntent(intent)

// Request notification permission in Android 33+
// https://developer.android.com/develop/ui/views/notifications/notification-permission
requestNotificationPermission()
}

private fun setIMEAction() {
Expand Down Expand Up @@ -485,6 +498,19 @@ class MainActivity : AppCompatActivity() {
}
}

private fun requestNotificationPermission() {
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

if (Build.VERSION.SDK_INT >= 33 && !notificationManager.areNotificationsEnabled()) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.POST_NOTIFICATIONS),
NOTIFICATION_REQUEST_CODE
)
}
}

fun sendMessage(view: View) {
val wordEdit = findViewById<EditText>(R.id.wordInput)

Expand Down

0 comments on commit a7bf86e

Please sign in to comment.