reactjs - firebase safari 错误消息 : This browser doesn't support the API's required to use the firebase SDK

标签 reactjs typescript firebase

我们使用的是 firebase 8.2.1 版本。
我们正在使用 React 和 TypeScript 进行开发。
仅当我在 safari 中查看时才会出现该错误。 错误是 FirebaseError: Messaging: This browser doesn't support the API's required to use the firebase SDK.(messaging/unsupported-browser).
看了下面的文档,只有safari不支持云消息。
我该如何解决这个问题?
https://firebase.google.com/support/guides/environments_js-sdk?hl=ja[enter此处链接描述] 1

import firebase from 'firebase/app';
import 'firebase/messaging';
import { asyncNotificationToken } from 'apis/asyncNotificationToken';

const firebaseConfig = {
  apiKey: '******************',
  projectId: '******',
  messagingSenderId: '*******',
  appId: '********',
};

const VAPID_KEY =
  '******************************';

if (firebase.apps.length < 1) {
  firebase.initializeApp(firebaseConfig);
}

export const prepareNotification = () => {
  firebase
    .messaging()
    .requestPermission()
    .then(() => {
      prepareNotificationToken();
    })
    .catch(() => {
      console.log('Unable to get permission to notify.');
    });
};

export const prepareNotificationToken = () => {
  firebase
    .messaging()
    .getToken({ vapidKey: VAPID_KEY })
    .then((token) => {
      asyncNotificationToken(token).then(() => {
        console.log('Registed notification token.');
      });
    });
};

a

export const prepareNotification = () => {
  let messaging = null;
  if (firebase.messaging.isSupported()) {
    messaging = firebase.messaging();
  }
  firebase
    .messaging()
    .requestPermission()
    .then(() => {
      prepareNotificationToken();
    })
    .catch(() => {
      console.log('Unable to get permission to notify.');
    });
};

export const prepareNotificationToken = () => {
  firebase
    .messaging()
    .getToken({ vapidKey: VAPID_KEY })
    .then((token) => {
      asyncNotificationToken(token).then(() => {
        console.log('Registed notification token.');
      });
    });
};

最佳答案

悲哀Push API Safari 尚不支持 Messaging,因此无法使用 Messaging。

在尝试使用之前检查兼容性。

let messaging = null;
if (firebase.messaging.isSupported(){
    messaging = firebase.messaging();
}

参见 documentation有关 .isSupported() 的详细信息。

有一个feature request支持safari push notification在 Mac 桌面上。

关于reactjs - firebase safari 错误消息 : This browser doesn't support the API's required to use the firebase SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65574164/

相关文章:

javascript - 调整 Material-UI <TextField> 大小会导致光标退出其容器

javascript - 'ItemIsLoading' on 指的是一个类型,但在这里被用作一个值

typescript - 我们什么时候在 Angular 2 中使用提供者?

android - FirebaseListAdapter 字母顺序

ios - 如何处理 firebase 动态链接的生命周期? (iOS)

unit-testing - 如何对调用子级方法的父级 react 组件进行单元测试

reactjs - 基于其他 Prop 值(value)的条件类型

javascript - 如何使用 bundle 中的 node_modules 依赖项正确构建用于生产的 NestJS 应用程序?

Javascript 将变量添加到正则表达式

javascript - 为什么 react-native-gifted-chat 不能从 firebase 时间戳正确显示时间?