Android 安卓-警报声通知

Android 安卓-警报声通知,android,kotlin,notifications,Android,Kotlin,Notifications,我有一段代码,应该在启动时显示一个持久的通知(必须让用户知道发生了重要的事情,比如警报) 除了我与声音/振动/光作斗争之外,一切都很好 电话:诺基亚8.1 声音:我想要TYPE_报警声,但我每次都会收到TYPE_通知,即使铃声标题是“timer”,这是我在手机设置中设置的报警声 振动:它不会自动振动,但只有在声音被禁用时才会振动。这是硬件限制吗?我想我可以同时喜欢声音和振动。另外,振动总是一样的,似乎不是习惯性的 另外,我想在手机锁定时打开手机。代码可以工作,但不推荐使用PowerMan

我有一段代码,应该在启动时显示一个持久的通知(必须让用户知道发生了重要的事情,比如警报)

除了我与声音/振动/光作斗争之外,一切都很好

电话:诺基亚8.1

  • 声音:我想要TYPE_报警声,但我每次都会收到TYPE_通知,即使铃声标题是“timer”,这是我在手机设置中设置的报警声

  • 振动:它不会自动振动,但只有在声音被禁用时才会振动。这是硬件限制吗?我想我可以同时喜欢声音和振动。另外,振动总是一样的,似乎不是习惯性的

另外,我想在手机锁定时打开手机。代码可以工作,但不推荐使用
PowerManager.SCREEN\u BRIGHT\u WAKE\u LOCK
。我尝试了
WindowManager.LayoutParams.FLAG\u KEEP\u SCREEN\u
,但它不起作用

我的代码:

class AlertNotification(private val context: Context) {

    companion object {
        private const val TAG = "AlertNotification"
        private const val CHANNEL_ID = "ALERT_CHANNEL"
        private const val NOTIFICATION_ID = 1
    }

    init {
        createNotificationChannel()
    }

    fun launchAlertNotification(service: Service) {
        Log.d(TAG, "launchAlertNotification")

        // Prepare intent to launch Alert fragment when tapping on the notification
        val activityIntent = Intent(context, MainActivity::class.java)
        activityIntent.putExtra(MainActivity.EXTRA_FRAGMENT, AlertFragment.TAG)
        activityIntent.action = "dummy${NOTIFICATION_ID}"
        val pendingIntent = PendingIntent.getActivity(context, NOTIFICATION_ID, activityIntent, 0)

        val notificationSound: Uri =
            RingtoneManager.getActualDefaultRingtoneUri(service, RingtoneManager.TYPE_ALARM)
        val ringtone = RingtoneManager.getRingtone(service, notificationSound)
        Log.d(TAG, ringtone.getTitle(service))  // Display timer !

        // Build notification
        val builder = NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_pr_24)
            .setContentTitle(context.getString(R.string.alert_notification_title))
            .setContentText(context.getString(R.string.alert_notification_msg))
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setCategory(NotificationCompat.CATEGORY_ALARM)
            .setFullScreenIntent(pendingIntent, true)
            .setOngoing(true)
            .setSound(notificationSound)
            .setVibrate(longArrayOf(2000, 500, 0, 500, 2000))
            .setLights(Color.RED, 1000, 1000)
        var notification = builder.build()
        notification.flags = notification.flags or Notification.FLAG_INSISTENT

        wakeUpScreen()
        service.startForeground(NOTIFICATION_ID, notification)
    }

    private fun createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val name = context.getString(R.string.alert_notification_channel_name)
            val importance = NotificationManager.IMPORTANCE_HIGH
            val descriptionText = context.getString(R.string.alert_notification_channel_description)
            val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
                description = descriptionText
            }
            // Register the channel with the system
            val notificationManager: NotificationManager =
                context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }

    private fun wakeUpScreen() {
        val pm = context.getSystemService(Context.POWER_SERVICE) as PowerManager
        val isScreenOn = pm.isInteractive
        Log.e(TAG, "" + isScreenOn)
        if (!isScreenOn) {
            val wl = pm.newWakeLock(
                PowerManager.SCREEN_BRIGHT_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP,
                "ParaRescue:MyWakeLock"
            )
            wl.acquire(10000)
            //val cpuWl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "app:MyCpuLock")
            //cpuWl.acquire(10000)
        }
    }
}
在清单中,我还尝试添加以下内容:

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>


提前谢谢

我找到的答案是: