Пользовательский звук уведомлений не работает Android Studio FCM - PullRequest
0 голосов
/ 16 июня 2020

Я пытаюсь установить собственный звук при получении уведомления в приложении Android. Я использую для этого FCM и Android Studio. Несмотря на то, что я добавил собственный звук в папку res / raw и установил атрибут звука на входе json, который отправляется в fcm / send API, я все равно получаю звук уведомления по умолчанию, когда уведомление получено в приложении. Не знаю, где я ошибаюсь. Может кто-нибудь предложить по этому поводу. Введите Json для API FCM / send:

{
  "registration_ids": [
    "xxxxx"
  ],
  "notification": {
    "body": "Notification",
    "title": "Enter_title",
    "sound": "the_purge_siren_ringtone.mp3",
    "android_channel_id": "admin_channel"
  },
  "data": {}
}

FirebaseMessagingService, который я использую при получении уведомления, выглядит следующим образом:

class MyFirebaseMessagingService: FirebaseMessagingService() {

    private val ADMIN_CHANNEL_ID = "admin_channel"

    override fun onMessageReceived(p0: RemoteMessage?) {
        super.onMessageReceived(p0)

        val intent = Intent(this, VisitorEntryActivity::class.java)
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val notificationID = Random().nextInt(3000)

        /*
        Apps targeting SDK 26 or above (Android O) must implement notification channels and add its notifications
        to at least one of them.
      */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            setupChannels(notificationManager)
        }

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        val pendingIntent = PendingIntent.getActivity(
            this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT
        )

        val largeIcon = BitmapFactory.decodeResource(
            resources,
            R.drawable.ic_notification_clear_all
        )

        //val notificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)
        val notificationBuilder = NotificationCompat.Builder(this, ADMIN_CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_notification_clear_all)
            .setLargeIcon(largeIcon)
            .setContentTitle(p0?.data?.get("title"))
            .setContentText(p0?.data?.get("message"))
            .setAutoCancel(true)
            //.setSound(notificationSoundUri)
            .setContentIntent(pendingIntent)

        //Set notification color to match your app color template
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            notificationBuilder.color = resources.getColor(R.color.background_dark)
        }
        notificationManager.notify(notificationID, notificationBuilder.build())
        //ri.play()
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private fun setupChannels(notificationManager: NotificationManager?) {
        val adminChannelName = "New notification"
        val adminChannelDescription = "Device to devie notification"
        val sound =
            Uri.parse("android.resource://"+this.getPackageName()+"/raw/the_purge_siren_ringtone")
        val attributes = AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_NOTIFICATION)
            .build()
        val adminChannel: NotificationChannel
        adminChannel = NotificationChannel(ADMIN_CHANNEL_ID, adminChannelName, NotificationManager.IMPORTANCE_HIGH)
        adminChannel.description = adminChannelDescription
        adminChannel.enableLights(true)
        adminChannel.lightColor = Color.RED
        adminChannel.enableVibration(true)
        adminChannel.setSound(sound, attributes);
        notificationManager?.createNotificationChannel(adminChannel)
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...