Messaging consists of two different parts: Messaging
and MessagingService
. The difference is that the Messaging
instance is used to send messages upstream and subscribe to topics, whereas the MessagingService
handles downstream messages.
Be sure to active Huawei Push Kit in AppGallery Connect!
Subscribe to token changes:
val tokenObserver = object : DisposableObserver<String>() {
override fun onNext(token: String) {}
override fun onError(e: Throwable?) {}
override fun onComplete() {}
}
MessagingRepositoryFactory.getMessagingService()
.getNewTokenObservable()
.subscribeWith(tokenObserver)
Get token once:
MessagingRepositoryFactory.getMessagingService()
.requestToken(context)
Receive messages:
val messageObserver: DisposableObserver<RemoteMessage> = object : DisposableObserver<RemoteMessage>() {
override fun onNext(remoteMessage: RemoteMessage) {
val from = remoteMessage.from
val data = remoteMessage.data
val title = remoteMessage.notification?.title
}
override fun onError(e: Throwable?) {}
override fun onComplete() {}
}
MessagingRepositoryFactory.getMessagingService()
.getMessageReceivedObservable()
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(messageObserver)
Subscribe to topic:
MessagingFactory.getMessaging().subscribeToTopic("News")
Send message:
val message = RemoteMessage
.Builder(destination)
.setMessageId(id)
.addData("my_message", "Hello World")
.build()
MessagingFactory.getMessaging().send(message)