我正在使用Firebase Cloud Messaging (FCM)发送数据消息,以便我可以使用Service Worker处理通知。现在我正在使用Service Worker显示通知,当我点击通知时,我想将通知内容复制到剪贴板中。
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler((payload)=> {
const title = payload.data.title;
const options = {
body: payload.data.body
};
return self.registration.showNotification(title,
options);
});
self.addEventListener('notificationclick', (event)=>{
console.log(event);
navigator.clipboard.writeText(event).then(function() {
console.log('Async: Copying to clipboard was successful!');
}, function(err) {
console.error('Async: Could not copy text: ', err);
});
});
当通知被点击时,会触发notificationclick
事件。但是我得到了navigator.clipboard
的undefined。我还为我的网站使用了安全域。我也不能使用document.execcommand('copy')
,因为无法使用Service Worker访问DOM。请问是否有一种方法可以在不打开任何URL的情况下复制通知内容?