Java (Desktop) Setup
Send push notifications to your Java desktop app with Pushy.
At any time, you may refer to our Java demo project for a working demo. Also, please make sure to follow our Android guide instead if you're developing for Android.
Create an App
Sign up for Pushy Trial to get started, or log in to your existing account
Note: No credit card needed. Upgrade to the Pro plan when you're ready by visiting the Billing page.
Create an app and fill in the Web Push Hostname with your company domain name:
Note: If you have already created an app in the Pushy Dashboard for another platform, simply configure your existing dashboard app with your Web Push Hostname with your company domain name in the App Settings tab and proceed to the next step.
If you're targeting other platforms, please fill in their respective app identifier fields.
Click Create and proceed to the next step.
Get the SDK
Install version 1.0.0
of the Pushy Java SDK by adding the following Maven dependency to your IntelliJ IDEA Java project:
me.pushy:sdk-java:1.0.0
With your Java project open in IntelliJ IDEA, go to File -> Project Structure -> Libraries -> + (Add) -> From Maven -> copy & paste the above string. Click OK and OK again to add the library to the selected module(s), and OK to apply the changes.
Alternatively, you may download sdk-1.0.0.jar and include it manually as a Java library.
Modify Main Class
Invoke the following methods in your main()
method to initialize the SDK:
// Set Pushy App ID
Pushy.setAppId("YOUR_APP_ID");
// Listen for notifications
Pushy.listen();
Note: Please make sure to replace YOUR_APP_ID
with your Pushy App ID in the Pushy Dashboard -> Click your app -> App Settings -> App ID.
Register Devices
Users need to be uniquely identified to receive push notifications.
Every user is assigned a unique device token that you can use to push it at any given time. Once the user has been assigned a device token, it should be stored in your application's backend database.
Add the following code to register the user for push notifications:
// Not registered yet?
if (!Pushy.isRegistered()) {
try {
// Register for push notifications & obtain device token
String deviceToken = Pushy.register();
// Display device token in alert dialog
JOptionPane.showMessageDialog(null, "Pushy registration success: " + deviceToken);
} catch (PushyException e) {
// Display registration errors
JOptionPane.showMessageDialog(null, "Pushy registration failed: " + e.getMessage());
}
}
Listen for Notifications
Invoke the Pushy.setNotificationListener()
method to listen for and process incoming push notifications:
// Handle incoming push notifications
Pushy.setNotificationListener(new PushyNotificationListener() {
@Override
public void onNotificationReceived(Map<String, Object> data) {
// Display an alert dialog with notification payload "message" value
JOptionPane.showMessageDialog(null, "Notification received: " + data.get("message"));
}
});
Feel free to modify this sample code to suit your own needs.
Parse Notification Data
Any payload data that you send with your push notifications is made available to your app via the data
parameter of your notification listener.
If you were to send a push notification with the following payload:
{"id": 1, "success": true, "message": "Hello World"}
Then you'd be able to retrieve each value from within your notification listener callback like so:
int id = (int) data.get("int"); // number
boolean success = (boolean) data.get("success"); // bool
String message = (String) data.get("message"); // string
Note: Unlike GCM / FCM, we do not stringify your payload data, except if you supply JSON objects or arrays.
Subscribe to Topics
Optionally subscribe the user to one or more topics to target multiple users with a shared interest when sending notifications.
Depending on your app's notification criteria, you may be able to leverage topics to simply the process of sending the same notification to multiple users. If your app only sends personalized notifications, skip this step and simply target individual users by their unique device tokens.
Add the following code to your app to subscribe the user to a topic:
// Make sure the user is registered for notifications
if (Pushy.isRegistered()) {
try {
// Subscribe the user to a topic
Pushy.subscribe("news");
} catch (PushyException e) {
// Display error dialog
JOptionPane.showMessageDialog(null, "Pushy subscribe failed: " + e.getMessage());
}
}
Note: Replace news
with your own case-sensitive topic name that matches the following regular expression: [a-zA-Z0-9-_.]{1,100}
.
You can then notify multiple users subscribed to a certain topic by specifying the topic name (prefixed with /topics/
) as the to
parameter in the Send Notifications API.
Send Test Notification
Input your device token and select your app to send a test push notification:
Note: You can specify a topic instead of a device token (i.e. /topics/news
). Also, if your app is not automatically detected, please manually copy the Secret API Key from the Dashboard and paste it into the form.
Did you receive the notification? If not, reach out, we'll be glad to help.
Congratulations on implementing Pushy in your Java app!
To start sending push notifications to your users, start persisting device tokens in your backend, and invoke the Send Notifications API when you want to send a notification. Follow our step-by-step guide: