Skip to content

Commit

Permalink
FCM Pre Modularization (#3234)
Browse files Browse the repository at this point in the history
* FCM Pre Modualization

* Refactor FCM typing

* Polish javascript doc

* Update index.d.ts

* Add Changeset

* Fix changeset from major to minor

* Add to changeset

* Polished index.d.ts based on Arthur's feedback

* Polish PR based on feedbacks

* Update Changeset

* Fix wording

* Polishing
  • Loading branch information
zwu52 committed Aug 11, 2020
1 parent 46fd70b commit 29327b2
Show file tree
Hide file tree
Showing 47 changed files with 1,435 additions and 688 deletions.
10 changes: 10 additions & 0 deletions .changeset/strange-crabs-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'firebase': minor
'@firebase/messaging': minor,
'@firebase/messaging-types': minor
---

Add `getToken(options:{serviceWorkerRegistration, vapidKey})`,`onBackgroundMessage`.
Deprecate `setBackgroundMessageHandler`, `onTokenRefresh`, `useVapidKey`, `useServiceWorker`, `getToken`.

Add Typing `MessagePayload`, `NotificationPayload`, `FcmOptions`.
5 changes: 3 additions & 2 deletions integration/messaging/download-browsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
const seleniumAssistant = require('selenium-assistant');

console.log('Starting browser download - this may take some time.');
// TODO: enable firefox testing once figure out how to give notification permission with SE webdriver.
// TODO: Run the integration test against multiple major chrome versions to ensure backward compatibility
// TODO: enable firefox testing once figure out how to give notification permission with SE
// webdriver. TODO: Run the integration test against multiple major chrome versions to ensure
// backward compatibility
Promise.all([seleniumAssistant.downloadLocalBrowser('chrome', 'stable', 80)])
.then(() => {
console.log('Browser download complete.');
Expand Down
2 changes: 1 addition & 1 deletion integration/messaging/manual-test-server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2017 Google Inc.
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
52 changes: 52 additions & 0 deletions integration/messaging/test/static/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

async function addPayloadToDb(payload) {
const dbOpenReq = indexedDB.open(TEST_DB);

dbOpenReq.onupgradeneeded = () => {
const db = dbOpenReq.result;

// store creation is a synchronized call
console.log('creating object store...');
db.createObjectStore(BACKGROUND_MESSAGES_OBJECT_STORE, {
keyPath: BACKGROUND_MESSAGES_OBJECT_STORE_PRIMARY_KEY
});
};

dbOpenReq.onsuccess = () => {
const db = dbOpenReq.result;

addPayloadToDbInternal(db, {
...payload,
// ndx is required as the primary key of the store. It doesn't have any other testing purpose
ndx: BACKGROUND_MESSAGES_OBJECT_STORE_DEFAULT_NDX
});
};
}

async function addPayloadToDbInternal(db, payload) {
// onsuccess might race with onupgradeneeded. Consequently causing "object stores was not found"
// error. Therefore, wait briefly for db.createObjectStore to complete
const delay = ms => new Promise(res => setTimeout(res, ms));
await delay(/* milliseconds= */ 30000);

tx = db.transaction(BACKGROUND_MESSAGES_OBJECT_STORE, 'readwrite');

console.log('adding message payload to db: ' + JSON.stringify(payload));
addReq = tx.objectStore(BACKGROUND_MESSAGES_OBJECT_STORE).add(payload);
}
38 changes: 2 additions & 36 deletions integration/messaging/test/static/sw-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

importScripts('../constants.js');
importScripts('../helpers.js');

// HEAD targets served through express
importScripts('/firebase-app.js');
Expand All @@ -27,45 +28,10 @@ const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(payload => {
console.log(
TAG +
'a background message is received: ' +
'a background message is received in the background handler hook: ' +
JSON.stringify(payload) +
'. Storing it into idb for tests to read...'
);

addPayloadToDb(payload);
});

async function addPayloadToDb(payload) {
const dbOpenReq = indexedDB.open(TEST_DB);

dbOpenReq.onupgradeneeded = () => {
const db = dbOpenReq.result;

// store creation is a synchronized call
console.log('creating object store...');
db.createObjectStore(BACKGROUND_MESSAGES_OBJECT_STORE, {
keyPath: BACKGROUND_MESSAGES_OBJECT_STORE_PRIMARY_KEY
});
};

dbOpenReq.onsuccess = () => {
const db = dbOpenReq.result;

addPayloadToDbInternal(db, {
...payload,
// ndx is required as the primary key of the store. It doesn't have any other testing purpose
ndx: BACKGROUND_MESSAGES_OBJECT_STORE_DEFAULT_NDX
});
};
}

async function addPayloadToDbInternal(db, payload) {
// onsuccess might race with onupgradeneeded. Consequently causing " object stores was not found" error. Therefore, wait briefly for db.createObjectStore to complete
const delay = ms => new Promise(res => setTimeout(res, ms));
await delay(/* milliseconds= */ 30000);

tx = db.transaction(BACKGROUND_MESSAGES_OBJECT_STORE, 'readwrite');

console.log('adding message payload to db: ' + JSON.stringify(payload));
addReq = tx.objectStore(BACKGROUND_MESSAGES_OBJECT_STORE).add(payload);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<html>
<head>
<title>FCM Demo</title>
<meta name="viewport" content="width=device-width,initial-scale=1" />
</head>
<body>
<h1>Valid <strong>WITH</strong> VAPID Key - Modern SW</h1>

<script src="/firebase-app.js"></script>
<script src="/firebase-messaging.js"></script>
<script src="../app.js"></script>
<script src="../constants.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sinon.js/4.1.3/sinon.min.js"></script>
<script>
navigator.serviceWorker
.register('./sw.js')
.then(reg => {
window.__test = new window.DemoApp(FIREBASE_CONFIG, {
swReg: reg,
vapidKey: PUBLIC_VAPID_KEY
});
})
.catch(error => {
console.log('Error registering FCM SW: ' + error);
});
</script>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@
* limitations under the License.
*/

import { MessagePayload } from './message-payload';
importScripts('../constants.js');
importScripts('../helpers.js');

export enum MessageType {
PUSH_RECEIVED = 'push-received',
NOTIFICATION_CLICKED = 'notification-clicked'
}
// HEAD targets served through express
importScripts('/firebase-app.js');
importScripts('/firebase-messaging.js');

export interface InternalMessage {
firebaseMessaging: {
type: MessageType;
payload: MessagePayload;
};
}
firebase.initializeApp(FIREBASE_CONFIG);
const messaging = firebase.messaging();

messaging.onBackgroundMessage(payload => {
console.log(
TAG +
'a background message is received in the onBackgroundMessage hook: ' +
JSON.stringify(payload) +
'. Storing it into idb for tests to read...'
);

addPayloadToDb(payload);
});
2 changes: 1 addition & 1 deletion integration/messaging/test/test-deleteToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('Firebase Messaging Integration Tests > get and delete token', function

const availableBrowsers = seleniumAssistant.getLocalBrowsers();
availableBrowsers.forEach(assistantBrowser => {
//TODO: enable testing for firefox
// TODO: enable testing for firefox
if (assistantBrowser.getId() !== 'chrome') {
return;
}
Expand Down

0 comments on commit 29327b2

Please sign in to comment.