如何使用媒体播放器播放铃声,同时在安卓系统中收到一个信号通知



我正在使用onesignal进行推送通知。接收到具有应答和拒绝操作的呼叫通知。我想在收到这个通知的45秒内播放一个声音。

一个信号有任何解决方案可以在呼叫通知时播放声音吗?有媒体播放器这样的替代方案吗?

我使用媒体播放器解决了我的问题。在我的应用程序中,一个信号通知点击在应用程序类中处理但当应用程序关闭时,我使用MyFirebaseMessagingService类进行实时通知处理。

MyFirebaseMessagingService类

class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onNewToken(token: String) {
super.onNewToken(token)
}
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
Timber.tag("message").d(message.toString())
val data = message.data
var notificationCount = true
data.values.forEach {
if (notificationCount) {
val modelNotification = Gson().fromJson(it, NotificationResponse::class.java)
val notification_type = modelNotification.a?.notificationType
if (notification_type == "callStart"){
playRingtone()
}
notificationCount = false
}
}
}
private fun playRingtone() {
if (!PH7User.isAppOpen){
if (!isPlaying){
mediaPlayer = MediaPlayer.create(applicationContext, R.raw.ringtone)
mediaPlayer.isLooping = true
isPlaying = true
mediaPlayer.start()
}
}
}
}

在Android清单

在应用程序标记中添加此服务。

<service
android:name=".network.firebase.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

即将到来的呼叫活动

lateinit var mediaPlayer : MediaPlayer
var isPlaying = false
lateinit var instance: IncomingCall //? = null
var isOpenIncoming = false

override fun onRendered(viewModel: ConsultationViewModel, binding: ActivityIncomingCallBinding) {
binding.apply {
activity = this@IncomingCall
vm = viewModel
instance = this@IncomingCall
isOpenIncoming = true
viewModel.doctorProfile.value = userProfile
if (!isPlaying) playRingtone()
tvName.text = "${getString(R.string.dr)} $name"
Glide.with(this@IncomingCall).load(userProfile).placeholder(R.drawable.ic_profile_bg).into(ivProfile)
//  broadcastReceiver()
}
SocketEvents.doctorCallReject {
lifecycleScope.launch {
try {
mediaPlayer.stop()
isPlaying = false
OneSignal.clearOneSignalNotifications()
finish()
} catch (e:Exception) {
toast(e.message.toString())
}
}
}
}

override fun onStop() {
super.onStop()
mediaPlayer.stop()
isPlaying = false
isOpenIncoming = false
}

最新更新