You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
csamsclient/lib/src/main/java/com/gyf/lib/service/MessageService.kt

63 lines
2.2 KiB

package com.gyf.lib.service
import android.content.Intent
import androidx.core.app.JobIntentService
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.gyf.lib.R
import com.gyf.lib.util.*
import com.orhanobut.logger.Logger
class MessageService : JobIntentService() {
lateinit var clientType: ClientType
val gson = Gson()
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
clientType = intent?.getSerializableExtra(ClientType::class.java.name) as ClientType
return super.onStartCommand(intent, flags, startId)
}
override fun onCreate() {
super.onCreate()
Logger.i("服务启动")
}
override fun onDestroy() {
super.onDestroy()
Logger.i("服务销毁")
}
override fun onHandleWork(intent: Intent) {
TokenManager.token?.let { it ->
HttpClient.postAsync<List<NotificationVo>>(
url = Api.buildUrl(NotificationApi.Pull),
jsonParam = NotificationDto(
receiverId = it.id,
receiverClient = clientType,
token = it
),
type = object : TypeToken<ApiResponse<List<NotificationVo>>>() {}.type
)?.let { it1 ->
Logger.i("拉取最新通知")
it1.body?.forEach {
Logger.i("构造通知【${it.title}")
val builder =
NotificationCompat.Builder(applicationContext, NotificationUtil.CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(it.title)
.setContentText(it.content)
.setPriority(NotificationCompat.PRIORITY_HIGH)
builder.build()
with(NotificationManagerCompat.from(applicationContext)) {
// notificationId is a unique int for each notification that you must define
notify(it.id, builder.build())
}
}
}
}
}
}