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. Wt 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
#include <Wt/WContainerWidget.h>
#include <Wt/WNotification.h>
#include <Wt/WPushButton.h>
#include <Wt/WString.h>
#include <Wt/WText.h>

auto container = std::make_unique<Wt::WContainerWidget>();

Wt::WPushButton * button = container->addNew<Wt::WPushButton>("Get asked for notification");
Wt::WText *text = container->addNew<Wt::WText>();

button->clicked().connect([=](){
    Wt::WNotification::askPermission();
    button->setEnabled(false);
});

Wt::WNotification::permissionUpdated().connect([=](Wt::WNotification::Permission permission){
    if (permission == Wt::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
#include <Wt/WContainerWidget.h>
#include <Wt/WNotification.h>
#include <Wt/WPushButton.h>
#include <Wt/WString.h>
#include <Wt/WText.h>

auto container = std::make_unique<Wt::WContainerWidget>();

std::unique_ptr<Wt::WNotification> persistant = std::make_unique<Wt::WNotification>("You will see me everytime");
Wt::WNotification *p = persistant.get();
container->addChild(std::move(persistant));
p->setSilent();
Wt::WPushButton * button = container->addNew<Wt::WPushButton>("Receive notifications");

button->clicked().connect([=](){
    p->send();
    Wt::WNotification oneUse("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
#include <Wt/WContainerWidget.h>
#include <Wt/WNotification.h>
#include <Wt/WPushButton.h>
#include <Wt/WString.h>
#include <Wt/WText.h>


class Channel: public Wt::WContainerWidget
{
public:
    Channel()
        : newMsg_(0)
    {
        notif_.setIcon("icons/wt.png");
        notif_.setSilent();
        notif_.clicked().connect(this, &Channel::onNotifClick);
    }

    void newMsgSent()
    {
        ++newMsg_;
        notif_.setTitle(std::to_string(newMsg_)+" new messages");
        notif_.setBody("There are "+std::to_string(newMsg_)+" new messages waiting for you.");
        notif_.send();
    }

private:
    int newMsg_;
    Wt::WNotification notif_;

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

auto container = std::make_unique<Wt::WContainerWidget>();
Channel *channel = container->addWidget(std::make_unique<Channel>());

Wt::WPushButton * button = channel->addNew<Wt::WPushButton>("Send Message");

button->clicked().connect([=](){
    channel->newMsgSent();
});