I am using Firebase Cloud Messaging (FCM) to send Data messages so that I can handle notification using Service Worker. Now I show the notification using Service Worker and when I click the notification I want to copy the content of notification in the clipboard.
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);
});
});
When notification is clicked notificationclick
event is fired. But I am getting navigator.clipboard
as undefined. I am also using secured domain for my website. I am also not able to use document.execcommand('copy')
because DOM is not accessible using Service Worker. Can you please suggest a way to copy notification content without opening any url?