Files
rider-pro-android-app/app/src/main/java/com/aiosman/ravenow/Messaging.kt

58 lines
1.9 KiB
Kotlin
Raw Normal View History

2024-11-17 20:07:42 +08:00
package com.aiosman.ravenow
2024-09-05 22:04:41 +08:00
2024-09-18 17:03:26 +08:00
import android.content.Context
2024-09-05 22:04:41 +08:00
import android.util.Log
2024-09-18 17:03:26 +08:00
import cn.jpush.android.api.JPushInterface
2024-11-17 20:07:42 +08:00
import com.aiosman.ravenow.data.AccountService
import com.aiosman.ravenow.data.AccountServiceImpl
2024-09-05 22:04:41 +08:00
import com.google.android.gms.tasks.OnCompleteListener
import com.google.firebase.messaging.FirebaseMessaging
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
object Messaging {
2024-10-12 10:07:18 +08:00
fun registerDevice(scope: CoroutineScope, context: Context) {
2024-09-18 17:03:26 +08:00
registerJpush(scope, context)
// registerFCM(scope)
}
2024-10-12 10:07:18 +08:00
suspend fun unregisterDevice(context: Context) {
unregisterJpush(context)
}
2024-09-18 17:03:26 +08:00
fun registerJpush(scope: CoroutineScope, context: Context) {
val accountService: AccountService = AccountServiceImpl()
val regId = JPushInterface.getRegistrationID(context)
scope.launch {
accountService.registerMessageChannel(client = "jpush", identifier = regId)
}
}
2024-10-12 10:07:18 +08:00
private suspend fun unregisterJpush(context: Context) {
val accountService: AccountService = AccountServiceImpl()
val regId = JPushInterface.getRegistrationID(context)
accountService.unregisterMessageChannel(client = "jpush", identifier = regId)
}
2024-09-18 17:03:26 +08:00
fun registerFCM(scope: CoroutineScope) {
2024-09-05 22:04:41 +08:00
val accountService: AccountService = AccountServiceImpl()
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
Log.w("Pushing", "Fetching FCM registration token failed", task.exception)
return@OnCompleteListener
}
// Get new FCM registration token
val token = task.result
// Log and toast
Log.d("Pushing", token)
scope.launch {
accountService.registerMessageChannel(client = "fcm", token)
}
})
}
2024-09-18 17:03:26 +08:00
2024-09-05 22:04:41 +08:00
}