parent
f15fd6588e
commit
61671b3f1b
@ -0,0 +1,26 @@ |
||||
package com.gyf.lib.service |
||||
|
||||
import android.content.Intent |
||||
import android.os.Bundle |
||||
import androidx.activity.ComponentActivity |
||||
import com.gyf.lib.util.ClientType |
||||
|
||||
abstract class BaseActivity : ComponentActivity() { |
||||
|
||||
abstract val clientType: ClientType |
||||
|
||||
private val serviceIntent |
||||
get() = Intent(this, MessageService::class.java).apply { |
||||
putExtra(ClientType::class.java.name, clientType) |
||||
} |
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) { |
||||
super.onCreate(savedInstanceState) |
||||
startService(serviceIntent) |
||||
} |
||||
|
||||
override fun onResume() { |
||||
super.onResume() |
||||
startService(serviceIntent) |
||||
} |
||||
} |
@ -1,70 +0,0 @@ |
||||
package com.gyf.lib.service |
||||
|
||||
import android.content.Context |
||||
import androidx.work.Data |
||||
import androidx.work.Worker |
||||
import androidx.work.WorkerParameters |
||||
import com.google.gson.Gson |
||||
import com.google.gson.reflect.TypeToken |
||||
import com.gyf.lib.util.* |
||||
import com.orhanobut.logger.Logger |
||||
import java.net.SocketTimeoutException |
||||
|
||||
data class NotificationVo(val title: String, val content: String, val id: Int) |
||||
|
||||
data class PageDto(val currentPage: Long, val pageSize: Int = 10) |
||||
|
||||
data class NotificationDto( |
||||
val receiverId: Int, |
||||
val receiverClient: String, |
||||
override val token: Token, |
||||
val page: PageDto? = null |
||||
) : BaseToken() |
||||
|
||||
/** |
||||
* 通知接收客户端 |
||||
* |
||||
*/ |
||||
enum class ReceiverType { |
||||
//前台 |
||||
Foreground, |
||||
|
||||
//后台 |
||||
Background |
||||
} |
||||
|
||||
class NotificationWorker(context: Context, workerParams: WorkerParameters) : |
||||
Worker(context, workerParams) { |
||||
|
||||
override fun doWork(): Result { |
||||
TokenManager.token?.let { |
||||
return try { |
||||
Logger.i("开始拉取通知") |
||||
val data = HttpClient.postAsync<NotificationVo>( |
||||
url = Api.buildUrl(NotificationApi.Pull), |
||||
jsonParam = NotificationDto( |
||||
receiverId = it.id, |
||||
receiverClient = inputData.getString("receiverClient") |
||||
?: throw IllegalArgumentException("缺少receiverClient参数"), |
||||
token = it |
||||
), |
||||
type = object : TypeToken<ApiResponse<NotificationVo>>() {}.type |
||||
) |
||||
val result = |
||||
Data.Builder().putString(NotificationVo::class.java.name, Gson().toJson(data)) |
||||
.build() |
||||
Logger.i("拉取通知成功:\n${result}") |
||||
Result.success(result) |
||||
} catch (e: SocketTimeoutException) { |
||||
Logger.e(e, "网络异常,拉取通知失败稍后再试") |
||||
Result.retry() |
||||
} catch (e: Exception) { |
||||
Logger.e(e, "发生未知异常,中止任务") |
||||
Result.failure() |
||||
} |
||||
} |
||||
Logger.w("找不到token信息,中止任务") |
||||
return Result.failure() |
||||
} |
||||
|
||||
} |
@ -0,0 +1,27 @@ |
||||
package com.gyf.lib.util |
||||
|
||||
|
||||
data class NotificationVo(val title: String, val content: String, val id: Int) |
||||
|
||||
data class PageDto(val currentPage: Long, val pageSize: Int = 10) |
||||
|
||||
data class NotificationDto( |
||||
val receiverId: Int, |
||||
val receiverClient: ClientType, |
||||
override val token: Token, |
||||
val page: PageDto? = null, |
||||
override val clientType: ClientType = receiverClient |
||||
) : ClientBaseVo() |
||||
|
||||
|
||||
/** |
||||
* 客户端类型 |
||||
* |
||||
*/ |
||||
enum class ClientType { |
||||
//前台 |
||||
Foreground, |
||||
|
||||
//后台 |
||||
Background |
||||
} |
Loading…
Reference in new issue