1

我已經爲聊天做了Webkit桌面通知。當用戶不在當前聊天頁面(我的意思是在另一個選項卡上)時,獲取通知。一切正常,因爲我希望他們工作:)單擊Webkit通知時,如何從瀏覽器中的任何其他選項卡移回當前選項卡

現在我需要做的是,當用戶點擊瀏覽器中的另一個標籤上出現的通知,它會將他移回到聊天標籤。

例如,用戶在MyAwesomeChat.com上聊天,然後移動到Google.com並在那裏獲得通知。只要他點擊通知,它會把他帶到MyAwesomeChat.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() 

任何想法/幫助/建議將是一個很大的欣賞。先謝謝你。

回答

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

乾杯!

+0

@FrOzenFyr 嘿,我不知道綁定部分。它的作品:) 非常感謝,很感激。 –

+0

嘿,朋友,我一看到你的答案就盡力去做。但它會提示'需要15個聲望'。 :( 我在這裏是新來的,所以我會很快做一個關於如何收集聲譽的研究,一旦我收到足夠的信息,我會盡你所能去做上面的事情。:) –

+0

@ the.big.lebowski:你可以閱讀它[這裏](http://stackoverflow.com/help/whats-reputation)和一點投票[這裏](http://stackoverflow.com/help/why-vote)。順便說一下,如果您標記我的答案被接受,您將收到+2代表。 – Fr0zenFyr

相關問題