使用 Firebase 的 Service Worker 中推送通知显示两次

Nis*_*nja 5 push-notification firebase firebase-cloud-messaging

当应用程序在后台运行时,我收到两次通知。这是我的服务人员文件。当我们调用此firebase.messaging()函数时,服务工作线程开始自动接收并显示推送通知。服务人员正在显示这两个通知。一种由 firebase 初始化,另一种使用此代码初始化self.registration.showNotification(notificationTitle, notificationOptions)

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

  // Initialize the Firebase app in the service worker by passing in
  firebase.initializeApp({
    apiKey: "apiKey",
    authDomain: "authDomain",
    projectId: "projectId",
    storageBucket: "storageBucket",
    messagingSenderId: "messagingSenderId",
    appId: "appId"
  
  });

  const messaging = firebase.messaging();

  messaging.onBackgroundMessage( function(payload){
   
    const notificationTitle = payload.notification.title;
    const notificationOptions = {
      body: 'Testing',
    };

    return self.registration.showNotification(notificationTitle, notificationOptions);

  });
Run Code Online (Sandbox Code Playgroud)

如何防止默认通知?