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> </intent-filter>
</service> </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. <!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
See README(https://goo.gl/l4GJaQ) for more. --> See README(https://goo.gl/l4GJaQ) for more. -->
<meta-data <meta-data

View File

@ -15,17 +15,6 @@ class MyFirebaseInstanceIDService : FirebaseInstanceIdService() {
override fun onTokenRefresh() { override fun onTokenRefresh() {
super.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.content.Intent
import android.os.Build import android.os.Build
import com.google.firebase.iid.FirebaseInstanceId
import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage import com.google.firebase.messaging.RemoteMessage
@ -39,4 +40,19 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
context.startService(intent) 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 { companion object {
internal val log = LogCategory("PollingWorker") 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 = 1
const val NOTIFICATION_ID_ERROR = 3 const val NOTIFICATION_ID_ERROR = 3
@ -112,15 +115,21 @@ class PollingWorker private constructor(c : Context) {
return s return s
} }
fun getDeviceId(context : Context) : String? { fun getDeviceId(context : Context) : String? {
// 設定ファイルに保持されていたらそれを使う
val prefDevice = PrefDevice.prefDevice(context) val prefDevice = PrefDevice.prefDevice(context)
var device_token = prefDevice.getString(PrefDevice.KEY_DEVICE_TOKEN, null) var device_token = prefDevice.getString(PrefDevice.KEY_DEVICE_TOKEN, null)
if(device_token?.isNotEmpty() == true) return device_token if(device_token?.isNotEmpty() == true) return device_token
try { try {
device_token = FirebaseInstanceId.getInstance().token // FirebaseのAPIから取得する
device_token = FirebaseInstanceId.getInstance().getToken(FCM_SENDER_ID,FCM_SCOPE)
if(device_token?.isNotEmpty() == true) { if(device_token?.isNotEmpty() == true) {
prefDevice.edit().putString(PrefDevice.KEY_DEVICE_TOKEN, device_token) prefDevice
.edit()
.putString(PrefDevice.KEY_DEVICE_TOKEN, device_token)
.apply() .apply()
return device_token return device_token
} }
@ -143,26 +152,25 @@ class PollingWorker private constructor(c : Context) {
var sv = prefDevice.getString(PrefDevice.KEY_INSTALL_ID, null) var sv = prefDevice.getString(PrefDevice.KEY_INSTALL_ID, null)
if(sv?.isNotEmpty() == true) return sv if(sv?.isNotEmpty() == true) return sv
SavedAccount.clearRegistrationCache() SavedAccount.clearRegistrationCache()
try { try {
var device_token = prefDevice.getString(PrefDevice.KEY_DEVICE_TOKEN, null) var device_token = prefDevice.getString(PrefDevice.KEY_DEVICE_TOKEN, null)
if(device_token == null || device_token.isEmpty()) { if(device_token?.isEmpty() != false ) {
try { try {
device_token = FirebaseInstanceId.getInstance().token device_token = FirebaseInstanceId.getInstance().getToken(FCM_SENDER_ID,FCM_SCOPE)
if(device_token == null || device_token.isEmpty()) { if(device_token == null || device_token.isEmpty()) {
log.e("getInstallId: missing device token.") log.e("getInstallId: missing device token.")
return null return null
} else { } else {
prefDevice.edit().putString(PrefDevice.KEY_DEVICE_TOKEN, device_token) prefDevice.edit().putString(PrefDevice.KEY_DEVICE_TOKEN, device_token).apply()
.apply()
} }
} catch(ex : Throwable) { } catch(ex : Throwable) {
log.e("getInstallId: could not get device token.") log.e("getInstallId: could not get device token.")
log.trace(ex) log.trace(ex)
return null return null
} }
} }
val request = Request.Builder() val request = Request.Builder()