简体   繁体   English

如何使气球通知中的URL可点击?

[英]How to make a URL in a balloon notification clickable?

I'm creating a simple IntelliJ Plugin that allows for creating new Pastebin pastes straight from the IDE. 我正在创建一个简单的IntelliJ插件,该插件可直接从IDE创建新的Pastebin粘贴。

When a paste has been successfully posted to Pastebin, I'd like to display a balloon notification. 将粘贴成功发布到Pastebin后,我想显示一个气球通知。

Currently the notification is displayed as follows: 当前,通知显示如下:

final Response<String> postResult = Constants.PASTEBIN.post(paste);
        NotificationGroup balloonNotifications = new NotificationGroup("Notification group", NotificationDisplayType.BALLOON, true);
        if (postResult.hasError()) {
            //Display error notification
        } else {
            //Display success notification
            Notification success = balloonNotifications.createNotification("Successful Paste", "<a href=\"" + postResult.get() + "\">Paste</a> successfully posted!", NotificationType.INFORMATION, null);
            Notifications.Bus.notify(success, project);
        }

Now, this balloon notification contains the URL to the newly created paste, but unfortunately clicking it does not open the link in a browser. 现在,此气球通知包含新创建的粘贴的URL,但是不幸的是,单击它并不会在浏览器中打开链接。 How can that be achieved? 如何实现?

The balloon notification with the URL that should become clickable: 带有URL的气球通知应可单击: 气球通知

After some searching I found the answer myself. 经过一番搜索,我自己找到了答案。 It wasn't too difficult actually. 其实并不太难。

If I instantiate the notification as follows, the link is clickable as was required. 如果我按以下方式实例化通知,则该链接可根据需要单击。

Notification success = balloonNotifications.createNotification("<html>Successful Paste", "<a href=\"" + postResult.get() + "\" target=\"blank\">Paste</a> successfully posted!</html>", NotificationType.INFORMATION, (notification, hyperlinkEvent) -> {
            if (hyperlinkEvent.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                BrowserUtil.browse(hyperlinkEvent.getURL());
            }
        });

Note: It is also rather useful that the link in the event log is also clickable now. 注意:事件日志中的链接现在也可以单击,这也非常有用。

There is NotificationListener which opens urls in notifications: com.intellij.notification.UrlOpeningListener 有NotificationListener可以在通知中打开URL: com.intellij.notification.UrlOpeningListener

So you can write: 所以你可以这样写:

Notification success = balloonNotifications.createNotification(
            "<html>Successful Paste", "<a href=\"" + postResult.get() + "\" target=\"blank\">Paste</a> successfully posted!</html>",
            NotificationType.INFORMATION, new NotificationListener.UrlOpeningListener(true));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM