Interface PushNotificationsService

All Known Implementing Classes:
AndroidPushNotificationsService, IOSPushNotificationsService

public interface PushNotificationsService
Adds the ability for the application to receive remote push notifications.

Example

 PushNotificationsService.create().ifPresent(service -> {
       service.tokenProperty.addListener((observable, oldValue, newValue) -> {
           if (newValue != null) {
               String deviceToken = newValue;
               // This deviceToken can be used to send push notifications to this device by calling
               // the appropriate server API for the platform. Usually, this token is sent to a server
               // where the push notifications are created and sent out.
           }
       });
       service.register();
   });

Requirements

The service requires the following changes on Android and iOS.

However, these are handled automatically by the GluonFX plugin, when used.

Android Configuration

To enable push notifications on android, an existing Google Firebase project is required. Copy the google-services.json file into your project's src/android/resources folder.

The following permissions, services and receiver need to be added to the android manifest configuration file to make push notifications work on android. The main activity also requires the attribute android:launchMode with value singleTop.

 <manifest ...>
    ...
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    ...
    <application ...>
      <activity android:name="com.gluonhq.helloandroid.MainActivity"
                android:configChanges="orientation|keyboardHidden"
                android:launchMode="singleTop">
        ...
      </activity>
      ...
      <service android:name="com.gluonhq.helloandroid.PushFcmMessagingService">
        <intent-filter>
          <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
      </service>
      <service android:name="com.gluonhq.helloandroid.PushNotificationJobService"
               android:permission="android.permission.BIND_JOB_SERVICE"
               android:exported="true" />
      <activity android:name="com.gluonhq.helloandroid.PushNotificationActivity"
                android:parentActivityName="com.gluonhq.helloandroid.MainActivity">
            <meta-data android:name="android.support.PARENT_ACTIVITY"
                       android:value="com.gluonhq.helloandroid.MainActivity"/>
      </activity>
    </application>
  </manifest>

iOS Configuration

You need to use a Provisioning Profile that defines the aps-environment property in the Entitlements section to either development or production.

Since:
3.2.0
  • Property Summary

    Properties
    Type
    Property
    Description
    javafx.beans.property.ReadOnlyStringProperty
    The unique registration token that can be passed to the server, so the app can receive push notifications
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns an instance of PushNotificationsService.
    void
    Register the app for receiving push notifications.
    void
    register(String authorizedEntity)
    Deprecated. 
    javafx.beans.property.ReadOnlyStringProperty
    The unique registration token that can be passed to the server, so the app can receive push notifications
  • Property Details

    • token

      javafx.beans.property.ReadOnlyStringProperty tokenProperty
      The unique registration token that can be passed to the server, so the app can receive push notifications
  • Method Details

    • create

      Returns an instance of PushNotificationsService.
      Returns:
      An instance of PushNotificationsService.
    • tokenProperty

      javafx.beans.property.ReadOnlyStringProperty tokenProperty()
      The unique registration token that can be passed to the server, so the app can receive push notifications
    • register

      void register()
      Register the app for receiving push notifications. On iOS this will trigger a confirmation dialog that must be accepted by the user.
      Since:
      4.0.10
    • register

      @Deprecated void register(String authorizedEntity)
      Deprecated.
      Register the app for receiving push notifications. On iOS this will trigger a confirmation dialog that must be accepted by the user. For Android, you need to pass in the authorizedEntity value that matches the Sender ID of your Google Cloud Messaging or Firebase Cloud Messaging application.
      Parameters:
      authorizedEntity - a string that matches the Sender ID of a GCM or FCM application