Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
angular 7中的Firebase推送通知无法注册serviceworker_Angular_Firebase_Push Notification_Firebase Cloud Messaging - Fatal编程技术网

angular 7中的Firebase推送通知无法注册serviceworker

angular 7中的Firebase推送通知无法注册serviceworker,angular,firebase,push-notification,firebase-cloud-messaging,Angular,Firebase,Push Notification,Firebase Cloud Messaging,我在尝试实现firebase推送通知时遇到此错误 “messaging.service.ts:54无法获得通知权限。FirebaseError:消息:我们无法注册默认的服务工作者。注册ServiceWorker失败:获取脚本时收到错误的HTTP响应代码(404)。(消息/ServiceWorker注册失败)。” 这是我的messaging.service.ts import { Injectable } from '@angular/core'; import { AngularFireData

我在尝试实现firebase推送通知时遇到此错误

“messaging.service.ts:54无法获得通知权限。FirebaseError:消息:我们无法注册默认的服务工作者。注册ServiceWorker失败:获取脚本时收到错误的HTTP响应代码(404)。(消息/ServiceWorker注册失败)。”

这是我的messaging.service.ts

import { Injectable } from '@angular/core';
import { AngularFireDatabase } from '@angular/fire/database';
import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFireMessaging } from '@angular/fire/messaging';
import { mergeMapTo } from 'rxjs/operators';
import { take } from 'rxjs/operators';
import { BehaviorSubject } from 'rxjs'

@Injectable()
export class MessagingService {

  currentMessage = new BehaviorSubject(null);

  constructor(
    private angularFireDB: AngularFireDatabase,
    private angularFireAuth: AngularFireAuth,
    private angularFireMessaging: AngularFireMessaging) {



    console.log("firebase.....");
    this.angularFireMessaging.messaging.subscribe(
      (_messaging) => {
        console.log(_messaging,"messegiung");
        _messaging.onMessage = _messaging.onMessage.bind(_messaging);
         console.log(_messaging,"messegiung");
        _messaging.onTokenRefresh = _messaging.onTokenRefresh.bind(_messaging);
         console.log(_messaging,"messegiung");
      }
    )
  }

  /**
   * update token in firebase database
   * 
   * @param userId userId as a key 
   * @param token token as a value
   */
  updateToken(userId, token) {
    // we can change this function to request our backend service
    this.angularFireAuth.authState.pipe(take(1)).subscribe(
      () => {
        const data = {};
        data[userId] = token
        this.angularFireDB.object('fcmTokens/').update(data)
      })
  }

  /**
   * request permission for notification from firebase cloud messaging
   * 
   * @param userId userId
   */
  requestPermission(userId) {
     console.log(userId,"userId");
    this.angularFireMessaging.requestToken.subscribe(
      (token) => {
         console.log(userId,"userId");
        console.log(token);
        this.updateToken(userId, token);
      },
      (err) => {
        console.error('Unable to get permission to notify.', err);
      }
    );
  }

  /**
   * hook method when new notification received in foreground
   */
  receiveMessage() {
    this.angularFireMessaging.messages.subscribe(
      (payload) => {
        console.log("new message received. ", payload);
        this.currentMessage.next(payload);
      })
  }
}

显示您的messaging.service.tsits的代码您可以检查一下。您是否允许在web浏览器中使用推送通知?此处没有实际体验,因此我以评论的形式发布。Web通知需要Service Worker,错误消息表示找不到Service Worker脚本(404)。您是否遵循了需要创建(或至少从文档中复制)名为
firebase messaging sw.js
的附加资产的步骤?是的。弹出窗口出现。显示您的messaging.service.tsits添加的代码。您是否允许在web浏览器中推送通知?这里没有实际经验,所以我把它作为评论发布。Web通知需要Service Worker,错误消息表示找不到Service Worker脚本(404)。您是否遵循了名为
firebase messaging sw.js
的部分中的步骤,尤其是必须创建额外资产(或至少从文档中复制)的步骤?是的,我遵循了。弹出窗口出现了。