SubwayTooter-Android-App/app/src/main/java/jp/juggler/subwaytooter/MyFirebaseMessagingService.kt

55 lines
1.7 KiB
Kotlin
Raw Normal View History

package jp.juggler.subwaytooter
import android.content.Intent
import android.os.Build
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import jp.juggler.subwaytooter.notification.PollingForegrounder
import jp.juggler.subwaytooter.notification.PollingWorker
import jp.juggler.subwaytooter.pref.PrefDevice
2018-12-01 00:02:18 +01:00
import jp.juggler.util.LogCategory
class MyFirebaseMessagingService : FirebaseMessagingService() {
2021-06-13 13:48:48 +02:00
companion object {
internal val log = LogCategory("MyFirebaseMessagingService")
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
var tag: String? = null
val data = remoteMessage.data
for ((key, value) in data) {
log.d("onMessageReceived: $key=$value")
2021-06-13 13:48:48 +02:00
when (key) {
"notification_tag" -> tag = value
"acct" -> tag = "acct<>$value"
}
}
val context = applicationContext
val intent = Intent(context, PollingForegrounder::class.java)
if (tag != null) intent.putExtra(PollingWorker.EXTRA_TAG, tag)
if (Build.VERSION.SDK_INT >= 26) {
context.startForegroundService(intent)
} else {
context.startService(intent)
}
}
override fun onNewToken(token: String) {
try {
log.d("onTokenRefresh: token=$token")
PrefDevice.from(this).edit().putString(PrefDevice.KEY_DEVICE_TOKEN, token).apply()
2021-06-13 13:48:48 +02:00
PollingWorker.queueFCMTokenUpdated(this)
} catch (ex: Throwable) {
log.trace(ex)
}
}
}