0

We are developing a chrome extension that utilizes the listener on chrome.gcm.onMessage. When a message is received, we want to store some details about this message in an array in chrome.storage.local. However, due to the asynchronous nature of how this works, if we receive two+ notifications at the same time, the array isn't always updated successfully, with only one of the notifications saving.

Below is the function called when the notification is received. The notification object being passed is a copy of the notification object created for gcm push notification (a javascript object).

 function getRecentNotifications(notification) {
  //array to hold the notifications
  var notificationArray = [];
  //array to hold result (used in method when page loads)
  var results = [];
  //get the stored notifications if we have any
  chrome.storage.local.get('notifications', function(result) {
    if(result.notifications != null) {
      console.log("We have results "+result.notifications.length);
      notificationArray = result.notifications;
      notificationArray.unshift({notification});
    }
    else {
      console.log("No results");
      notificationArray = [{notification}];
    }
    chrome.storage.local.set({notifications:notificationArray});
  });
GeorgeA
  • 153
  • 6

0 Answers0