部署后,Firebase Messaging无法获得令牌

时间:2018-11-30 10:58:51

标签: javascript angular firebase firebase-cloud-messaging

我正在尝试使用Firebase Messaging在浏览器中进行通知。实际上,它可以在本地运行,但不能在服务器上部署。

它在Angular5应用程序中使用。

"@angular-devkit/core": "0.0.28",
"@angular/animations": "5.2.9",
"@angular/common": "5.2.9",
"@angular/compiler": "5.2.9",
"@angular/core": "5.2.9",
"@angular/forms": "5.2.9",
"@angular/http": "5.2.9",
"@angular/platform-browser": "5.2.9",
"@angular/platform-browser-dynamic": "5.2.9",
"@angular/router": "5.2.9",
"@angular/service-worker": "5.2.9",
"@firebase/app": "^0.3.4",
"angularfire2": "^5.0.0-rc.7",
"firebase": "^5.0.3",

这是我使用的代码:

import * as firebase from 'firebase/app';
import 'firebase/messaging';

constructor(private http: HttpClient, private afAuth: AngularFireAuth,
          @Inject(FirebaseApp) private _firebaseApp: firebase.app.App) {
  const messaging = this.getFirebaseMessaging();
  messaging.onTokenRefresh(() => {
    messaging.getToken().then((refreshedToken) => {
      this.notificationTokenRefreshed$.next(refreshedToken);
    });
  });
}

getFirebaseMessaging() {
  return firebase.messaging(this._firebaseApp);
}

(...)

getNotificationToken() {
  const messaging = this.getFirebaseMessaging();
  return fromPromise(messaging.requestPermission().then(() => {
    return messaging.getToken();
  }));
}

我使用以下firebase-messaging-sw.js文件:

// [START initialize_firebase_in_sw]
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here, other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js');

// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
firebase.initializeApp({
  apiKey: "abc",
  authDomain: "some-app.firebaseapp.com",
  databaseURL: "https://some-app.firebaseio.com",
  projectId: "some-app",
  storageBucket: "some-app.appspot.com",
  messagingSenderId: "123"
});

// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
// [END initialize_firebase_in_sw]

// If you would like to customize notifications that are received in the
// background (Web app is closed or not in browser focus) then you should
// implement this optional method.
// [START background_handler]
messaging.setBackgroundMessageHandler(function(payload) {
  console.log('[firebase-messaging-sw.js] Received background message ', payload);
  // Customize notification here
  const notificationTitle = 'Background Message Title';
  const notificationOptions = {
    body: 'Background Message body.'
  };

  return self.registration.showNotification(notificationTitle,
    notificationOptions);
});
// [END background_handler]

我们有不同的行为:

  • getNotificationToken对某些用户什么也不返回(实际上并没有返回)
  • 我们对于某些用户也有此错误

    code: "messaging/unsupported-browser"
    message: "Messaging: This browser doesn't support the API's required to use the firebase SDK. (messaging/unsupported-browser)."
    stack: "FirebaseError: Messaging: This browser doesn't support the API's required to use the firebase SDK. (messaging/unsupported-browser).↵    at Object.Kt.a.INTERNAL.registerService.isSupported [as messaging
    

我错过了什么吗?如何使用Firebase Messaging正确实施通知处理。

感谢您的帮助! 蒂埃里

0 个答案:

没有答案