【问题标题】:Can't get notification payload from firebase-sw on Ionic PWA无法从 Ionic PWA 上的 firebase-sw 获取通知有效负载
【发布时间】:2018-06-21 14:54:27
【问题描述】:

我已经为这个问题进行了很多搜索,但我已经坚持了一周。 我有一个 Ionic PWA 项目,它接收来自 firebase 的一些通知,我可以毫无问题地接收通知,并且可以在前台获取有效负载(应用程序已打开),但是当应用程序关闭时我无法获取有效负载并且我没有搞清楚是怎么回事,所以特地来请高手帮帮我。

messaging.setBackgroundMessageHandler(function(payload) {
    var notificationTitle = 'Teste';
    var notificationOptions = {
      body: 'Background Message body.',
      tag: 'campanhas',
      data: payload.data,
      icon: 'https://firebasestorage.googleapis.com/v0/b/hemotomobile-edeb9.appspot.com/o/logo.png?alt=media&token=4a9fc487-b8cf-4d3c-875c-774454ff6f50'
   };


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

self.addEventListener('notificationclick', function(event) {
   event.notification.close();

   event.waitUntil(clients.matchAll({
     type: "window"
   }).then(function(clientList) {
     for (var i = 0; i < clientList.length; i++) {
       var client = clientList[i];
       if (client.url == '/' && 'focus' in client)
       return client.focus();
     }
     if (clients.openWindow)
      return clients.openWindow('/');
   }));
});

在我的通知提供商上,我使用以下代码:

public receiveMessage() {
  console.log('notification')
  this.messaging.onMessage((payload) => {
    console.log('payload',payload)
  });
}

我在标签页上调用此提供程序:

ionViewDidLoad(){
  this.notification.receiveMessage()
}

那么,当 PWA 关闭时,谁能帮我获取有效载荷?

【问题讨论】:

    标签: angularjs firebase ionic3 service-worker pwa


    【解决方案1】:

    您是否尝试过在 service worker 中初始化 firebase 并使用它,

    self.addEventListener("notificationclick", (event) => {
       //your code 
    });
    
    importScripts("https://www.gstatic.com/firebasejs/4.7.0/firebase.js")
    importScripts("https://www.gstatic.com/firebasejs/4.7.0/firebase-messaging.js")
    
    // Initialize Firebase
    firebase.initializeApp({
    'messagingSenderId': 'your sender id'
    });
    
    const messaging = firebase.messaging();
    messaging.setBackgroundMessageHandler(function(payload) {
      //your code
    });
    
    this.messaging.onMessage((payload) => {
       //your code
    });
    
    self.addEventListener('notificationclose', function (event) {
      self.registration.getNotifications().then(function (notifications) {
         notifications.forEach(function (notification) {
             notification.close()
         })
      });
    })
    

    我已经尝试在服务工作者中使用上面的代码,它会在应用程序关闭时显示通知,如果您想在浏览器关闭时显示通知,请确保您已启用 继续在 Chrome 设置中关闭 Google Chrome 时运行后台应用程序选项。

    【讨论】:

    • 您好,感谢您的回答。但是当我单击通知时,问题是读取有效负载。我可以在我的应用程序关闭时显示通知,但我无法在 Ionic 应用程序上获取有效负载。有一种方法可以将一些数据从 Javascript 文件传递​​到 Angular?
    • 正如您所说,在通知单击时您无法获取有效负载,看来firebase消息传递库中存在一些问题,
    • 试试github.com/firebase/quickstart-js/issues/102,你可以参考哪个说在我们加载firebase导入脚本之前定义通知关闭的addEvent监听器,这就是为什么在我添加的代码中提到的原因在代码开头添加了通知关闭事件监听器
    • 点击事件工作正常,但打开 PWA 时无法获取有效负载数据。我可以在 firebase-service-worker 的 console.log 上看到有效负载数据,但我无法在我的 Angular 项目中使用这些数据
    猜你喜欢
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 2019-04-22
    • 2019-08-16
    • 1970-01-01
    • 2017-07-23
    相关资源
    最近更新 更多