FCMのAPI変更に対応

This commit is contained in:
tateisu 2018-08-09 05:08:15 +09:00
parent 61c019f2f3
commit ecfcb37ba3
4 changed files with 38 additions and 34 deletions

View File

@ -268,15 +268,6 @@
</intent-filter>
</service>
<service
android:name=".MyFirebaseInstanceIDService"
tools:ignore="ExportedService"
>
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
See README(https://goo.gl/l4GJaQ) for more. -->
<meta-data

View File

@ -15,17 +15,6 @@ class MyFirebaseInstanceIDService : FirebaseInstanceIdService() {
override fun onTokenRefresh() {
super.onTokenRefresh()
try {
val token = FirebaseInstanceId.getInstance().token
log.d("onTokenRefresh: instance_token=%s", token)
PrefDevice.prefDevice(this).edit().putString(PrefDevice.KEY_DEVICE_TOKEN, token).apply()
PollingWorker.queueFCMTokenUpdated(this)
} catch(ex : Throwable) {
log.trace(ex)
}
}

View File

@ -2,6 +2,7 @@ package jp.juggler.subwaytooter
import android.content.Intent
import android.os.Build
import com.google.firebase.iid.FirebaseInstanceId
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
@ -39,4 +40,19 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
context.startService(intent)
}
}
override fun onNewToken(token : String?) {
try {
token?: return
log.d("onTokenRefresh: token=%s", token)
PrefDevice.prefDevice(this).edit().putString(PrefDevice.KEY_DEVICE_TOKEN, token).apply()
PollingWorker.queueFCMTokenUpdated(this)
} catch(ex : Throwable) {
log.trace(ex)
}
}
}

View File

@ -61,6 +61,9 @@ class PollingWorker private constructor(c : Context) {
companion object {
internal val log = LogCategory("PollingWorker")
private const val FCM_SENDER_ID ="433682361381"
private const val FCM_SCOPE = "FCM"
const val NOTIFICATION_ID = 1
const val NOTIFICATION_ID_ERROR = 3
@ -112,15 +115,21 @@ class PollingWorker private constructor(c : Context) {
return s
}
fun getDeviceId(context : Context) : String? {
// 設定ファイルに保持されていたらそれを使う
val prefDevice = PrefDevice.prefDevice(context)
var device_token = prefDevice.getString(PrefDevice.KEY_DEVICE_TOKEN, null)
if(device_token?.isNotEmpty() == true) return device_token
try {
device_token = FirebaseInstanceId.getInstance().token
// FirebaseのAPIから取得する
device_token = FirebaseInstanceId.getInstance().getToken(FCM_SENDER_ID,FCM_SCOPE)
if(device_token?.isNotEmpty() == true) {
prefDevice.edit().putString(PrefDevice.KEY_DEVICE_TOKEN, device_token)
prefDevice
.edit()
.putString(PrefDevice.KEY_DEVICE_TOKEN, device_token)
.apply()
return device_token
}
@ -143,26 +152,25 @@ class PollingWorker private constructor(c : Context) {
var sv = prefDevice.getString(PrefDevice.KEY_INSTALL_ID, null)
if(sv?.isNotEmpty() == true) return sv
SavedAccount.clearRegistrationCache()
try {
var device_token = prefDevice.getString(PrefDevice.KEY_DEVICE_TOKEN, null)
if(device_token == null || device_token.isEmpty()) {
if(device_token?.isEmpty() != false ) {
try {
device_token = FirebaseInstanceId.getInstance().token
device_token = FirebaseInstanceId.getInstance().getToken(FCM_SENDER_ID,FCM_SCOPE)
if(device_token == null || device_token.isEmpty()) {
log.e("getInstallId: missing device token.")
return null
} else {
prefDevice.edit().putString(PrefDevice.KEY_DEVICE_TOKEN, device_token)
.apply()
prefDevice.edit().putString(PrefDevice.KEY_DEVICE_TOKEN, device_token).apply()
}
} catch(ex : Throwable) {
log.e("getInstallId: could not get device token.")
log.trace(ex)
return null
}
}
val request = Request.Builder()
@ -1097,8 +1105,8 @@ class PollingWorker private constructor(c : Context) {
client.account = account
val wps = PushSubscriptionHelper(context,account)
if( wps.flags != 0) {
val wps = PushSubscriptionHelper(context, account)
if(wps.flags != 0) {
job.bPollingRequired.set(true)
}