Ionic android无法注册ServiceWorker:获取脚本时发生未知错误

时间:2019-04-17 12:36:19

标签: android cordova service-worker ionic4

我所拥有的

我正在使用Firebase和Ionic 4。

我得到了这个插件cordova-plugin-firebase@2.0.5

我做了一个push-notification.service.ts,看起来像这样:

  initPushNotification() {

    this.firebase.getToken()
      .then(token => {
        // Get token
      })
      .catch(error => console.error('Error getting token :', error));

    this.firebase.onTokenRefresh()
    .subscribe((token: string) => {
      // Get token
    });

    // When app is active (foreground) and receive a notification
    this.firebase.onNotificationOpen()
      .subscribe(data => {
        // Get new notification
      });
  }

  displayBannerNotification(message: string, interlocutor: User) {
    const options = new HttpHeaders()
      .set('Content-Type', 'application/json')
      .set('Authorization', `key=${environment.firebase.serverKey}`);

    const body = {
      'notification': {
        'title': interlocutor.nickname,
        'body': message,
        'click_action': 'https://www.x.com/'
      },
      // tslint:disable-next-line:max-line-length
      'to': 'TOKEN-DEVICE'
    };

    this.http.post( 'https://fcm.googleapis.com/fcm/send', JSON.stringify(body), { headers: options })
      .subscribe(data => { console.log('Message envoyé ! ', data); });
  }

initPushNotification()从app.component.ts中调用。

我了解到必须聘请一名服务人员。但显然,它们在设备(android)上不可用。无论如何,我做到了:

importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js');

firebase.initializeApp({
  // get this from Firebase console, Cloud messaging section
  'messagingSenderId': 'ID'
});

const messaging = firebase.messaging();

messaging.setBackgroundMessageHandler(payload => {
  console.log('Received background message ', payload);
  // here you can override some options describing what's in the message; 
  // however, the actual content will come from the Webtask
  const notificationTitle = "Background message title"
  const notificationOptions = {
    body: 'Background Message body.',
  };
  return self.registration.showNotification(notificationTitle, notificationOptions);
});

我想要的

在后台模式下将横幅通知从Firebase触发到我的应用

我会得到什么

当我从Firebase控制台触发推送通知时,该通知仅出现在通知面板上,并且没有横幅滑到设备顶部。

我从设备android收到此错误:

TypeError: Failed to register a ServiceWorker: An unknown error occurred when fetching the script

0 个答案:

没有答案