通知没有声音或振动

时间:2020-10-07 16:07:22

标签: android kotlin notifications

我将数据发布到fcm后发出了通知,当我收到回传的数据时,通知没有问题,但没有声音没有振动,即使我确保在手机中启用了声音和颤音,我我不确定我做错了什么,请帮我解决,谢谢。

  • 这是我的应用程序类代码,在其中设置了oreo +的通知渠道
override fun onCreate() {
       super.onCreate()
       Timber.plant(Timber.DebugTree())
       setUpNotification()

   }

   private fun setUpNotification(){
       if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
           val notificationChannel  = NotificationChannel(
               getString(R.string.channelid),getString(R.string.channelstring),
               NotificationManager.IMPORTANCE_DEFAULT)

           val ringtone  = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
           val audioAttributes = AudioAttributes.Builder()
               .setUsage(AudioAttributes.USAGE_NOTIFICATION)
               .build()
           notificationChannel.description = "Notifications"
           notificationChannel.enableLights(true)
           notificationChannel.enableVibration(true)
           notificationChannel.lightColor = Color.BLUE
           notificationChannel.setSound(ringtone,audioAttributes)

           val notificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as 
          NotificationManager
           notificationManager.createNotificationChannel(notificationChannel)
       }
   } 
  • 这是我的通知代码
private fun FireNotification(title : String, body : String, isVibrationEnabled : Boolean, 
      notificationchannelid : String){
       
        val intent = Intent(applicationContext,MainActivity::class.java)
        val taskBuilder = TaskStackBuilder.create(applicationContext).run {
            addNextIntentWithParentStack(intent)
            getPendingIntent(1,PendingIntent.FLAG_UPDATE_CURRENT)
        }

         val vibratePattern = longArrayOf(1000L, 1000L, 1000L, 1000L)

         val notification = NotificationCompat.Builder(applicationContext, notificationchannelid)
         notification.apply {
             setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
             setContentTitle(title)
             setContentText(body)
             setContentIntent(taskBuilder)
             setSmallIcon(R.drawable.ic_baseline_notifications_active_24)
             addAction(R.drawable.default_icon,"Snooze",taskBuilder)
             addAction(R.drawable.default_icon,"Action",taskBuilder)
             priority = NotificationCompat.PRIORITY_HIGH
             if(isVibrationEnabled){
                 notification.setVibrate(vibratePattern)
             }
         }
        NotificationManagerCompat.from(applicationContext).notify(counter++,notification.build())
    } 

1 个答案:

答案 0 :(得分:1)

解决我的问题的方法是简单地将默认声音设置如下

setDefaults(NotificationCompat.DEFAULT_SOUND)