Check whether you have notification permission

NotificationManagerCompat.from(context).areNotificationsEnabled()
Copy the code

Open the Notification permission Settings page

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings

object NotificationUtil {
    // Call this method to get whether the notification bar permission is enabled
    fun goToNotificationSetting(context: Context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // This solution applies to API 26, i.e. 8.0 and above
            try {
                val intent = Intent()
                intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
                intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
                intent.putExtra(Settings.EXTRA_CHANNEL_ID, context.applicationInfo.uid)
                context.startActivity(intent)
            } catch (e: Exception) {
                toPermissionSetting(context)
            }
        } else {
            toPermissionSetting(context)
        }
    }

    /** * jumps to permission Settings **@param activity
     */
    private fun toPermissionSetting(activity: Context) {
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
            toSystemConfig(activity)
        } else {
            try {
                toApplicationInfo(activity)
            } catch (e: java.lang.Exception) {
                L.printStackTrace(e)
                toSystemConfig(activity)
            }
        }
    }

    /** * Application information interface **@param activity
     */
    private fun toApplicationInfo(activity: Context) {
        try {
            val localIntent = Intent()
            localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            localIntent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
            localIntent.data = Uri.fromParts("package", activity.packageName, null)
            activity.startActivity(localIntent)
        } catch (e: java.lang.Exception) {
            L.printStackTrace(e)
        }
    }

    /** * System setup interface **@param activity
     */
    private fun toSystemConfig(activity: Context) {
        try {
            val intent = Intent(Settings.ACTION_SETTINGS)
            activity.startActivity(intent)
        } catch (e: java.lang.Exception) {
            L.printStackTrace(e)
        }
    }

}
Copy the code