javascript - FCM 推送通知自动关闭

标签 javascript angularjs firebase-cloud-messaging

我已经为我的 Angular Web 应用程序设置了 FCM 通知,该通知将在特定时间段从后端触发。

触发通知后,它将保持打开状态几秒钟,然后自动关闭。

有没有办法将通知设置为保持打开状态,直到我单击它或关闭它?

::编辑::

根据要求,这是我的脚本中的简化代码。我不太确定是否有一个设置可以让通知保持打开状态,直到对其采取操作。

self.addEventListener('push', function(event) {  
    console.log('Push message received', event);
    console.log('Started', self);

    event.waitUntil(registration.pushManager.getSubscription()
        .then(function(subscription){
            fetch("MY_ENDPOINT").then(function(response) {
                response.json().then(function(data){
                  var title = "Random Title";
                  self.registration.showNotification(title, {  
                    body: "body",
                    icon: "icon",  
                    tag: "tag"
                  });
            }).catch(function(err) {
                console.error("Unable to retrieve data", err);
                var title = "Something went wrong!";
                return self.registration.showNotification(title, {  
                    body: "body",  
                    icon: "icon",  
                    tag: "tag"
                });  
            })
        })
    );
});

最佳答案

要保持通知处于打开状态,您必须发送一条纯数据消息并显示您自己的通知,然后您可以使用 Web API 的 requireInteraction 参数来保持通知可见。

注意:桌面浏览器上的通知只会在一段时间后消失,Android 上不会出现这种情况。

代码如下所示:

messaging.setBackgroundMessageHandler(function(payload) {
  console.log('[firebase-messaging-sw.js] Received background message ', payload);
  // Customize notification here
  const notificationTitle = 'Background Message Title';
  const notificationOptions = {
    body: 'Background Message body.',
    icon: '/firebase-logo.png',
    requireInteraction: true
  };

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

了解有关 requireInteraction parameter, I've put some notes here 的更多信息

关于javascript - FCM 推送通知自动关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40194060/

相关文章:

javascript - AngularJS 在 IE8 中未定义

android - Firebase 云消息传递如何在 Android 上运行

javascript - LightBox 无法打开 - 可能是一些 JavaScript 错误

javascript - 谷歌浏览器不在导航中保存 Javascript

AngularJs Material Design RTL 布局

javascript - 在 Firestore 中使用 Firebase Cloud 函数推送通知

android - Firebase 通知 : Easiest way to implement opt-out

javascript - AngularJS $routeProvider 无法直接加载到部分

javascript - 如何使用 JavaScript 格式化电话字符

javascript - Angularjs promise 拒绝链接