Huawei Push Kit

Huawei Push Kit

Huawei Push Kit is a messaging service provided for you. It establishes a messaging channel from the cloud to devices. By integrating Push Kit, you can send messages to your apps on users’ devices in real time.

The Apache Pekko Connectors Huawei Push Kit connector provides a way to send notifications with Huawei Push Kit.

Project Info: Apache Pekko Connectors Huawei Push Kit
Artifact
org.apache.pekko
pekko-connectors-huawei-push-kit
1.0.2
JDK versions
OpenJDK 8
OpenJDK 11
OpenJDK 17
Scala versions2.13.13, 2.12.19, 3.3.3
JPMS module namepekko.stream.connectors.huawei.pushkit
License
API documentation
Forums
Release notesGitHub releases
IssuesGithub issues
Sourceshttps://github.com/apache/pekko-connectors

Artifacts

sbt
val PekkoVersion = "1.0.2"
val PekkoHttpVersion = "1.0.0"
libraryDependencies ++= Seq(
  "org.apache.pekko" %% "pekko-connectors-huawei-push-kit" % "1.0.2",
  "org.apache.pekko" %% "pekko-stream" % PekkoVersion,
  "org.apache.pekko" %% "pekko-http" % PekkoHttpVersion,
  "org.apache.pekko" %% "pekko-http-spray-json" % PekkoHttpVersion
)
Maven
<properties>
  <pekko.version>1.0.2</pekko.version>
  <pekko.http.version>1.0.0</pekko.http.version>
  <scala.binary.version>2.13</scala.binary.version>
</properties>
<dependencies>
  <dependency>
    <groupId>org.apache.pekko</groupId>
    <artifactId>pekko-connectors-huawei-push-kit_${scala.binary.version}</artifactId>
    <version>1.0.2</version>
  </dependency>
  <dependency>
    <groupId>org.apache.pekko</groupId>
    <artifactId>pekko-stream_${scala.binary.version}</artifactId>
    <version>${pekko.version}</version>
  </dependency>
  <dependency>
    <groupId>org.apache.pekko</groupId>
    <artifactId>pekko-http_${scala.binary.version}</artifactId>
    <version>${pekko.http.version}</version>
  </dependency>
  <dependency>
    <groupId>org.apache.pekko</groupId>
    <artifactId>pekko-http-spray-json_${scala.binary.version}</artifactId>
    <version>${pekko.http.version}</version>
  </dependency>
</dependencies>
Gradle
def versions = [
  PekkoVersion: "1.0.2",
  PekkoHttpVersion: "1.0.0",
  ScalaBinary: "2.13"
]
dependencies {
  implementation "org.apache.pekko:pekko-connectors-huawei-push-kit_${versions.ScalaBinary}:1.0.2"
  implementation "org.apache.pekko:pekko-stream_${versions.ScalaBinary}:${versions.PekkoVersion}"
  implementation "org.apache.pekko:pekko-http_${versions.ScalaBinary}:${versions.PekkoHttpVersion}"
  implementation "org.apache.pekko:pekko-http-spray-json_${versions.ScalaBinary}:${versions.PekkoHttpVersion}"
}

The table below shows direct dependencies of this module and the second tab shows all libraries it depends on transitively.

Settings

All of the configuration settings for Huawei Push Kit can be found in the reference.conf.

sourcepekko.connectors.huawei.pushkit {
  app-id: "105260069"
  app-secret: "a192c0f08d03216b0f03b946918d5c725bbf54264a434227928c612012eefd24"
}

The test and maxConcurrentConnections parameters in HmsSettings are the predefined values. You can send test notifications (so called validate only). And you can set the number of maximum concurrent connections.

Sending notifications

To send a notification message create your notification object, and send it!

Scala
sourceimport org.apache.pekko
import pekko.actor.ActorSystem
import pekko.stream.connectors.huawei.pushkit._
import pekko.stream.connectors.huawei.pushkit.scaladsl.HmsPushKit
import pekko.stream.connectors.huawei.pushkit.models.AndroidConfig
import pekko.stream.connectors.huawei.pushkit.models.AndroidNotification
import pekko.stream.connectors.huawei.pushkit.models.BasicNotification
import pekko.stream.connectors.huawei.pushkit.models.ClickAction
import pekko.stream.connectors.huawei.pushkit.models.Condition
import pekko.stream.connectors.huawei.pushkit.models.ErrorResponse
import pekko.stream.connectors.huawei.pushkit.models.PushKitNotification
import pekko.stream.connectors.huawei.pushkit.models.PushKitResponse
import pekko.stream.connectors.huawei.pushkit.models.Response
import pekko.stream.connectors.huawei.pushkit.models.Tokens

val result1: Future[immutable.Seq[Response]] =
  Source
    .single(notification)
    .via(HmsPushKit.send(config))
    .map {
      case res @ PushKitResponse(code, msg, requestId) =>
        println(s"Response $res")
        res
      case res @ ErrorResponse(errorMessage) =>
        println(s"Send error $res")
        res
    }
    .runWith(Sink.seq)
