如何启用桌面通知? jxBrowser

时间:2017-01-10 17:31:51

标签: jxbrowser

如何启用桌面通知? 我搜遍了整个文档。没有找到任何关于权限或通知的信息

由于 Tzelon

1 个答案:

答案 0 :(得分:0)

以下示例显示如何使用JxBrowser Notifications API显示桌面通知:

import com.teamdev.jxbrowser.chromium.*;
import com.teamdev.jxbrowser.chromium.events.NotificationEvent;
import com.teamdev.jxbrowser.chromium.events.NotificationListener;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Demonstrates how to handle HTML5 Desktop Notifications.
 */
public class NotificationSample {
    public static void main(String[] args) {
        Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        // Grant notification permission if it's necessary
        browser.setPermissionHandler(new PermissionHandler() {
            @Override
            public PermissionStatus onRequestPermission(PermissionRequest request) {
                if (request.getPermissionType() == PermissionType.NOTIFICATIONS) {
                    return PermissionStatus.GRANTED;
                }
                return PermissionStatus.DENIED;
            }
        });

        // Display your own notification GUI
        BrowserContext browserContext = browser.getContext();
        NotificationService notificationService = browserContext.getNotificationService();
        notificationService.setNotificationHandler(new NotificationHandler() {
            @Override
            public void onShowNotification(NotificationEvent event) {
                showNotification(event.getNotification());
            }
        });

        browser.loadURL("notifications.html");
    }

    private static void showNotification(final Notification notification) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final JFrame frame = new JFrame(notification.getTitle());
                frame.setAlwaysOnTop(true);
                JLabel messageLabel = new JLabel(notification.getMessage());
                messageLabel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
                frame.add(messageLabel, BorderLayout.CENTER);
                frame.setMinimumSize(new Dimension(300, 100));
                frame.pack();
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
                frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        notification.close();
                    }
                });

                notification.addNotificationListener(new NotificationListener() {
                    @Override
                    public void onClose(NotificationEvent event) {
                        SwingUtilities.invokeLater(new Runnable() {
                            @Override
                            public void run() {
                                frame.setVisible(false);
                                frame.dispose();
                            }
                        });
                    }
                });
            }
        });
    }
}