PusherHub

Docs Add PusherHub to your app

Android push notifications (Java): add the SDK step by step

Every step with a picture from a real Java app: where the SDK file goes, the Firebase lines in Gradle, the manifest and the code in MainActivity.

8 min read

All guides
1

Download PusherHub.java

In the dashboard, open Applications your app SDK Integration and pick Java ①. Click Download PusherHub.java ②. Under the button is the folder it goes in ③: your app's package with pusherhub on the end.

1 Pick Java · 2 Download the SDK file · 3 The folder it goes in

Every step below is on this tab too, with your own keys and package already filled in — copy the code from there.

2

Put the two files in your project

In Android Studio, switch the file list at the top left to Project.

  1. google-services.json ① goes in the app folder, next to the app's build.gradle.
  2. PusherHub.java ② goes in a new pusherhub folder inside your package. Right-click your package (here com.example.myapp), choose New Package , type pusherhub, then drag the file into it. Android Studio shows it as PusherHub.
  3. Open it: its first line ③ already has your own package. The file was made for your app, so don't change anything in it.
1 google-services.json in app · 2 PusherHub.java in your package's pusherhub folder · 3 Its package line
3

Add the Google services plugin to the project

Open the project's build.gradle — the one outside the app folder, shown as build.gradle (MyApp). Inside plugins { }, add ①:

id 'com.google.gms.google-services' version '4.4.3' apply false
1 The plugin line · 2 Sync Now

Android Studio offers to Sync Now ②. You can wait and sync once, after the next step.

4

Add Firebase to the app

Open the app's build.gradle, shown as build.gradle (:app).

  1. Inside plugins { }, add id 'com.google.gms.google-services' ①.
  2. Inside dependencies { }, add Firebase Cloud Messaging ②. PusherHub needs nothing else.
  3. Click Sync Now ③ and wait for it to finish.
implementation platform('com.google.firebase:firebase-bom:34.4.0')
implementation 'com.google.firebase:firebase-messaging'
1 The plugin · 2 Firebase Cloud Messaging · 3 Sync Now
5

Add permissions and the service to AndroidManifest.xml

Open app/src/main/AndroidManifest.xml and add these three things to the manifest you already have — don't replace it. The code is in step 3 on your SDK Integration tab.

  1. The permissions ①, above <application>. ACCESS_COARSE_LOCATION is only needed if you want each phone's state and city (step 6).
  2. A second <intent-filter> ② inside your MainActivity's <activity>. It lets a tap on a notification open your app and be counted. The name says Flutter, but every PusherHub notification uses it, Java apps too.
  3. The <service> ③ inside <application>. It's what receives the notifications. Its name is your package plus .pusherhub.PusherHub$PusherHubMessagingService — the $ is meant to be there.
1 Permissions · 2 The notification-tap intent filter · 3 The messaging service
6

Start PusherHub in MainActivity

Open MainActivity.java and add the code from step 4 on your SDK Integration tab — your keys and server are already filled in. (In the picture they're placeholders.)

  1. The import ①. Android Studio can add it for you: click on PusherHub and press Alt+Enter (Option+Return on a Mac).
  2. PusherHub.initialize(…) ② in onCreate, with your App key, Public key and server.
  3. PusherHub.registerDevice(…) ③ gets the phone's push token, asks the user to allow notifications and adds the phone to PusherHub. collectDeviceMetadata() inside it is optional: it adds the phone's brand, model, language and country, plus state and city if you added the location permission.
  4. PusherHub.handleIntent(getIntent()) ④ counts the tap when someone opened the app from a notification.
1 The import · 2 Your keys · 3 Register the phone · 4 Count notification taps
7

Pass taps and answers on to PusherHub

Lower down in MainActivity, add these three methods. If you already have one, just add its PusherHub line to it.

  1. onNewIntent ① — a notification tapped while the app was open in the background.
  2. onRequestPermissionsResult ② — tells PusherHub what the user answered when asked to allow notifications or location.
  3. onResume ③ — optional: shows your in-app messages each time the app is opened. Leave it out if you don't use them.
1 Taps while the app is open · 2 Permission answers · 3 In-app messages (optional)
8

Optional: log the user in

If your app has its own sign-in, tell PusherHub who's using the phone. Then you can send to one person by your own user ID. It's remembered, even after the app restarts, until logout().

// When someone signs in:
PusherHub.login(currentUserId);

// When they sign out:
PusherHub.logout();
9

Optional: open a screen when a notification is tapped

Put your own data in Custom Data when you create a notification — say {"campaign_id": "232"} — and your app gets it back whenever someone taps that notification, whether the app was open, in the background or closed. Add onNotificationOpened to the PusherHub.initialize(…) from step 6:

PusherHub.initialize(
        this,
        "YOUR_APP_KEY",
        "YOUR_PUBLIC_KEY",
        "YOUR_SERVER",
        null, // your deep-link listener, if you have one
        data -> {
            String campaignId = data.get("campaign_id");
            if (campaignId != null) {
                startActivity(new Intent(this, CampaignActivity.class).putExtra("campaign_id", campaignId));
            }
        });

It runs from handleIntent, so keep both of its calls from steps 6 and 7. Every value arrives as text ("232"). This needs PusherHub.java from 24 October 2026 or later — download it again if yours is older.

10

Run the app on a phone

Click Run ▶ to open the app on a phone or an emulator with Google Play, and tap Allow when it asks about notifications. Within a few seconds the phone shows up in PusherHub under Devices.

Then send it a test notification.