无法存储indexDB中服务工作者收到的Web通知

时间:2017-08-02 04:45:04

标签: firebase firebase-cloud-messaging service-worker

我正在使用Firebase云消息传递。我正在使用以下示例链接。 https://github.com/firebase/quickstart-js

收到通知。我想在serviceDB中的indexDB中存储通知。

var setupDone = false;
var storage = {
    setup: function() {
        return new Promise(function(resolve, reject) {
            var request = indexedDB.open(notificationdb, 1);
            request.onsuccess = function (e) {
                setupDone = true;
                var db = e.target.result;
                storage.db = db;
                resolve();
            };
            request.onupgradeneeded = function (e) {
                storage.db = e.target.result;
                if (storage.db.objectStoreNames.contains('notifications')) {
                    storage.db.deleteObjectStore('notifications');
                }
                storage.db.createObjectStore('notifications', {
                    keyPath: '_id'
                });
            };
        });
    },

    transaction: function (mode) {
        var trans = storage.db.transaction('notifications', mode);
        return trans.objectStore('notifications');
    },

    add: function (notification) {
        return new Promise(function(resolve, reject) {
            var request = 
            storage.transaction('readwrite').put(notification);
            request.onsuccess = function () {
                resolve(notification);
            };
            request.onerror = function() {
                reject();
            };
        });
    }
};
 messaging.setBackgroundMessageHandler(function(payload) {
       var notification = payload.notification;
       var data = {};
       data.title = notification.title;
       data.message = notification.body;
       data._id = getUniqueId();
      data.time = Date.now();
      if (!setupDone) {
        storage.setup().then(function() {
            storage.add(data);
        });
       } else {
        storage.add(data);
       }
     return 
      self.registration.showNotification(notification.title,notification.body);
  });

但它没有存储在indexDB中。数据库已创建。但是没有存储数据。如果这里有任何问题请帮忙。

0 个答案:

没有答案