Class:
class SubscriptionMessageService : FirebaseMessagingService() {
companion object {
private val TAG = SubscriptionMessageService::class.java.simpleName
private const val REMOTE_MESSAGE_SUBSCRIPTIONS_KEY = "currentStatus"
}
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
remoteMessage?.data?.let {
val data = it
if (data.isNotEmpty()) {
var result: List<SubscriptionStatus>? = null
if (REMOTE_MESSAGE_SUBSCRIPTIONS_KEY in data) {
result = data[REMOTE_MESSAGE_SUBSCRIPTIONS_KEY]?.let {
SubscriptionStatus.listFromJsonString(it)
}
}
if (result == null) {
Log.e(TAG, "Invalid subscription data")
} else {
val app = application as SubApp
app.repository.updateSubscriptionsFromNetwork(result)
}
}
}
}
}
错误:'onMessageReceived'不会覆盖任何内容
RemoteMessage不可为空您应该像这样更改
override fun onMessageReceived(remoteMessage: RemoteMessage) {
val data = remoteMessage.data
if (data.isNotEmpty()) {
var result: List<SubscriptionStatus>? = null
if (REMOTE_MESSAGE_SUBSCRIPTIONS_KEY in data) {
result = data[REMOTE_MESSAGE_SUBSCRIPTIONS_KEY]?.let {
SubscriptionStatus.listFromJsonString(it)
}
}
if (result == null) {
Log.e(TAG, "Invalid subscription data")
} else {
val app = application as SubApp
app.repository.updateSubscriptionsFromNetwork(result)
}
}
}
我以这种方式工作:
override fun onMessageReceived(remoteMessage: RemoteMessage) {
remoteMessage?.data?.let {
val data = it
if (data.isNotEmpty()) {
var result: List<SubscriptionStatus>? = null
if (REMOTE_MESSAGE_SUBSCRIPTIONS_KEY in data) {
result = data[REMOTE_MESSAGE_SUBSCRIPTIONS_KEY]?.let {
SubscriptionStatus.listFromJsonString(it)
}
}
if (result == null) {
Log.e(TAG, "Invalid subscription data")
} else {
val app = application as SubApp
app.repository.updateSubscriptionsFromNetwork(result)
}
}
}
}