Notifications

Index

Type aliases

CustomData

CustomData: object

Application-defined context data that can be attached to notifications.

Type declaration

  • [key: string]: any

Notification

Notification: Readonly<Required<NotificationOptions>>

A fully-hydrated form of NotificationOptions.

After creating a notification, the service will return an object of this type. This will be the given options object, with any unspecified fields filled-in with default values.

This object should be treated as immutable. Modifying its state will not have any effect on the notification or the state of the service.

Events

NotificationActionEvent

NotificationActionEvent:

Event fired for interactions with notification UI elements. It is important to note that applications will only receive these events if they indicate to the service that they want to receive these events. See Actions for a full example of how actions are defined, and how an application can listen to and handle them.

This can be fired due to interaction with notification buttons, or the notification itself. Later versions of the service will add additional control types. All actions, for all control types, will be returned to the application via the same notification-action event type.

The event object will contain the application-defined metadata that allowed this action to be raised, and details on what triggered this action and which control the user interacted with.

This type includes a generic type argument, should applications wish to define their own interface for action results. See NotificationActionResult for details.

Optional control

control: Readonly<ControlOptions>

The control whose interaction resulted in this action being raised. Will only be present when trigger is ActionTrigger.CONTROL.

Future versions of the service will add additional controls beyond buttons, and interactions with these new control types will also come through this one event type. For best forward-compatibility, applications should always check the type property of this control, and not assume that the type will always be 'button'.

This field is marked optional as future versions of the service will also include alternate methods of raising notification-action events that do not originate from a button or other control.

When present, the object here will always be strictly equal to one of the control definitions within notification. This means indexOf checks and other equality checks can be performed on this field if required, such as:

function onNotificationAction(event: NotificationActionEvent): void {
    if (event.control && event.control.type === 'button') {
        const butttonIndex = event.notification.buttons.indexOf(event.control);

        // Handle button click
        // . . .
    }
}

notification

notification: Readonly<Notification>

The notification that created this action

result

Application-defined metadata that this event is passing back to the application.

A notification-action event is only fired for an interaction with a notification if the notification options included an action result for that interaction.

See the comment on the NotificationActionEvent type for an example of buttons that do and don't raise actions.

trigger

trigger: ActionTrigger

Indicates what triggered this action.

Note that the programmatic trigger is not yet implemented.

type

type: "notification-action"

NotificationClosedEvent

NotificationClosedEvent:

Event fired whenever the notification has been closed.

This event is fired regardless of how the notification was closed - i.e.: via a call to clear/clearAll, or by a user clicking either the notification itself, the notification's close button, or a button on the notification.

notification

notification: Notification

The notification that has just been closed.

This object will match what is returned from the create call when the notification was first created.

type

type: "notification-closed"

NotificationCreatedEvent

NotificationCreatedEvent:

Event fired whenever a new notification has been created.

notification

notification: Notification

The notification that has just been created.

This object will match what is returned from the create call.

type

type: "notification-created"

Functions

addEventListener

  • addEventListener(eventType: "notification-action", listener: function): void
  • addEventListener(eventType: "notification-created", listener: function): void
  • addEventListener(eventType: "notification-closed", listener: function): void
  • Adds a listener, see definitions of individual event interfaces for details on each event.

    Parameters

    Returns void

  • Adds a listener, see definitions of individual event interfaces for details on each event.

    Parameters

    Returns void

  • Adds a listener, see definitions of individual event interfaces for details on each event.

    Parameters

    Returns void

clear

  • clear(id: string): Promise<boolean>
  • Clears a specific notification from the Notification Center.

    Returns true if the notification was successfully cleared. Returns false if the notification was not cleared, without errors.

    import {clear} from 'openfin-notifications';
    
    clear('uniqueNotificationId');

    Parameters

    • id: string

      ID of the notification to clear.

    Returns Promise<boolean>

clearAll

  • clearAll(): Promise<number>
  • Clears all Notifications which were created by the calling application, including child windows.

    Returns the number of successfully cleared Notifications.

    import {clearAll} from 'openfin-notifications';
    
    clearAll();

    Returns Promise<number>

create

  • Creates a new notification.

    The notification will appear in the Notification Center and as a toast if the Center is not visible.

    If a notification is created with an id of an already existing notification, the existing notification will be recreated with the new content.

    import {create} from 'openfin-notifications';
    
    create({
         id: 'uniqueNotificationId',
         title: 'Notification Title',
         body: 'Text to display within the notification body',
         category: 'Sample Notifications',
         icon: 'https://openfin.co/favicon.ico'
    });

    Parameters

    Returns Promise<Notification>

getAll

  • Retrieves all Notifications which were created by the calling application, including child windows.

    import {getAll} from 'openfin-notifications'
    
    getAll().then((notifications: Notification[]) => {
        console.log(`Service has ${notifications.length} notifications for this app:`, notifications);
    });

    There is deliberately no mechanism provided for fetching notifications that were created by a different application.

    Returns Promise<Notification[]>

removeEventListener

  • removeEventListener(eventType: "notification-action", listener: function): void
  • removeEventListener(eventType: "notification-created", listener: function): void
  • removeEventListener(eventType: "notification-closed", listener: function): void
  • Removes a listener previously added with addEventListener.

    Has no effect if eventType isn't a valid event, or listener isn't a callback registered against eventType.

    Parameters

    • eventType: "notification-action"

      The event being unsubscribed from

    • listener: function

      The callback function to remove, must be strictly-equal (=== equivilance) to a listener previously passed to addEventListener to have an effect

    Returns void

  • Removes a listener previously added with addEventListener.

    Has no effect if eventType isn't a valid event, or listener isn't a callback registered against eventType.

    Parameters

    • eventType: "notification-created"

      The event being unsubscribed from

    • listener: function

      The callback function to remove, must be strictly-equal (=== equivilance) to a listener previously passed to addEventListener to have an effect

    Returns void

  • Removes a listener previously added with addEventListener.

    Has no effect if eventType isn't a valid event, or listener isn't a callback registered against eventType.

    Parameters

    • eventType: "notification-closed"

      The event being unsubscribed from

    • listener: function

      The callback function to remove, must be strictly-equal (=== equivilance) to a listener previously passed to addEventListener to have an effect

    Returns void

toggleNotificationCenter

  • toggleNotificationCenter(): Promise<void>
  • Toggles the visibility of the Notification Center.

    import {toggleNotificationCenter} from 'openfin-notifications';
    
    toggleNotificationCenter();

    Returns Promise<void>