Skip to content

Latest commit

 

History

History
68 lines (56 loc) · 2.02 KB

messaging.md

File metadata and controls

68 lines (56 loc) · 2.02 KB

Messaging (Push Kit)

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.

Setup

Be sure to active Huawei Push Kit in AppGallery Connect!

Usage

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)

Request token once in order to get token in tokenObserver (see above):

MessagingRepositoryFactory.getMessagingService().requestToken(context)

Directly get token via suspending function:

MessagingRepositoryFactory.getMessagingService().getToken(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)

Links