问题描述
经过长时间的努力和尝试不同的事情后: 我无法收到 firebase 消息(通过邮递员/firebase 控制台发送
firebase-messaging.sw.js
importScripts('https://www.gstatic.com/firebasejs/7.8.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.8.0/firebase-messaging.js');
firebase.initializeApp({
apiKey: 'AIzaSy....',authDomain: '....firebaseapp.com',projectId: '...-20254',storageBucket: '....appspot.com',messagingSenderId: '6962....',appId: '1:696....:web:7d0a.....',measurementId: 'G-Q....',});
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function (payload) {
console.log(
'[firebase-messaging-sw.js] Received background message ',payload
);
const notificationTitle = 'Background Message Title';
const notificationoptions = {
body: 'Background Message body.',icon: '/firebase-logo.png',};
self.registration.showNotification(notificationTitle,notificationoptions);
});
*** 也尝试了以下作为 setBackgroundMessageHandler:
messaging.setBackgroundMessageHandler(function(payload) {
console.log('back ')
const promiseChain = clients.matchAll({
type: "window",includeUncontrolled: true
}).then(windowClients => {
for (let i = 0; i < windowClients.length; i++) {
const windowClient = windowClients[i];
windowClient.postMessage(payload);
}
}).then(() =>{
return registration.showNotification("my notification title");
});
console.log(promiseChain)
self.registration.showNotification('title',{body:'body'});
return promiseChain;
});
push-notification.js
import firebase from 'firebase';
import {sendMessage} from './sendMessage';
firebase.initializeApp({
apiKey: 'AIzaSy....',});
const messaging = firebase.messaging();
export const ask = async () => {
await messaging.requestPermission()
const token = await messaging.getToken();
console.log('user token: ',token);
sendMessage(token); // sending with POST,which works
return token;
}
messaging.onMessage(function (payload) {
alert('Foreground message fired!');
console.log(payload);
});
manifest.json
{
"short_name": "app name","name": "Create React App Sample","start_url": ".","display": "standalone","theme_color": "#000000","background_color": "#ffffff","gcm_sender_id": "69...."
}
App.js
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./firebase-messaging-sw.js')
.then(function(registration) {
console.log('Registration successful,scope is:',registration.scope);
ask(); // request premission at push-notification.js
}).catch(function(err) {
console.log('Service worker registration Failed,error:',err);
});
}
控制台和网络: 成功对象
multicast_id: 4369108....
success: 1
failure: 0
canonical_ids: 0
results: Array(1)
0: {message_id: "0:16099......"}
length: 1
__proto__: Array(0)
__proto__: Object
任何帮助将不胜感激!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)