簡體   English   中英

如何使用 react 在 firebase-messaging-sw.js 中的 onBackgroundMessage 觸發 App.tsx 的事件

[英]How to trigger an event at App.tsx from onBackgroundMessage in firebase-messaging-sw.js with react

我在公共文件夾中有一個文件 firebase-messaging-sw.js。 當應用服務器發送消息並且瀏覽器處於后台時,會觸發 onBackgroundMessage function 並顯示通知。 這是代碼:

const messaging = firebase.messaging();
messaging.onBackgroundMessage(function(payload) {
  console.log('[firebase-messaging-sw.js] Received background message ', payload);
  // Customize notification here
  const notificationTitle = payload.notification?.title;
  const notificationOptions = {
    body: payload.notification?.body,
    icon: '/firebase-logo.png'
  };
  
  self.registration.showNotification(notificationTitle, notificationOptions);
  // Call a function in app.tsx to show a modal dialog when the user back to the browser
  //code example
});

我想當 onBackgroundMessage 被觸發時,它會在 app.tsx 的 react 處觸發 function。 你是怎樣做的?

self.client.postMessage are helping to triggering the event
initMessaging.onBackgroundMessage(function(payload) {
  console.log('Received background message ', payload);
  
 // Customize notification here
  const notification = payload.data;
  const notificationTitle = payload.data.body.title;
  const notificationOptions = {
    body: payload.data.body,
  };
  **self.clients.matchAll({
    type: 'window',
    includeUncontrolled: true
  }).then(all => all.forEach(client => {
    console.log('client',client)
    client.postMessage(payload);
    // client.postMessage("Responding to ");
   }))**
   self.registration.showNotification(notificationTitle,
    notificationOptions);
});

你如何處理 app.tsx 中的 postmessage 事件。 我無法從 onBackgroundMessage 接收事件發布消息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM