public interface LocalNotificationsService
In the simple scenario where the same application instance is running when the native notification fires (and
is subsequently clicked on), the app will resume (if it was in the background), and the notification
Runnable
will be executed.
By and large notifications 'just work', but there is one scenario to be aware of. This is the situation where a native notification fires when the application is either completely closed (i.e. not even running 'in the background'), or when the application that created the notifications is not the same instance as the one receiving the notifications.
In these cases, it is important to remember that the application has the responsibility of restoring
notifications at startup. Typically the developer will have to load upon startup all the notifications that
were created on the first place, call
getNotifications()
to get access to the observable list of notifications and
call add(Notification)
or
List.addAll(java.util.Collection)
every time the application is opened.
Doing this on every startup does not have the effect of 'duplicating' the
same notifications - provided the ID
of the notification remains constant
between runs, the native platform will deliver it only once.
But the developer has the possibility to remove the notification, once it has been delivered, by avoiding registering it all over again once the scheduled time is in the past. Note that any notification scheduled for a past time will be fired immediately. Note as well that if either the notification's scheduled date or its text are null or empty, the notification won't be scheduled on the device.
In cases where the application was completely closed when the notification is fired and clicked on, the local
notification service on the device will still launch the application. In this case, it will add as runtime parameter
the notification id, e.g. --notificationId=<id>
.
The developer can get the map with runtime parameters in the
Application.init()
method by calling Application.getParameters()
to
find the notification id. Then the method processNotification(String)
can be called to process
the notification runnable as soon as possible.
Example
String notificationId = "abcd1234";
Services.get(LocalNotificationsService.class).ifPresent(service -> {
service.getNotifications().add(new Notification(notificationId, "Sample Notification Text",
ZonedDateTime.now().plusSeconds(20), () -> {
Alert alert = new Alert(AlertType.INFORMATION, "You have been notified!");
Platform.runLater(() -> alert.showAndWait());
}));
});
Android Configuration
The following activity
and receiver
need to be added to the android manifest
configuration file to make local notifications work on android. The main activity also requires the attribute
android:launchMode
with value singleTop
and an extra meta-data
element with name java.args
and value notificationId
.
<manifest ...>
...
<application ...>
<activity android:name="javafxports.android.FXActivity"
android:label="SampleGluonApp"
android:launchMode="singleTop"
android:configChanges="orientation|screenSize">
<meta-data android:name="main.class" android:value="com.gluonhq.sample.SampleGluonApp"/>
<meta-data android:name="java.args" android:value="notificationId"/>
...
</activity>
...
<activity android:name="com.gluonhq.impl.charm.down.plugins.android.NotificationActivity"
android:parentActivityName="javafxports.android.FXActivity">
<meta-data android:name="android.support.PARENT_ACTIVITY" android:value="javafxports.android.FXActivity"/>
</activity>
<receiver android:name="com.gluonhq.impl.charm.down.plugins.android.AlarmReceiver" />
</application>
</manifest>
iOS Configuration
To make local notifications work on iOS, you will need to create a custom launcher based on
the basic launcher class, that you can copy from
https://bitbucket.org/javafxports/javafxmobile-plugin/src/tip/src/main/resources/ios/sources/BasicLauncher.java.
Copy the file in a package under the ios sources located in the folder src/ios/java
. Then add the
following code inside the run
method of the launchThread
instance in the
didFinishLaunching
method that is defined at the top:
...
public boolean didFinishLaunching(UIApplication application, UIApplicationLaunchOptions launchOptions)
{
Thread launchThread = new Thread()
{
@Override
public void run() { String[] args = new String[] {}; if (launchOptions != null && launchOptions.getLocalNotification() != null) { final NSDictionary userInfo = launchOptions.getLocalNotification().getUserInfo(); if (userInfo.containsKey("userId")) { String notificationId = "--" + LocalNotificationsService.NOTIFICATION_KEY + "=" + userInfo.getString("userId"); args = new String[]{ notificationId }; } } ... }
};
launchThread.setDaemon(true); launchThread.start(); return true;
}
...
Finally, in the build.gradle
file located at the root of your project, add the
launcherClassName
property to specify that your custom launcher should be used instead of the
default one:
jfxmobile {
ios {
launcherClassName = 'com.gluonhq.sample.CustomLauncher'
}
}
Notification
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
NOTIFICATION_KEY
Key used to pass the notification id value as runtime parameter
when launching the application from a notification
Important Note: The Android implementation requires this key to be
added to the AndroidManifest file:
<meta-data android:name="java.args" android:value="notificationId"/>
Important Note: Any iOS application that wants to use this service must add
a Custom Launcher class, and include this in the build.gradle script for jfxmobile - ios:
launcherClassName = 'path.to.CustomLauncher' |
Modifier and Type | Method and Description |
---|---|
javafx.collections.ObservableList<Notification> |
getNotifications()
An Observable List of Notifications, that can be used to
add or remove notifications
If the notification is marked as scheduled, it won't be
added to the device
|
void |
processNotification(java.lang.String id)
Processes a notification by its id.
|
static final java.lang.String NOTIFICATION_KEY
<meta-data android:name="java.args" android:value="notificationId"/>
Important Note: Any iOS application that wants to use this service must add
a Custom Launcher class, and include this in the build.gradle script for jfxmobile - ios:
launcherClassName = 'path.to.CustomLauncher'
javafx.collections.ObservableList<Notification> getNotifications()
void processNotification(java.lang.String id)
id
- the id of the notification