1

我的目標是在Firebase中的值發生更改時使用Chrome擴展程序創建通知。
使用下面的代碼,我在第一次更改該值時收到通知,但不是以下時間。Chrome擴展程序通知僅顯示一次

f.on('child_changed', function (snapshot) { 
    ignoreInitialData = false; 
    var options = { 
    type: 'basic', 
    iconUrl: '../../icons/green.png', 
    title: "Availability notifier", 
    message: snapshot.val(), 
    contextMessage: "", 
    eventTime: Date.now(), 
    isClickable: false, 
    }; 
    chrome.notifications.create("child_changed", options, function (notificationID) { 
    console.error(chrome.runtime.lastError); 
    }); 
}); 

任何解決方案?

回答

5

由於您使用的是固定通知ID,因此它已更新而不是創建新通知ID。

如果通知沒有關閉但隱藏在Chrome通知中心,則會再次顯示而不是

選項包括:

  • 不使用固定的ID(通過""爲ID)
    這將導致這兩個老的和新的通知,將保持,直到被解散。可能不是你想要的。

  • Closing the old notification就在展示一個新的。
    這個效果很好,但如果您的舊通知尚未消失,則會在視覺上產生震動。但是,如果您想強調「這是新信息」,您可能實際上需要此效果。

  • 使用priority change trick to re-show the notification
    這最好,但是「哈克」。我們只能希望Chrome API能夠在本地完善。

1

您還可以添加時間戳後綴,以便每一個通知不同的是:

var timestamp = new Date().getTime(); 
var id = 'myid' + timestamp;