1

サーバーから複数の通知を取得する Service Worker があります。問題は、最後の通知を除いて、Chrome のすべての通知が自動的に閉じられることです。私は何を間違っていますか?

self.addEventListener('push', function(event) {
var subscriptionId;
var sessionId;
var notification = {};

event.waitUntil(
    self.registration.pushManager.getSubscription().then(function(subscription) {
        subscriptionId = subscription.endpoint.split('/');
        subscriptionId = subscriptionId[subscriptionId.length - 1];

        notification.title = 'Yay a message.';
        notification.icon = '/app/img/icon-256x256.png';
        notification.tag = 'notification-tag-' + (new Date)*1;
        notification.messages = [];

        //get context
        fetch(requestUrl,
            {
                method: 'post',
                body: body
            }).then(function(response) {
                response.json().then(function(data) {
                    sessionId = response.headers.get('SessionId');
                    fetch(requestUrl + '?SessionId=' + sessionId, {
                        method: 'post',
                        headers: JSON.stringify({
                            'Content-Type': 'application/json'
                        }),
                        body: JSON.stringify({
                            data: {
                                subscriberId: subscriptionId
                            }
                        })
                    }).then(function(responce) {
                        responce.json().then(function(data) {
                            data = data.data;
                            if (!data.chromeNotifications || !data.chromeNotifications.length) return;
                            data.chromeNotifications.forEach(function(push) {
                                notification.messages.push(push.message);
                            });
                        }).then(function() {
                            var promises = [];
                            for(var i=0; notification.messages && i<notification.messages.length; i++) {
                                promises.push(self.registration.showNotification(notification.title, {
                                    body: notification.messages[i],
                                    icon: notification.icon,
                                    tag: notification.tag
                                }));
                            }
                            return Promise.all( promises );
                        });
                    });
                })
            });
    }).catch(function(error) {
        console.error('Unable to get subscription data', error);
        var title = 'An error occurred';
        var message = 'We were unable to get the information for this push message';
        var notificationTag = 'notification-error';
        return self.registration.showNotification(title, {
            body: message,
            tag: notificationTag
        });
    })
);
4

1 に答える 1

5

保留中の通知をサーバーから取得する直前に、例の 13 行目tagで、着信プッシュ メッセージに対して一意になるように once を設定します。これtagは、47 行目で、サーバーからフェッチされた各メッセージに使用されます。

tag通知のを使用して、作成時に通知がtag新しい通知を表示する前に、同じ通知を共有する以前の通知を置き換えようとする必要があることを示すことができます。この場合、以前の通知を繰り返し上書きしています。

tagとにかく一意にしたいので、解決策はまったく使用しないことです。エラー通知用に保持することは理にかなっています。2 回表示してもユーザーの役に立ちません。

于 2015-10-02T19:30:01.543 に答える