![](/img/trans.png)
[英]How to trigger an event at App.tsx from onBackgroundMessage in firebase-messaging-sw.js with react
[英]How to play audio/sound in firebase-messaging-sw.js
我對如何在firebase-messaging-sw.js
中播放通知聲音/音頻文件有疑問。 但是消息發送成功。 這是我到目前為止實施的
importScripts("https://gstatic.com/firebasejs/9.9.3/firebase-app-compat.js");
importScripts("https://gstatic.com/firebasejs/9.9.3/firebase-messaging-compat.js");
firebase.initializeApp({
messagingSenderId: "...",
});
const messaging = firebase.messaging();
messaging.onBackgroundMessage(function (payload) {
const options= {
body: notification.body,
icon: notification.icon,
sound: "/media/notification.mp3",
};
return self.registration.showNotification(payload.notification.title, options);
});
從我從多個來源讀到的內容來看,firebase 雲消息僅在移動設備中提供聲音通知。
但是,當通知出現在我的桌面/瀏覽器中時,我想播放音頻/聲音。 有沒有辦法在瀏覽器中播放音頻?
我也試過像這樣播放音頻
messaging.onBackgroundMessage(function (payload) {
const options = {
body: notification.body,
icon: notification.icon,
sound: "/media/notification.mp3",
};
const audio = new Audio("/media/notification.mp3");
audio.play();
return self.registration.showNotification(payload.notification.title, options);
});
但仍然無法播放音頻通知。
這是請求 POST 到 FCM https://fcm.googleapis.com/fcm/send
,帶有正文參數
{
"notification": {
"title": "Title Notification",
"body": "Lorem ipsum dolor",
"click_action": "FCM_PLUGIN_ACTIVITY",
"icon": "/media/logos/png-Icon-Logo.ico",
"sound": "/media/notification.mp3"
},
"to": "...", // firebase messaging token
"priority": "high"
}
通知工作沒有問題,但通知顯示時沒有音頻反饋。
我認為您遇到了在沒有用戶交互的情況下播放音頻的問題。 此線程中的更多信息。
是否可以在用戶之前不與 web 交互的情況下播放音頻? 我該怎么做?
簡短的回答是你現在不能
長答案是你可以通過一些技巧來做到這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.