How to use media player for play a ringtone while one signal notification received in android

146 Views Asked by At

I am using onesignal for push notification. A call notification is received which has actions answer and reject. i wanna play a sound while this notification is received to 45 second.

does one-signal have any solution for playing a sound while call notification? is there any alternative solution like media player?

1

There are 1 best solutions below

0
Surajkaran Meghwanshi On BEST ANSWER

I Fixed my problem by using media player . In my app one signal notification clicks handled in Application class But I used MyFirebaseMessagingService class for real time notification handle when app are closed.

MyFirebaseMessagingService class

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()
            }
        }
    }
}

In Android Manifest

add this service in application tag.

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

in InComing Call Activity

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
}