-
Notifications
You must be signed in to change notification settings - Fork 6
Native Subscription to Notifications using JavaSDK
(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.
In order to use the @Subscribe tag, you will need to import the Guava EventBus library (com.google.common.eventbus.Subscribe).
Subscribing to ReachabilityWatcherNotification:
...
@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!");
}
}
...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";
}
}Intro *Home
Services
- Authentication
- Boot
- Command
- Connectivity
- Data Access High-level
- Data Access Low-level
- Logging
- Notify
- OpenURL
- Secure Storage
- TouchId
- User Information
- User Settings
- Version
- Window
iOS SDK
- General
- Posted Notifications
- Remote Notifications
- Classes
- PredixMobilityConfiguration
- PredixMobilityManager
- Protocols
- PredixAppWindowProtocol
iOS Examples
Java Examples
General
[//]: (Enabling Sync Logging)