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 addAll(Notification...)
or
List.addAll(java.util.Collection)
every time the application is opened. Using add(Notification)
is not advisable in case of having more than one notification.
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.
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
.
<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"/>
...
</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: none
Notification
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
|
javafx.collections.ObservableList<Notification> getNotifications()