【问题标题】:How to move back to the current tab from any other tab in browser when a Webkit Notification is clicked单击 Webkit 通知时如何从浏览器中的任何其他选项卡移回当前选项卡
【发布时间】:2020-09-20 17:42:13
【问题描述】:

我为聊天制作了一个 Webkit 桌面通知。当用户不在当前聊天页面(我的意思是在另一个选项卡上)时,会收到通知。一切正常,因为我希望它们正常工作:)

现在我需要做的是,当用户点击浏览器中另一个选项卡上出现的通知时,它会将他移回聊天选项卡。

一个例子,用户在MyAwesomeChat.example.com 上聊天,然后转到 Google.com 并在那里收到通知。只要他点击通知,它就会将他带到MyAwesomeChat.example.com**

下面我在一个函数中编写了我的代码。我在我想做上述事情的地方添加了一个“点击”addEventListner,尽管仍然一无所知。

desktopnotify:(chat) ->
                if (window.webkitNotifications)
                    havePermission = window.webkitNotifications.checkPermission()
                    if havePermission is 0 && @window_blur
                      if (!(chat.get('USER_MAIL')==LOCAL_DATA.username)) 
                      # 0 is PERMISSION_ALLOWED means the user 'allow's the permission to show desktop notification when asked
                        pLength = Participants.where({USER_MAIL:chat.get('USER_MAIL')}).length
                        userImage = Participants.where({USER_MAIL:chat.get('USER_MAIL')})[pLength-1].get('USER_IMAGE')
                        
                        if(userImage?)
                          notification = window.webkitNotifications.createNotification(userImage, chat.get('USER_MAIL'), chat.get('MESSAGE'))
                          
                        else
                          notification = window.webkitNotifications.createNotification('/images/no-image.jpeg', chat.get('USER_MAIL'), chat.get('MESSAGE'))
                        notification.addEventListener('display',()->
                          window.setTimeout((e)->
                            notification.cancel()
                          , 5000)
                        notification.addEventListener('click',()->
                            console.log('here i want to do the magic. it will take back user to the page where he was chatting')
                            )  
                        )
                        notification.show()

任何想法/帮助/建议将不胜感激。

【问题讨论】:

    标签: javascript google-chrome-extension coffeescript dom-events webkit-notifications


    【解决方案1】:

    默认情况下,通知与触发它的窗口/选项卡绑定。所以你只需要告诉它在点击时将注意力集中在绑定标签上。

    添加类似的东西

    notification.onclick = function() { 
    window.focus();    // focus on binding window
    this.cancel();     // close the notification on clicking it
    };
    

    所以你的最终代码应该是这样的

        if (window.webkitNotifications)
                            havePermission = window.webkitNotifications.checkPermission()
                            if havePermission is 0 && @window_blur
                              if (!(chat.get('USER_MAIL')==LOCAL_DATA.username)) 
                              # 0 is PERMISSION_ALLOWED means the user 'allow's the permission to show desktop notification when asked
                                pLength = Participants.where({USER_MAIL:chat.get('USER_MAIL')}).length
                                userImage = Participants.where({USER_MAIL:chat.get('USER_MAIL')})[pLength-1].get('USER_IMAGE')
    
                                if(userImage?)
                                  notification = window.webkitNotifications.createNotification(userImage, chat.get('USER_MAIL'), chat.get('MESSAGE'))
    
                                else
                                  notification = window.webkitNotifications.createNotification('/images/no-image.jpeg', chat.get('USER_MAIL'), chat.get('MESSAGE'))
                                notification.addEventListener('display',()->
                                  window.setTimeout((e)->
                                    notification.cancel()
                                  , 5000)
                                notification.addEventListener('click',()->
                                    console.log('here i want to do the magic. it will take back user to the page where he was chatting')
                                    )  
                                )
    
    //--------------------------------------------------------------------//
    
                                notification.onclick = function() { 
                                window.focus();
                                this.cancel();
                                };
    
    //--------------------------------------------------------------------//
    
                                notification.show()
    

    干杯!!

    【讨论】:

    • @FrOzenFyr 嘿,我不知道绑定部分。它有效 :) 非常感谢,感谢。
    • 嘿朋友,我一看到你的回答就尝试这样做。但它提示'需要15声望'。 :(我是新来的,所以很快就会研究如何收集声望,一旦我得到足够的声望,我就会做你想做的一切。:)
    • @the.big.lebowski:你可以阅读它here 和一些关于投票here 的信息。顺便说一句,如果您将我的回答标记为已接受,您将获得 +2 代表。
    • 干杯!留下来寻找数百个问题,您可以在这些问题上提供答案以收集声誉。您可以提出问题以获得代表。也是。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多