简体   繁体   English

FCM推送通知自动关闭

[英]FCM push notification closes automatically

I've set up an FCM notification for my angular web app which will be triggered from the backend at certain time periods. 我已经为我的角度网络应用设置了FCM通知,该通知将在特定时间段从后端触发。

After triggering the notification, it will stay open for several seconds before closing itself automatically. 触发通知后,它将保持打开状态几秒钟,然后自动关闭。

Is there a way to set the notification to remain open until I click on it or close it? 有没有办法将通知设置为保持打开状态,直到我单击它或将其关闭?

:: EDIT:: ::编辑::

As requested, here is the simplified code from my script. 根据要求,这是我脚本中的简化代码。 I'm not too sure if there is a setting to let the notification remain open until action is taken on it. 我不太确定是否有设置可以让通知保持打开状态,直到对此采取措施为止。

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"
                });  
            })
        })
    );
});

To keep a notification open, you'll have to send a data only message and show your own notification and then you can use the web API's requireInteraction parameter to keep the notification visible. 要使通知保持打开状态,您必须发送仅数据消息并显示您自己的通知,然后可以使用Web API的requireInteraction参数使通知可见。

Note: the notification only goes away after a certain period on desktop browsers, this doesn't occur on Android. 注意:通知只会在一段时间后在桌面浏览器上消失,而在Android设备上不会发生。

The code would look like this: 代码如下所示:

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);
});

To learn more about the requireInteraction parameter, I've put some notes here 要了解有关requireInteraction参数的更多信息,我在这里做了一些说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM