Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please supoort android 13 #88

Open
wll19890224 opened this issue Dec 22, 2022 · 1 comment
Open

Please supoort android 13 #88

wll19890224 opened this issue Dec 22, 2022 · 1 comment

Comments

@wll19890224
Copy link

Please adapt to Android13 dynamic application notification permission

@eeppee-admin
Copy link

eeppee-admin commented Jan 22, 2025

It works with my android 13 device(API 33), example is:
do <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> in AndroidManifest.xml first
then fix the dynamic permission grant with below code

/** ref link: https://github.com/navczydev/NotificationPermissionAndroid13/blob/master/app/src/main/java/com/example/myapplication/MainActivity.kt#L50 */
private fun sendANotification() {
        when {
            ContextCompat.checkSelfPermission(
                this, Manifest.permission.POST_NOTIFICATIONS
            ) == PackageManager.PERMISSION_GRANTED -> {
                // You can use the API that requires the permission.
                sendNotification(this)
            }

            shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS) -> {
                Snackbar.make(
                    findViewById(R.id.main),
                    "Notification blocked",
                    Snackbar.LENGTH_LONG
                ).setAction("Settings") {
                    // Responds to click on the action
                    val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    val uri: Uri = Uri.fromParts("package", packageName, null)
                    intent.data = uri
                    startActivity(intent)
                }.show()
            }

            else -> {
                // You can directly ask for the permission.
                // The registered ActivityResultCallback gets the result of this request.
                if (Build.VERSION.SDK_INT >= 33) {
                    requestPermissionLauncher.launch(
                        Manifest.permission.POST_NOTIFICATIONS
                    )
                }
            }
        }

        /** ref link: https://github.com/Karn/notify*/
        Notify
            .with(this)
            .content { // this: Payload.Content.Default
                title = "你好呀"
                text = "哈哈哈"
            }
            .show()
    }

use this sendANotification() method at MainActivity.kt onCreate() method

by the way, I install the apk twice to fix Notification show up problem, it seems grant POST_NOTIFICATIONS permission not really show notification immediately

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants