|
@@ -1,8 +1,14 @@
|
|
|
package io.nexilis.service.core
|
|
|
|
|
|
+import android.Manifest
|
|
|
import android.content.Context
|
|
|
+import android.content.pm.PackageManager
|
|
|
import android.util.Log
|
|
|
-import io.newuniverse.SDK.nuSDKService
|
|
|
+import androidx.core.app.ActivityCompat
|
|
|
+import androidx.core.app.NotificationManagerCompat
|
|
|
+import com.google.android.gms.tasks.OnCompleteListener
|
|
|
+import com.google.firebase.messaging.FirebaseMessaging
|
|
|
+import io.nexilis.service.Api
|
|
|
import io.nexilis.service.Service
|
|
|
import io.nexilis.service.apiScope
|
|
|
import io.nexilis.service.data.entities.Buddy
|
|
@@ -11,31 +17,14 @@ import io.nexilis.service.data.entities.GroupMember
|
|
|
import io.nexilis.service.data.entities.Message
|
|
|
import io.nexilis.service.data.entities.MessageStatus
|
|
|
import io.nexilis.service.data.rooms.ApiRoomDatabase
|
|
|
-import io.nexilis.service.pass
|
|
|
import io.nexilis.service.tag
|
|
|
import kotlinx.coroutines.launch
|
|
|
import org.json.JSONArray
|
|
|
import java.util.zip.ZipInputStream
|
|
|
|
|
|
-class Incoming private constructor() {
|
|
|
+class Incoming(private val context: Context) {
|
|
|
|
|
|
- private val context: Context by lazy { nuSDKService.getInstance(pass) as Context }
|
|
|
-
|
|
|
- companion object {
|
|
|
-
|
|
|
- @Volatile
|
|
|
- private var INSTANCE: Incoming? = null
|
|
|
-
|
|
|
- fun getInstance(): Incoming {
|
|
|
- return INSTANCE ?: synchronized(this) {
|
|
|
- val instance = Incoming()
|
|
|
- INSTANCE = instance
|
|
|
- instance
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- fun process(data: Data) {
|
|
|
+ fun process(data: Data, completion: ((Boolean) -> Unit)? = null) {
|
|
|
Log.d(tag, "process(${data.code}):$data")
|
|
|
apiScope.launch {
|
|
|
val callback = Service.map.remove(data.status)
|
|
@@ -65,7 +54,7 @@ class Incoming private constructor() {
|
|
|
}
|
|
|
|
|
|
"S0" -> {
|
|
|
- pushMessage(data)
|
|
|
+ pushMessage(data, completion)
|
|
|
}
|
|
|
|
|
|
"INQ" -> {
|
|
@@ -108,6 +97,14 @@ class Incoming private constructor() {
|
|
|
f_pin = data.f_pin
|
|
|
)
|
|
|
)
|
|
|
+ FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
|
|
|
+ if (!task.isSuccessful) {
|
|
|
+ Log.w(tag, "Fetching FCM registration token failed", task.exception)
|
|
|
+ return@OnCompleteListener
|
|
|
+ }
|
|
|
+ val token = task.result
|
|
|
+ Api.sendToken(context, token)
|
|
|
+ })
|
|
|
}
|
|
|
} catch (e: Exception) {
|
|
|
Log.e(tag, e.message, e)
|
|
@@ -369,7 +366,7 @@ class Incoming private constructor() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private suspend fun pushMessage(data: Data) {
|
|
|
+ private suspend fun pushMessage(data: Data, completion: ((Boolean) -> Unit)? = null) {
|
|
|
try {
|
|
|
val messageId = data.bodies["A18"] ?: ""
|
|
|
if (messageId.isEmpty()) return
|
|
@@ -471,6 +468,25 @@ class Incoming private constructor() {
|
|
|
)
|
|
|
}
|
|
|
Service.sendAck(me, data.status)
|
|
|
+ completion?.let { it(true) }
|
|
|
+ with(NotificationManagerCompat.from(context)) {
|
|
|
+ if (ActivityCompat.checkSelfPermission(
|
|
|
+ context,
|
|
|
+ Manifest.permission.POST_NOTIFICATIONS
|
|
|
+ ) != PackageManager.PERMISSION_GRANTED
|
|
|
+ ) {
|
|
|
+ return@with
|
|
|
+ }
|
|
|
+ val buddy = ApiRoomDatabase.getDatabase(context).buddyDao().getBuddySync(opposite)
|
|
|
+ Log.d(tag, "getBuddy:${buddy}")
|
|
|
+ notify(
|
|
|
+ 112346,
|
|
|
+ ChatNotification(context).getNotification(
|
|
|
+ "${buddy.first_name} ${buddy.last_name}".trim(),
|
|
|
+ data.bodies["A07"]?.toNormalString() ?: ""
|
|
|
+ )
|
|
|
+ )
|
|
|
+ }
|
|
|
} catch (e: Exception) {
|
|
|
Log.e(tag, e.message, e)
|
|
|
}
|