Java
sourceimport org.apache.pekko.stream.connectors.huawei.pushkit.*;
import org.apache.pekko.stream.connectors.huawei.pushkit.javadsl.HmsPushKit;
import org.apache.pekko.stream.connectors.huawei.pushkit.models.AndroidConfig;
import org.apache.pekko.stream.connectors.huawei.pushkit.models.AndroidNotification;
import org.apache.pekko.stream.connectors.huawei.pushkit.models.BasicNotification;
import org.apache.pekko.stream.connectors.huawei.pushkit.models.ClickAction;
import org.apache.pekko.stream.connectors.huawei.pushkit.models.ErrorResponse;
import org.apache.pekko.stream.connectors.huawei.pushkit.models.PushKitNotification;
import org.apache.pekko.stream.connectors.huawei.pushkit.models.PushKitResponse;
import org.apache.pekko.stream.connectors.huawei.pushkit.models.Response;
import org.apache.pekko.stream.connectors.huawei.pushkit.models.Tokens;

CompletionStage<List<Response>> result =
    Source.single(notification)
        .via(HmsPushKit.send(config))
        .map(
            res -> {
              if (!res.isFailure()) {
                PushKitResponse response = (PushKitResponse) res;
                System.out.println("Response " + response);
              } else {
                ErrorResponse response = (ErrorResponse) res;
                System.out.println("Send error " + response);
              }
              return res;
            })
        .runWith(Sink.seq(), system);

With this type of send you can get responses from the server. These responses can be PushKitResponse or ErrorResponse. You can choose what you want to do with this information, but keep in mind if you try to resend the failed messages you will need to use exponential backoff! (see @extref[Apache Pekko docs RestartFlow.onFailuresWithBackoff)

If you don’t care if the notification was sent successfully, you may use fireAndForget.

Scala
sourceimport org.apache.pekko
import pekko.actor.ActorSystem
import pekko.stream.connectors.huawei.pushkit._
import pekko.stream.connectors.huawei.pushkit.scaladsl.HmsPushKit
import pekko.stream.connectors.huawei.pushkit.models.AndroidConfig
import pekko.stream.connectors.huawei.pushkit.models.AndroidNotification
import pekko.stream.connectors.huawei.pushkit.models.BasicNotification
import pekko.stream.connectors.huawei.pushkit.models.ClickAction
import pekko.stream.connectors.huawei.pushkit.models.Condition
import pekko.stream.connectors.huawei.pushkit.models.ErrorResponse
import pekko.stream.connectors.huawei.pushkit.models.PushKitNotification
import pekko.stream.connectors.huawei.pushkit.models.PushKitResponse
import pekko.stream.connectors.huawei.pushkit.models.Response
import pekko.stream.connectors.huawei.pushkit.models.Tokens

val config = HmsSettings()
val notification: PushKitNotification =
  PushKitNotification.empty
    .withNotification(
      BasicNotification.empty
        .withTitle("title")
        .withBody("body"))
    .withAndroidConfig(
      AndroidConfig.empty
        .withNotification(
          AndroidNotification.empty
            .withClickAction(
              ClickAction.empty
                .withType(3))))
    .withTarget(Tokens(Set[String]("token").toSeq))

Source
  .single(notification)
  .runWith(HmsPushKit.fireAndForget(config))
Java
sourceimport org.apache.pekko.stream.connectors.huawei.pushkit.*;
import org.apache.pekko.stream.connectors.huawei.pushkit.javadsl.HmsPushKit;
import org.apache.pekko.stream.connectors.huawei.pushkit.models.AndroidConfig;
import org.apache.pekko.stream.connectors.huawei.pushkit.models.AndroidNotification;
import org.apache.pekko.stream.connectors.huawei.pushkit.models.BasicNotification;
import org.apache.pekko.stream.connectors.huawei.pushkit.models.ClickAction;
import org.apache.pekko.stream.connectors.huawei.pushkit.models.ErrorResponse;
import org.apache.pekko.stream.connectors.huawei.pushkit.models.PushKitNotification;
import org.apache.pekko.stream.connectors.huawei.pushkit.models.PushKitResponse;
import org.apache.pekko.stream.connectors.huawei.pushkit.models.Response;
import org.apache.pekko.stream.connectors.huawei.pushkit.models.Tokens;

HmsSettings config = HmsSettings.create(system);
PushKitNotification notification =
    PushKitNotification.fromJava()
        .withNotification(BasicNotification.fromJava().withTitle("title").withBody("body"))
        .withAndroidConfig(
            AndroidConfig.fromJava()
                .withNotification(
                    AndroidNotification.fromJava()
                        .withClickAction(ClickAction.fromJava().withType(3))))
        .withTarget(new Tokens(new Set.Set1<>("token").toSeq()));

Source.single(notification).runWith(HmsPushKit.fireAndForget(config), system);

With fire and forget you will just send messages and ignore all the errors.

To help the integration and error handling or logging, there is a variation of the flow where you can send data beside your notification.

Scala only

You can build notification described in the original documentation. It can be done by hand, or using some builder method. See an example of the condition builder below.

Scala
sourceimport org.apache.pekko.stream.connectors.huawei.pushkit.models.Condition.{ Topic => CTopic }
val condition = Condition(CTopic("TopicA") && (CTopic("TopicB") || (CTopic("TopicC") && !CTopic("TopicD"))))