Skip to content

Native Subscription to Notifications using JavaSDK

Omer Elhiraika edited this page Jul 19, 2017 · 12 revisions

(For a list of available notifications that you can subscribe to, click here)

In addition to subscribing to notifications using Javascript via the (Notify Service), you can write native Java code within the Android/Desktop container apps and subscribe to standard or custom notifications.

Examples

In order to use the @Subscribe tag, you will need to import the Guava EventBus library (com.google.common.eventbus.Subscribe).

    ...
    @Subscribe
    public void listenForReachabilityChanges(ReachabilityWatcherNotification reachabilityNotification) {
        Set<ReachabilityStatus> notification = reachabilityNotification.getPayload();
        if (notification.contains(ReachabilityStatus.Authenticated) && notification.contains(ReachabilityStatus.Online)) {
            PredixSDKLogger.debug("We are now authenticated and online!");
        }
    }
    ...

Creating and subscribing to custom notifications:

You can subscribe to custom notifications(extending NotificationBase) natively or by using NotifyService. For example, you can subscribe to notification name, "TemperatureChangedNotification", to receive all temperature change notifications as shown in the example below:

public class TemperatureApp
{
    private int temperature = 72;

    // Calling setTemperature will notify users about temperature changes (posting a notification)
    public void setTemperature(int currentTemperature)
    {
        temperature = currentTemperature;
        NotificationBus.defaultInstance().postNotification(new TemperatureChangedNotification());
    }
}

class TemperatureAppUser
{
    private static int hot = 90;
    private static int cold = 32;

    // Register our listeners (annotated with @Subcribe) to notifications
    public void startListening() {
        PlatformImpl.instance.getNotificationRegistrar().registerListener(this);
    }

    // Unregister listeners in this class from notifications
    public void stopListening() {
        PlatformImpl.instance.getNotificationRegistrar().unRegisterListener(this);
    }

    // As a user of the temperature app (subscriber to temperature notifications), the user
    // will determine how to go about their day
    @Subscribe
    public void temperatureChangedNotificationListener(TemperatureChangedNotification notification)
    {
        int newTemperature = notification.getPayload();
        if (newTemperature >= hot)
        {
            PredixSDKLogger.info("Looks like it's pretty hot today!");
        }
        else if (newTemperature <= cold)
        {
            PredixSDKLogger.info("I shouldn't forget my heavy jacket today");
        }
    }
}

// This is our custom notification which extends com.ge.predix.mobile.platform.NotificationBase
class TemperatureChangedNotification extends NotificationBase<Integer>
{
    public TemperatureChangedNotification(Integer temperature)
    {
        super(temperature);
    }

    @Override
    public String getName()
    {
        return "TemperatureChangedNotification";
    }
}

Clone this wiki locally