【问题标题】:firebase safari error Messaging: This browser doesn't support the API's required to use the firebase SDKfirebase safari 错误消息:此浏览器不支持使用 firebase SDK 所需的 API
【发布时间】:2021-04-10 22:10:44
【问题描述】:

我们使用的是 firebase 8.2.1 版本。
我们正在使用 react 和 typescript 进行开发。
仅当我在 safari 中查看时才会出现该错误。 错误是 FirebaseError: Messaging: This browser does not 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.');
      });
    });
};

一个

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.');
      });
    });
};

【问题讨论】:

    标签: reactjs typescript firebase


    【解决方案1】:

    遗憾的是,Safari 尚不支持 Push API,因此无法使用消息传递。

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

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

    有关 .isSupported() 的详细信息,请参阅documentation

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

    【讨论】:

    • 感谢您的回复。我应该把上面的代码放在我的代码哪里?
    • 您应该在顶部声明代码。然后,不要在 prepareNotification 中写 firebase.messaging(),而是写 messaging.requestPermission()。并且不要忘记测试 if (!messaging) return; 或者您可能想要提醒它不受支持。
    • 如果有帮助,请批准答案。
    • 这是否意味着你可以通过写“a”之类的东西来解决问题?
    • 不,因为现在您只是用prepareNotification 修复它,您还希望它在prepareNotificationToken 中。所以不要写两次,而是在firebase.apps.length 旁边声明它。除此之外,在检查messaging不为空之后,您需要使用messaging而不是firebase.messaging()
    猜你喜欢
    • 2020-07-11
    • 2019-06-27
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多