messages.requestPermission 不是一个函数

发布于 2025-01-11 13:10:58 字数 1064 浏览 1 评论 0原文

尝试向应用程序发送推送通知,但我需要用户的许可,但收到此错误消息: messages.requestPermission 不是一个函数, 这是我的推送 notification.js 文件的代码

import * as  firebase from 'firebase/app';
import { getMessaging } from "firebase/messaging";
export const  initializeFirebase = () => {
  firebase.initializeApp({
    apiKey: "",
    authDomain: "",
    projectId: "pushnotification-9b180",
    storageBucket: "pushnotification-9b180.appspot.com",
    messagingSenderId: "878043563283",
    appId: "1:878043563283:web:c2a44f3c8b02ad8a17c6e6",
    measurementId: "G-GMWQKL94ZD"
  });
}
 
export const askForPermissionToReceiveNotifications = async () => {
    try {
      const messaging = getMessaging();

      await messaging.requestPermission();
      const token = await messaging.getToken();
      console.log('Your token is:', token);
      
      return token;
    } catch (error) {
      console.error(error);
    }
  }

这是错误的屏幕截图

erros

trying to send push notification to app but i need permission from user for that but getting this error that messaging.requestPermission is not a function ,
here is my code for push notification.js file

import * as  firebase from 'firebase/app';
import { getMessaging } from "firebase/messaging";
export const  initializeFirebase = () => {
  firebase.initializeApp({
    apiKey: "",
    authDomain: "",
    projectId: "pushnotification-9b180",
    storageBucket: "pushnotification-9b180.appspot.com",
    messagingSenderId: "878043563283",
    appId: "1:878043563283:web:c2a44f3c8b02ad8a17c6e6",
    measurementId: "G-GMWQKL94ZD"
  });
}
 
export const askForPermissionToReceiveNotifications = async () => {
    try {
      const messaging = getMessaging();

      await messaging.requestPermission();
      const token = await messaging.getToken();
      console.log('Your token is:', token);
      
      return token;
    } catch (error) {
      console.error(error);
    }
  }

Here is the screenshot of errors

erros

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再可℃爱ぅ一点好了 2025-01-18 13:10:58

你用的是react-native还是react?

但以反应本机的方式,你需要添加

import messaging from '@react-native-firebase/messaging';

你不需要的这一行

const messaging = getMessaging();

What are you using react-native or react?

but in react-native way you need add this line

import messaging from '@react-native-firebase/messaging';

you don´t need

const messaging = getMessaging();
ˇ宁静的妩媚 2025-01-18 13:10:58
import * as  firebase from 'firebase/app';
import { getMessaging } from "firebase/messaging";
export const  initializeFirebase = () => {
  firebase.initializeApp({
    apiKey: "",
    authDomain: "",
    projectId: "pushnotification-9b180",
    storageBucket: "pushnotification-9b180.appspot.com",
    messagingSenderId: "878043563283",
    appId: "1:878043563283:web:c2a44f3c8b02ad8a17c6e6",
    measurementId: "G-GMWQKL94ZD"
  });
}
 
export const askForPermissionToReceiveNotifications = async () => {
    try {
      const messaging = getMessaging();

      await messaging.requestPermission();
      const token = await messaging.getToken();
      console.log('Your token is:', token);
      
      return token;
    } catch (error) {
      console.error(error);
    }
  }

import * as  firebase from 'firebase/app';
import { getMessaging } from "firebase/messaging";
export const  initializeFirebase = () => {
  firebase.initializeApp({
    apiKey: "",
    authDomain: "",
    projectId: "pushnotification-9b180",
    storageBucket: "pushnotification-9b180.appspot.com",
    messagingSenderId: "878043563283",
    appId: "1:878043563283:web:c2a44f3c8b02ad8a17c6e6",
    measurementId: "G-GMWQKL94ZD"
  });
}
 
export const askForPermissionToReceiveNotifications = async () => {
    try {
      const messaging = getMessaging();

      await messaging.requestPermission();
      const token = await messaging.getToken();
      console.log('Your token is:', token);
      
      return token;
    } catch (error) {
      console.error(error);
    }
  }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文