如何与FirebaseMessagingService实例互动

时间:2020-02-20 18:21:21

标签: android kotlin android-service android-notifications

我扩展了FirebaseMessagingService来处理来自后端的数据有效载荷

class MyFirebaseMessagingService: FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        remoteMessage.data.let {
            if (it.isNotEmpty()) {
                sendNotification()
            }
        }
    }

    override fun onNewToken(token: String) {
        ...
    }

    fun sendNotification() {
        ...
    }
}

现在,我可能还需要通过应用本身启动通知。 我知道我可以这样发送请求到系统通知管理器来做到这一点

val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(id, notification)

,但是将所有相关的东西都放在一个地方是很方便的,尤其是在MyFirebaseMessagingService类内部。

那么我如何获得对类实例的引用以能够调用它的公共方法?

0 个答案:

没有答案