我正在关注此 Codelab 教程 - https://firebase.google.com/codelabs/firebase-web#10 但我在使用 FCM 发送云消息时遇到问题 我没有获得在我的应用程序中启用通知的权限
我的 js 函数
async function saveMessagingDeviceToken() {
try {
const currentToken = await getToken(getMessaging());
if (currentToken) {
console.log('Got FCM device token:', currentToken);
// Saving the Device Token to Cloud Firestore.
const tokenRef = doc(getFirestore(), 'fcmTokens', currentToken);
await setDoc(tokenRef, { uid: getAuth().currentUser.uid });
// This will fire when a message is received while the app is in the foreground.
// When the app is in the background, firebase-messaging-sw.js will receive the message instead.
onMessage(getMessaging(), (message) => {
console.log(
'New foreground notification from Firebase Messaging!',
message.notification
);
});
} else {
// Need to request permissions to show notifications.
requestNotificationsPermissions();
}
} catch(error) {
console.error('Unable to get messaging token.', error);
};
}
// Requests permissions to show notifications.
async function requestNotificationsPermissions() {
console.log('Requesting notifications permission...');
const permission = await Notification.requestPermission();
if (permission === 'granted') {
console.log('Notification permission granted.');
// Notification permission granted.
await saveMessagingDeviceToken();
} else {
console.log('Unable to get permission to notify.');
}
}
我还在配置文件中正确启用了 FCM 服务
import { initializeApp } from 'firebase/app';
import { getMessaging } from 'firebase/messaging/sw';
import { getFirebaseConfig } from './firebase-config';
const firebaseApp = initializeApp(getFirebaseConfig());
getMessaging(firebaseApp);
console.info('Firebase messaging service worker is set up');
谁能帮我解决问题
我在索引文件中检查了这个函数的出现,它初始化得很好。