Docs

  • Channels Channels
  • Beams Beams
  • Developers
  • Support
  • Blog
  • Sign up
    • Search powered by Algolia
    • Sign in
    • Sign up
    • Channels
    • Beams
    • Getting started
      • Android
        • 1. Configure FCM
        • 2. Integrate SDK
        • 3. Initialize Beams
        • 4. Publish Notifications
      • iOS
        • 1. Configure APNS
        • 2. Integrate SDK
        • 3. Publish Notifications
      • Web
        • 1. SDK integration
        • 2. Safari configuration
      • Flutter
        • 1. Configure FCM and APNS
        • 2. Integrate SDK
        • 4. Publish Notifications
    • Concepts
      • Subscribers
      • Device interests
      • Authenticated users
      • Insights
      • Webhooks
    • Guides
      • Handle incoming notifications
        • Android
        • iOS
        • Web
        • Flutter
      • Publishing to multiple devices
      • Publish to specific user
        • Android
        • iOS
        • Web
        • Flutter
      • Web push guides
        • Using an existing service worker
        • Web notification permissions in Firefox
        • Handling Safari certificate expiration
    • Reference
      • Client SDKs
        • Android
        • iOS
        • Web
      • All Libraries
      • Server SDKs
        • Go
        • PHP
        • Node.js
        • Python
        • Java/Kotlin
        • Ruby
        • Swift
      • API
        • Publish API
        • Customer API
        • Device API
        • Reporting API
        • Webhooks
      • Platform Publish Formats
    • Pusher lab

    Java/Kotlin Server SDK

    ∞ Installation

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

    <dependencies>
    <dependency>
    <groupId>com.pusher</groupId>
    <artifactId>push-notifications-server-java</artifactId>
    <version>1.1.1</version>
    </dependency>
    </dependencies>
    dependencies {
    compile 'com.pusher:push-notifications-server-java:1.1.1'
    }

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

    ∞ Reference

    ∞ Class: PushNotifications

    PushNotifications(instanceId, secretKey)

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

    ∞ Arguments

    ∞ instanceIdString Required

    The unique identifier for your Push notifications instance. This can be found in the dashboard under “Credentials”.

    ∞ secretKeyString Required

    The secret key your server will use to access your Beams instance. This can be found in the dashboard under “Credentials”.

    ∞ Example

    String instanceId = "YOUR_INSTANCE_ID_HERE";
    String secretKey = "YOUR_SECRET_KEY_HERE";

    PushNotifications beamsClient = new PushNotifications(instanceId, secretKey);
    val instanceId = "YOUR_INSTANCE_ID_HERE"
    val secretKey = "YOUR_SECRET_KEY_HERE"

    val beamsClient = PushNotifications(instanceId, secretKey)

    ∞ .publishToInterests

    Sends broadcast notifications to groups of subscribed devices using Device Interests

    ∞ Arguments

    ∞ interestsList<String> Required

    List of interests to send the push notification to, ranging from 1 to 100 per publish request. See Interests.

    ∞ publishRequestMap<String, Map> Required

    Map containing the body of the push notification publish request. See publish API reference.

    ∞ Returns

    String that contains publishId: See publish API reference

    ∞ Example

    List<String> interests = Arrays.asList("donuts", "pizza");

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

    Map<String, String> apsAlert = new HashMap();
    apsAlert.put("title", "hello");
    apsAlert.put("body", "Hello world");
    Map<String, Map> alert = new HashMap();
    alert.put("alert", apsAlert);
    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);

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

    beamsClient.publishToInterests(interests, publishRequest);
    val interests = listOf("donuts", "pizza")
    val publishRequest = hashMapOf(
    "apns" to hashMapOf("aps" to hashMapOf("alert" to hashMapOf("title" to "hello", "body" to "Hello world"))),
    "fcm" to hashMapOf("notification" to hashMapOf("title" to "hello", "body" to "Hello world")),
    "web" to hashMapOf("notification" to hashMapOf("title" to "hello", "body" to "Hello world"))
    )

    beamsClient.publishToInterests(interests, publishRequest)

    ∞ .publishToUsers

    Securely send notifications to individual users of your application using Authenticated Users

    ∞ Arguments

    ∞ userIdsList<String> Min length=1, Max length=1000 Required

    List of ids of users to send the push notification to, ranging from 1 to 1000 per publish request. See Authenticated Users

    ∞ publishRequestMap<String, Map> Required

    Map containing the body of the push notification publish request. See publish API reference.

    ∞ Returns

    String that contains publishId: See publish API reference

    ∞ Example

    List<String> users = Arrays.asList("user-001", "user-002");

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

    Map<String, String> apsAlert = new Hashmap();
    apsAlert.put("title", "hello");
    apsAlert.put("body", "Hello world");
    Map<String, Map> alert = new HashMap();
    alert.put("alert", apsAlert);
    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);

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

    beamsClient.publishToUsers(users, publishRequest);
    val users = listOf("user-001", "user-002")
    val publishRequest = hashMapOf(
    "apns" to hashMapOf("aps" to hashMapOf("alert" to "hashMapOf("title" to "hello", "body" to "Hello world"))),
    "fcm" to hashMapOf("notification" to hashMapOf("title" to "hello", "body" to "Hello world")),
    "web" to hashMapOf("notification" to hashMapOf("title" to "hello", "body" to "Hello world"))
    )

    beamsClient.publishToUsers(users, publishRequest)

    ∞ .generateToken

    Generate a Beams auth token to allow a user to associate their device with their user id. The token is valid for 24 hours.

    ∞ Arguments

    ∞ userIDString Required

    Id of the user you would like to generate a Beams auth token for.

    ∞ Returns

    ∞ beamsTokenMap<String, Any>

    Beams Token for the given user

    ∞ Example

    String userId = "user-001";
    Map<String, Object> token = beamsClient.generateToken(userId);
    val userId = "user-001"
    val token = beamsClient.generateToken(userId)

    Contents

    • Installation
    • Reference
      • Class: PushNotifications
      • .publishToInterests
      • .publishToUsers
      • .generateToken

    Spotted something that isn’t quite right? Create an issue on GitHub.

    Copyright © 2024 Pusher Ltd. All rights reserved.

    • Support,
    • Status
    • Follow Pusher on Twitter Twitter
    • Subscribe to Pusher’s channel on YouTube
    • Follow Pusher on LinkedIn
    • Follow Pusher on Github GitHub
    • Follow Pusher on Twitch Twitch
    • Follow Pusher on Discord Discord