1

我为聊天制作了一个 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()

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

4

1 回答 1

2

默认情况下,通知与触发它的窗口/选项卡绑定。因此,您只需要告诉它在单击时将注意力集中在绑定选项卡上。

添加类似的东西

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()

干杯!!

于 2013-10-11T13:27:14.483 回答