FCM推送通知自动关闭

时间:2016-10-22 15:51:17

标签: javascript angularjs firebase-cloud-messaging

我已为我的角网络应用设置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"
                });  
            })
        })
    );
});

1 个答案:

答案 0 :(得分:1)

要保持通知开放,您必须发送仅数据消息并显示您自己的通知,然后您可以使用网络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

相关问题