FirebaseMessagingService onMessageReceived覆盖错误

问题描述 投票:0回答:1

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'不会覆盖任何内容

Github链接:https://github.com/googlesamples/android-play-billing/blob/master/ClassyTaxi/android/ClassyTaxi/app/src/main/java/com/example/subscriptions/data/network/firebase/SubscriptionMessageService.kt

android in-app-purchase google-cloud-messaging
1个回答
1
投票

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)
        }
    }
}

0
投票

我以这种方式工作:

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)
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.