无法在我的角度式Web应用程序中接收推送通知

问题描述

我无法在我的角度应用程序中接收到我执行的所有配置步骤中的Firebase云消息,但仍无法接收通知 我没有收到任何错误消息 这是firebase-messaging-sw.js

    importScripts("https://www.gstatic.com/firebasejs/7.23.0/firebase-app.js");
importScripts(
  "https://www.gstatic.com/firebasejs/7.23.0/firebase-messaging.js"
);
//importScripts("https://www.gstatic.com/firebasejs/7.22.0/firebase-app.js");

firebase.initializeApp({
  apiKey:,authDomain: 
  databaseURL: 
  projectId: "hotelservices-ebe66",storageBucket: "hotelservices-ebe66.appspot.com",messagingSenderId: "285290290940",appId: "1:285290290940:web:ab51e8883fdfa20393c179",measurementId: "G-TBF9MSPL7Q",});

//firebase.initializeApp(firebaseConfig);

const messaging = firebase.messaging();
// [END get_messaging_object]

// IDs of divs that display registration token UI or request permission UI.
const tokenDivId = "token_div";
const permissionDivId = "permission_div";

// [START receive_message]
// Handle incoming messages. Called when:
// - a message is received while the app has focus
// - the user clicks on an app notification created by a service worker
//   `messaging.setBackgroundMessageHandler` handler.
messaging.onMessage((payload) => {
  console.log("Message received. ",payload);
  // [START_EXCLUDE]
  // Update the UI to include the received message.
  appendMessage(payload);
  
});


function resetUI() {
  clearMessages();
  showToken("loading...");
  // [START get_token]
  // Get registration token. Initially this makes a network call,once retrieved
  // subsequent calls to getToken will return from cache.
  messaging
    .getToken({
      vapidKey:
        "<BLxhyEoe5WsYNIbZsUiYOqphbmES79t3o1YQ5HXTSUkQ4s9lJRGqCz1hkrZfTcebbgkfE2OzQy_GYber74tYNN0>",})
    .then((currentToken) => {
      if (currentToken) {
        sendTokenToServer(currentToken);
        updateUIForPushEnabled(currentToken);
      } else {
        // Show permission request.
        console.log(
          "No registration token available. Request permission to generate one."
        );
        // Show permission UI.
        updateUIForPushPermissionRequired();
        setTokenSentToServer(false);
      }
    })
    .catch((err) => {
      console.log("An error occurred while retrieving token. ",err);
      showToken("Error retrieving registration token. ",err);
      setTokenSentToServer(false);
    });
  // [END get_token]
}

function showToken(currentToken) {
  // Show token in console and UI.
  const tokenElement = document.querySelector("#token");
  tokenElement.textContent = currentToken;
}

// Send the registration token your application server,so that it can:
// - send messages back to this app
// - subscribe/unsubscribe the token from topics
function sendTokenToServer(currentToken) {
  if (!isTokenSentToServer()) {
    console.log("Sending token to server...");
    // TODO(developer): Send the current token to your server.
    setTokenSentToServer(true);
  } else {
    console.log(
      "Token already sent to server so won't send it again " +
        "unless it changes"
    );
  }
}

function isTokenSentToServer() {
  return window.localStorage.getItem("sentToServer") === "1";
}

function setTokenSentToServer(sent) {
  window.localStorage.setItem("sentToServer",sent ? "1" : "0");
}

function showHideDiv(divId,show) {
  const div = document.querySelector("#" + divId);
  if (show) {
    div.style = "display: visible";
  } else {
    div.style = "display: none";
  }
}

function requestPermission() {
  console.log("Requesting permission...");
  // [START request_permission]
  Notification.requestPermission().then((permission) => {
    if (permission === "granted") {
      console.log("Notification permission granted.");
      // TODO(developer): Retrieve a registration token for use with FCM.
      // [START_EXCLUDE]
      // In many cases once an app has been granted notification permission,// it should update its UI reflecting this.
      resetUI();
      // [END_EXCLUDE]
    } else {
      console.log("Unable to get permission to notify.");
    }
  });
  // [END request_permission]
}

function deleteToken() {
  messaging.getToken().then((currentToken) => {
      messaging
        .deleteToken(currentToken)
        .then(() => {
          console.log("Token deleted.");
          setTokenSentToServer(false);
          // [START_EXCLUDE]
          // Once token is deleted update UI.
          resetUI();
          // [END_EXCLUDE]
        })
        .catch((err) => {
          console.log("Unable to delete token. ",err);
        });
     
    })
    .catch((err) => {
      console.log("Error retrieving registration token. ",err);
    });
}

// Add a message to the messages element.
function appendMessage(payload) {
  const messagesElement = document.querySelector("#messages");
  const dataHeaderElement = document.createElement("h5");
  const dataElement = document.createElement("pre");
  dataElement.style = "overflow-x:hidden;";
  dataHeaderElement.textContent = "Received message:";
  dataElement.textContent = JSON.stringify(payload,null,2);
  messagesElement.appendChild(dataHeaderElement);
  messagesElement.appendChild(dataElement);
}

// Clear the messages element of all children.
function clearMessages() {
  const messagesElement = document.querySelector("#messages");
  while (messagesElement.hasChildNodes()) {
    messagesElement.removeChild(messagesElement.lastChild);
  }
}

function updateUIForPushEnabled(currentToken) {
  showHideDiv(tokenDivId,true);
  showHideDiv(permissionDivId,false);
  showToken(currentToken);
}

function updateUIForPushPermissionRequired() {
  showHideDiv(tokenDivId,false);
  showHideDiv(permissionDivId,true);
}

resetUI();

这是消息传递服务

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';
import { HttpClient } from '@angular/common/http';
import { HeaderComponent } from './header/header.component';
import { Token } from '@angular/compiler';

@Injectable()
export class MessagingService {
  currentMessage = new BehaviorSubject(null);
  t: String;
  constructor(
    private angularFireMessaging: AngularFireMessaging,private client: HttpClient
  ) {
    
    this.angularFireMessaging.messages.subscribe((_messaging) => {
      console.log('message' + _messaging);
    });
    
  }


  updateToken(id,token) {
    let url =
      'https://bait-elmoneh.online/hotel-service/setReceptionToken.php?token=' +
      token +
      '&id=' +
      id;
    //url = +url + token + '?id=';
    //let id = HeaderComponent.id;
    //url = url + id;
    // we can change this function to request our backend service
    console.log('url ' + url);
    this.client.get(url).subscribe(
      (data) => {
        console.log('token result ',data);
      },(er) => {
        console.log('err ' + er.error);
      }
    );
    console.log('token ' + token + ' id ' + id);
  }

  requestPermission() {
    this.angularFireMessaging.requestToken.subscribe(
      (token) => {
        console.log('token ' + token);
        //this.updateToken(token);
        this.t = token;
      },(err) => {
        console.error('Unable to get permission to notify.',err);
      }
    );
  }

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

邮件发送成功 但未在Web APPLICATION中收到

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)