Interface LocalNotificationsService

  • All Known Implementing Classes:
    AndroidLocalNotificationsService, DummyLocalNotificationsService, IOSLocalNotificationsService, LocalNotificationsServiceBase

    public interface LocalNotificationsService
    Used to schedule a native notification.

    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 ...>
        ...
        <activity android:name='com.gluonhq.helloandroid.MainActivity'
                       android:configChanges="orientation|keyboardHidden"
                       android:launchMode="singleTop">
                 <intent-filter>
                     <category android:name='android.intent.category.LAUNCHER'/>
                     <action android:name='android.intent.action.MAIN'/>
                 </intent-filter>
             </activity>
             <activity android:name="com.gluonhq.helloandroid.NotificationActivity"
                 android:parentActivityName="com.gluonhq.helloandroid.MainActivity">
                 <meta-data android:name="android.support.PARENT_ACTIVITY"
                     android:value="com.gluonhq.helloandroid.MainActivity"/>
             </activity>
             <receiver android:name="com.gluonhq.helloandroid.AlarmReceiver" />
      </manifest>

    iOS Configuration: none

    Since:
    3.0.0
    See Also:
    Notification