WNotification

A WNotification is an object that represents a notification that can be sent to the user.

Asking Permission

In order for a notification to be displayed to the user, you first need to have it's permission. JWt will automatically ask the permission the first time you send a notification to a user, but you can also do it manually. You can react to the user accepting or refusing to give you the permission by listening to the WNotification.permissionUpdated() signal.

Tips: You can reset your notification settings for the site in your browser settings.

Example
source
  void AskNotificationPermission() {
    WContainerWidget container = new WContainerWidget();
    final WPushButton button =
        new WPushButton("Get asked for notification", (WContainerWidget) container);
    final WText text = new WText((WContainerWidget) container);
    button
        .clicked()
        .addListener(
            this,
            () -> {
              WNotification.askPermission();
              button.setEnabled(false);
            });
    WNotification.permissionUpdated()
        .addListener(
            this,
            (WNotification.Permission permission) -> {
              if (permission == WNotification.Permission.Granted) {
                text.setText("You have allowed us to send you notifications.");
              } else {
                text.setText("You have not allowed us to send you notifications.");
              }
            });
  }

Sending Notifications

You can send a notification by using its send() method. If you try to send a notification without having permission from the user to send notifications, the notifications will wait for the user to give permission. Once the user gives permission, all still relevant notification that are waiting will be sent. A notification is relevant if it was not closed or destroyed.

Example
source
  void NotificationSend() {
    WContainerWidget container = new WContainerWidget();
    final WNotification p = new WNotification("You will see me everytime");
    p.setSilent();
    WPushButton button = new WPushButton("Receive notifications", (WContainerWidget) container);
    button
        .clicked()
        .addListener(
            this,
            () -> {
              p.send();
              final WNotification oneUse =
                  new WNotification("You will only see me if you already accepted notifications.");
              oneUse.setSilent();
              oneUse.send();
            });
  }

As long as the a notification was not closed, sending it again will only update it. This can be used to avoid spamming the user with many notifications while only the last one needs to be kept. For instance, in a chat application, if several new messages are sent in the same channel, it is better to have no more than 1 notification for that channel which says how many new messages arrived, rather than having one notification per new message.

Example
source
  class Channel extends WContainerWidget {
    private static Logger logger = LoggerFactory.getLogger(Channel.class);

    Channel(WContainerWidget parentContainer) {
      super();
      this.newMsg_ = 0;
      this.notif_ = new WNotification();
      this.notif_.setIcon(new WLink("icons/jwt.png"));
      this.notif_.setSilent();
      this.notif_
          .clicked()
          .addListener(
              this,
              () -> {
                Channel.this.onNotifClick();
              });
      if (parentContainer != null) parentContainer.addWidget(this);
    }

    public Channel() {
      this((WContainerWidget) null);
    }

    void newMsgSent() {
      ++this.newMsg_;
      this.notif_.setTitle(String.valueOf(this.newMsg_) + " new messages");
      this.notif_.setBody(
          "There are " + String.valueOf(this.newMsg_) + " new messages waiting for you.");
      this.notif_.send();
    }

    private int newMsg_;
    private final WNotification notif_;

    void onNotifClick() {
      this.newMsg_ = 0;
    }
  }

  void NotificationChat() {
    WContainerWidget container = new WContainerWidget();
    final Channel channel = new Channel();
    container.addWidget(channel);
    WPushButton button = new WPushButton("Send Message", (WContainerWidget) channel);
    button
        .clicked()
        .addListener(
            this,
            () -> {
              channel.newMsgSent();
            });
  }