FirebaseMessagingService onMessageReceived覆盖错误

时间:2019-10-09 13:39:59

标签: android in-app-purchase google-cloud-messaging

班级:

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

3 个答案:

答案 0 :(得分: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)
        }
    }
}

答案 1 :(得分: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)
            }
        }
    }
}

答案 2 :(得分:0)

它可以通过更改来解决 override fun onMessageReceived(remoteMessage: RemoteMessage?)override fun onMessageReceived(p0: RemoteMessage)

只需在类中的每个位置用p0替换“ remoteMessage”即可。

如果您在onNewToken上遇到同样的错误,也可以这样做 将override fun onNewToken(token: String?)更改为override fun onNewToken(p0: String)