自定义通知声音不起作用Android Studio FCM

时间:2020-06-16 06:53:24

标签: android firebase-cloud-messaging

我正在尝试在Android App中收到通知时设置自定义声音。我为此使用FCM和Android Studio。即使我在res / raw文件夹中添加了自定义声音,并在要发送到fcm / send API的输入json中设置了声音属性,当在应用程序中收到通知时,仍会收到默认的通知声音。不知道哪里出了问题。有人可以对此提出建议。 FCM /发送API的输入Json为

{
  "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)
    }
}

0 个答案:

没有答案