Skip to content

Commit

Permalink
Added support for Huawei notifications (#136)
Browse files Browse the repository at this point in the history
* Implement registerHuawei method

* Update push service docs

* Fix ktlint

* Improve docs
  • Loading branch information
Hopsaheysa authored Feb 15, 2024
1 parent 6405166 commit ee125bc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 9 deletions.
41 changes: 35 additions & 6 deletions docs/Using-Push-Service.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,21 @@ __Optional parameters:__
All available methods of the `IPushService` API are:

- `acceptLanguage` - Language settings, that will be sent along with each request.
- `register(fcmToken: String, listener: IPushRegisterListener)` - Registers Firebase Cloud Messaging token on the backend
- `fcmToken` - Firebase Cloud Messaging token.
- `listener` - Called when the request finishes.
- `register(fcmToken: String, callback: (result: Result<Unit>) -> Unit)` - Registers Firebase Cloud Messaging token on the backend
- `registerHuawei(hmsToken: String, callback: (result: Result<Unit>) -> Unit)` - Registers Huawei Mobile Services token on the backend

## Registering to Push Notifications
Messaging token on the backend

- `fcmToken` - Firebase Cloud Messaging token.
- `hmsToken` - Huawei Mobile Services token
- `callback` - Called when the request finishes.

## Registering to Push Notifications
### Android (with Google Play Services)
To register an app to push notifications, you can simply call the `register` method:

```kotlin
// first, retrieve FireBase token
// first, retrieve Firebase token (do so in the background thread)
FirebaseInstanceId.getInstance().instanceId.addOnCompleteListener { task ->
if (task.isSuccessful) {
task.result?.token?.let { token ->
Expand All @@ -85,9 +90,33 @@ FirebaseInstanceId.getInstance().instanceId.addOnCompleteListener { task ->

To be able to successfully process notifications, you need to register the app to receive push notifications in the first place. For more information visit [official documentation](https://firebase.google.com/docs/cloud-messaging/android/client).

### Huawei (HarmonyOS / EMUI)
For Huawei devices, you can also register your app to receive push notifications using Huawei Push Kit. To integrate Huawei Push Kit into your app, please refer to the Huawei Push Kit documentation.

```kotlin
// first, retrieve HMS token (do so in the background thread)
try {
val appId = AGConnectOptionsBuilder().build(appContext).getString("client/app_id")
val token = HmsInstanceId.getInstance(appContext).getToken(appId, "HCM")

if (token.isNotEmpty()) {
pushService.registerHuawei(token) {
it.onSuccess {
// push notification registered
}.onFailure {
// push notification registration failed
}
} else {
// token retrieval failed
}
} catch (e: Exception) {
// token retrieval failed
}
```
For more information visit [official documentation](https://developer.huawei.com/consumer/en/doc/hmscore-guides/android-client-dev-0000001050042041)
## Receiving WMT Push Notifications

To process the raw notification obtained from Firebase Cloud Messaging service (FCM), you can use `PushParser` helper class that will parse the notification into a `PushMessage` result.
To process the raw notification obtained from Firebase Cloud Messaging service (FCM) or from (HMS) HUAWEI Mobile Services, you can use `PushParser` helper class that will parse the notification into a `PushMessage` result.

The `PushMessage` is an abstract class that is implemented by following classes for concrete results

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@ import com.google.gson.annotations.SerializedName
*/
internal data class PushRegistrationRequestObject(
@SerializedName("token")
val token: String
) {
val token: String,

@SerializedName("platform")
val platform: String = "android"
val platform: Platform = Platform.ANDROID
)
/**
* Enum representing the platform for push registration.
*/
internal enum class Platform {
@SerializedName("android")
ANDROID,

@SerializedName("huawei")
HUAWEI
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@ interface IPushService {
* @param callback Result listener
*/
fun register(fcmToken: String, callback: (result: Result<Unit>) -> Unit)

/**
* Registers HMS on backend to receive notifications about operations
* @param hmsToken Huawei Push Token
* @param callback Result listener
*/
fun registerHuawei(hmsToken: String, callback: (result: Result<Unit>) -> Unit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.wultra.android.mtokensdk.push
import android.content.Context
import com.wultra.android.mtokensdk.api.push.PushApi
import com.wultra.android.mtokensdk.api.push.PushRegistrationRequest
import com.wultra.android.mtokensdk.api.push.model.Platform
import com.wultra.android.mtokensdk.api.push.model.PushRegistrationRequestObject
import com.wultra.android.mtokensdk.common.Logger
import com.wultra.android.powerauth.networking.IApiCallResponseListener
Expand Down Expand Up @@ -90,4 +91,20 @@ class PushService(okHttpClient: OkHttpClient, baseURL: String, powerAuthSDK: Pow
}
)
}

override fun registerHuawei(hmsToken: String, callback: (result: Result<Unit>) -> Unit) {
pushApi.registerToken(
PushRegistrationRequest(PushRegistrationRequestObject(hmsToken, Platform.HUAWEI)),
object : IApiCallResponseListener<StatusResponse> {
override fun onSuccess(result: StatusResponse) {
callback(Result.success(Unit))
}

override fun onFailure(error: ApiError) {
Logger.e("Failed to register hms token for WMT push notifications.")
callback(Result.failure(ApiErrorException(error)))
}
}
)
}
}

0 comments on commit ee125bc

Please sign in to comment.