Java Client SDK

Quickstart

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
 
 
import com.google.firebase.messaging.RemoteMessage;
import com.pusher.pushnotifications.PushNotifications;
import com.pusher.pushnotifications.PushNotificationReceivedListener;
 
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PushNotifications.start(getApplicationContext(), "YOUR_INSTANCE_ID");
        PushNotifications.subscribe("hello");
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        PushNotifications.setOnMessageReceivedListenerForVisibleActivity(this, new PushNotificationReceivedListener() {
            @Override
            public void onMessageReceived(RemoteMessage remoteMessage) {
                Log.i("MainActivity", "A remote message was received while this activity is visible!");
            }
        });
    }
}

Start Beams

There are 2 options to start Beams: using the PushNotificationsInstance constructor:

1
2
3
4
PushNotificationsInstance pushNotifications =
    new PushNotificationsInstance(getApplicationContext(), "YOUR_INSTANCE_ID");

pushNotifications.start();

Or by using the PushNotifications singleton.

1
PushNotifications.start(getApplicationContext(), "YOUR_INSTANCE_ID");

Both will start the Beams client and synchronize the FCM device token with the Pusher service.

Handle Incoming Notifications

The Beams SDK makes it possible trigger callback code in your application when a push notification arrives on a user's device. This can be used to respond to the arrival of the notification in your application. For example, if you send a data payload with your push notification you can access the content in the payload from this callback code.

You can implement a service to handle incoming notifications. It will get called when your application is in foreground or when it contains a data payload while your application is in the background.

When your application is in the background and the notification doesn't contain any data in the payload, this service will not receive it.

Android Manifest

<manifest>
    <application>
        ...
        <service android:name=".NotificationsMessagingService">
            <intent-filter android:priority="1">
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
    </application>
</manifest>

NotificationsMessagingService.java

1
2
3
4
5
6
7
8
9
10
11
import android.util.Log;

import com.google.firebase.messaging.RemoteMessage;
import com.pusher.pushnotifications.fcm.MessagingService;

public class NotificationsMessagingService extends MessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.i("NotificationsService", "Got a remote message 🎉");
    }
}

Inside an Activity

When a push notification arrives while your activity is in display, the Beams SDK will call the onMessageReceived listener with a RemoteMessage object containing the notification and data payload.

PHP Server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$publishResponse = $pushNotifications->publish(
  array("hello"),
  array(
    "fcm" => array(
      "notification" => array(
        "title" => "Hello",
        "body" => "Hello, World!",
      ),
      // This data can be used in your activity when a notification arrives
      // with your app in the foreground.
      "data" => array(
        "inAppNotificationMessage" => "Display me somewhere in the app ui!",
      ),
    ),
  ),
);

Android Activity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Override
protected void onResume() {
    super.onResume();
    PushNotifications.setOnMessageReceivedListenerForVisibleActivity(this, new PushNotificationReceivedListener() {
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            String messagePayload = remoteMessage.getData().get("inAppNotificationMessage");
            if (messagePayload == null) {
                // Message payload was not set for this notification
                Log.i("MyActivity", "Payload was missing");
            } else {
                Log.i("MyActivity", messagePayload);
                // Now update the UI based on your message payload!
            }
        }
    });
}

When a User Taps the Notification

When the user taps on a notification, the intent in which your activity is launched with has the same extras as your notification data payload.

PHP Server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$pushNotifications = new \Pusher\PushNotifications\PushNotifications(array(
  "instanceId" => "YOUR_INSTANCE_ID_HERE",
  "secretKey" => "YOUR_SECRET_KEY_HERE",
));
$publishResponse = $pushNotifications->publish(
  array("hello"),
  array(
    "fcm" => array(
      "notification" => array(
        "title" => "Hello",
        "body" => "Hello, World!",
      ),
      // This data can be used in your activity when a user taps the notification
      // and launches your app.
      "data" => array(
        "appLaunchMessage" => "You just launched our app!",
      ),
    ),
  ),
);

Android Activity

1
2
3
4
5
6
7
8
9
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String messagePayload = getIntent().getStringExtra("appLaunchMessage");
    if (messagePayload != null) {
      // This means that the onCreate is the result of a notification being opened
      Log.i("MyActivity", messagePayload);
    }
}

Reference

subscribe

Subscribes the device to an Interest. For example:

PushNotifications.subscribe("hello");

unsubscribe

Subscribes the device to an Interest. For example:

PushNotifications.unsubscribe("hello");

unsubscribeAll

Unsubscribes the device from all the Interests. For example:

PushNotifications.unsubscribeAll();

setSubscriptions

Sets the subscriptions state for the device. Any Interests not in the set will be unsubscribed from, i.e. this will replace the Interest set by the one provided.

  • Java
  • Kotlin
PushNotifications.setSubscriptions(Arrays.asList("hello", "donuts").toSet());

getSubscriptions

Returns the set of subscriptions that the device is currently subscribed to.

PushNotifications.getSubscriptions();

setOnSubscriptionsChangedListener

Configures a listener that handles a change in Interest subscriptions.
Any time Interest subscriptions change, the onSubscriptionsChangedcallback will be fired. Useful if you want to reflect a change in subscriptions in your UI.

  • Java
  • Kotlin
PushNotifications.setOnSubscriptionsChangedListener(new SubscriptionsChangedListener() {
  @Override
  public void onSubscriptionsChanged(Set<String> interests) {
    // do something magical 🔮
  }
});

setOnMessageReceivedListenerForVisibleActivity

Configures the listener that handles a remote message only when this activity is in the foreground. Use this method to update your UI. This should be called from the Activity.onResume method.

  • Java
  • Kotlin
PushNotifications.setOnMessageReceivedListenerForVisibleActivity(this, new PushNotificationReceivedListener() {
  @Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
      // do something magical 🔮
  }
});

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.