Java/Kotlin Server SDK

Installation

The Beams Java/Kotlin server SDK is available on Maven Central.

Maven

1
2
3
4
5
6
7
<dependencies>
    <dependency>
      <groupId>com.pusher</groupId>
      <artifactId>push-notifications-server-java</artifactId>
      <version>0.9.0</version>
    </dependency>
</dependencies>

Gradle

1
2
3
dependencies {
  compile 'com.pusher:push-notifications-server-java:0.9.0'
}

You can download a version of the .jar directly from Maven.

Usage

Configuring the SDK for Your Instance

Use your instance id and secret key (you can get these from the dashboard) to create a PushNotifications instance:

Java

1
2
3
4
String instanceId = "YOUR_INSTANCE_ID_HERE";
String secretKey = "YOUR_SECRET_KEY_HERE";

PushNotifications pushNotifications = new PushNotifications(instanceId, secretKey);

Kotlin

1
2
3
4
val instanceId = "YOUR_INSTANCE_ID_HERE"
val secretKey = "YOUR_SECRET_KEY_HERE"

val pn = PushNotifications(instanceId, secretKey)

Publishing a Notification

Once you have created your PushNotifications instance you can publish a push notification to your registered and subscribed devices:

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
List<String> interests = Arrays.asList("donuts", "pizza");

Map<String, Map> publishRequest = new HashMap();

Map<String, String> alert = new HashMap();
alert.put("alert", "hi");
Map<String, Map> aps = new HashMap();
aps.put("aps", alert);
publishRequest.put("apns", aps);

Map<String, String> fcmNotification = new HashMap();
fcmNotification.put("title", "hello");
fcmNotification.put("body", "Hello world");
Map<String, Map> fcm = new HashMap();
fcm.put("notification", fcmNotification);
publishRequest.put("fcm", fcm);

pushNotifications.publish(interests, publishRequest);

Kotlin

1
2
3
4
5
6
7
val interests = listOf("donuts", "pizza")
val publishRequest = hashMapOf(
  "apns" to hashMapOf("aps" to hashMapOf("alert" to "hi")),
  "fcm" to hashMapOf("notification" to hashMapOf("title" to "hello", "body" to "Hello world"))
)

pn.publish(interests, publishRequest)

Reference

Class: PushNotifications

PushNotifications(instanceId, secretKey)

Construct a new Pusher Beams Client connected to your Beams instance.

Arguments

  • instanceId(string): The unique identifier for your Push notifications instance. This can be found in the dashboard under "keys".
  • secretKey(string): The secret key your server will use to access your Beams instance. This can be found in the dashboard under "keys".

publish(interests, publishRequest)

Publish a new push notification to Pusher Beams with the given payload.

Arguments

  • interests: List of interests to send the push notification to, ranging from 1 to 100 per publish request. See Interests.
  • publishRequest: Map containing the body of the push notification publish request. See publish API reference.

Returns
String that contains publishId: See publish API reference

Did you find this document useful?

We are always striving to create the most accurate and informative docs as possible. If there is something especially wrong (or right) here then please let us know.