0

ブラウザが開いているときに、Gcm プッシュ通知メッセージがエンドポイントに正しく送信されます: json ファイルにある通知メッセージ。

serviceWorker.js

    'use strict';
self.addEventListener('install', function(event) {
    self.skipWaiting();
    console.log('Installed', event);
});

    self.addEventListener('activate', function(event) {

    console.log('Activated', event);
});
self.addEventListener('push', function(event) {
    console.log('Started', self);
    self.addEventListener('install', function(event) {
        self.skipWaiting();
    });

    self.addEventListener('activate', function(event) {
        console.log('Activated', event);
    });

    self.addEventListener('push', function(event) {
        var url = "http://localhost/pntest/gmpush1.json?param="+Math.random();
        event.waitUntil(
            fetch(url).then(function(response) {
                if (response.status !== 200) {
                    console.log('Problem. Status Code: ' + response.status);
                    throw new Error();
                }
                // Examine the text in the response
                return response.json().then(function(data) {

                    if (data.error || !data.notification) {
                        console.error('The API returned an error.', data.error);
                        throw new Error();
                    }

                    var promises = [];
                            for(var i=0; data.notification && i < data.notification.length; i++) {
                                promises.push(self.registration.showNotification(data.notification[i].title, {
                                    body: data.notification[i].body,
                                    'renotify': true,
                                    icon: data.notification[i].icon
                                    //tag: notification.tag
                                }));
                            }
                            return Promise.all( promises );
                });
            })
        );
    });

    self.addEventListener('notificationclick', function(event) {
        console.log('Notification click: tag ', event.notification.tag);
        event.notification.close();
        var newurl = event.notification.data.newurl;
        console.log(newurl.updatedurl);
        var url = newurl.updatedurl;
        event.waitUntil(
            clients.matchAll({
                type: 'window'
            })
            .then(function(windowClients) {
                console.log(url);
                for (var i = 0; i < windowClients.length; i++) {
                    var client = windowClients[i];
                    if (client.url === url && 'focus' in client) {
                        return client.focus();
                    }
                }
                if (clients.openWindow) {
                    return clients.openWindow(url);
                }
            })
        );

    });
});    

gcmpush1.json

{"notification": [{"body": "Test data", "url": "https://www.google.com/", "icon": "http://www.wired.com/wp-content/uploads/2015/09/google-logo-1200x630.jpg", "title": "Test Notification"}]}

ブラウザを開くと、元のメッセージが表示されます

テスト通知

curl トリガー中にクライアント ブラウザーがオフライン (開かれていない) の場合。クライアントブラウザを再度開くと、元のメッセージが表示されるはずですが、取得しているのは

サイトはバックグラウンドで更新されました

curl 呼び出しでは、'time_to_live' = 2419200 を使用しました。

4

1 に答える 1