-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59ce31b
commit d532b27
Showing
10 changed files
with
232 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { ActivationStateMachine } from 'plugins/push/pushactivation'; | ||
|
||
function toBase64Url(arrayBuffer: ArrayBuffer) { | ||
const buffer = new Uint8Array(arrayBuffer.slice(0, arrayBuffer.byteLength)); | ||
return btoa(String.fromCharCode.apply(null, Array.from(buffer))); | ||
} | ||
|
||
function urlBase64ToBase64(base64String: string) { | ||
const padding = '='.repeat((4 - (base64String.length % 4)) % 4); | ||
const base64 = (base64String + padding).replace(/-/g, '+').replace(/_/g, '/'); | ||
return base64; | ||
} | ||
|
||
function base64ToUint8Array(base64String: string) { | ||
const rawData = window.atob(base64String); | ||
const rawDataChars = []; | ||
for (let i = 0; i < rawData.length; i++) { | ||
rawDataChars.push(rawData[i].charCodeAt(0)); | ||
} | ||
return Uint8Array.from(rawDataChars); | ||
} | ||
|
||
export async function getW3CPushDeviceDetails(machine: ActivationStateMachine) { | ||
const GettingPushDeviceDetailsFailed = machine.GettingPushDeviceDetailsFailed; | ||
const GotPushDeviceDetails = machine.GotPushDeviceDetails; | ||
const { ErrorInfo, Defaults } = machine.client; | ||
|
||
const permission = await Notification.requestPermission(); | ||
|
||
if (permission !== 'granted') { | ||
machine.handleEvent( | ||
new GettingPushDeviceDetailsFailed(new ErrorInfo('User denied permission to send notifications', 400, 40000)), | ||
); | ||
return; | ||
} | ||
|
||
const swUrl = machine.client.options.pushServiceWorkerUrl; | ||
if (!swUrl) { | ||
machine.handleEvent( | ||
new GettingPushDeviceDetailsFailed(new ErrorInfo('Missing ClientOptions.pushServiceWorkerUrl', 400, 40000)), | ||
); | ||
return; | ||
} | ||
|
||
try { | ||
const worker = await navigator.serviceWorker.register(swUrl); | ||
|
||
machine._pushManager = worker.pushManager; | ||
|
||
const headers = Defaults.defaultGetHeaders(machine.client.options, { format: 'text' }); | ||
const appServerKey = ( | ||
await machine.client.rest.Resource.get(machine.client, '/push/publicVapidKey', headers, {}, null, true) | ||
).body as string; | ||
|
||
if (!worker.active) { | ||
await navigator.serviceWorker.ready; | ||
} | ||
|
||
const subscription = await worker.pushManager.subscribe({ | ||
userVisibleOnly: true, | ||
applicationServerKey: base64ToUint8Array(urlBase64ToBase64(appServerKey)), | ||
}); | ||
|
||
const endpoint = subscription.endpoint; | ||
|
||
const [p256dh, auth] = [subscription.getKey('p256dh'), subscription.getKey('auth')]; | ||
|
||
if (!p256dh || !auth) { | ||
throw new ErrorInfo('Public key not found', 50000, 500); | ||
} | ||
|
||
const device = machine.client.device; | ||
device.push.recipient = { | ||
transportType: 'web', | ||
targetUrl: btoa(endpoint), | ||
encryptionKey: { | ||
p256dh: toBase64Url(p256dh), | ||
auth: toBase64Url(auth), | ||
}, | ||
}; | ||
device.persist(); | ||
|
||
machine.handleEvent(new GotPushDeviceDetails()); | ||
} catch (err) { | ||
machine.handleEvent( | ||
new GettingPushDeviceDetailsFailed(new ErrorInfo('Failed to register service worker', 50000, 500, err as Error)), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
'use strict'; | ||
|
||
define(['ably', 'shared_helper', 'chai', 'push'], function (Ably, helper, chai, PushPlugin) { | ||
const expect = chai.expect; | ||
const whenPromiseSettles = helper.whenPromiseSettles; | ||
const swUrl = '/push_sw.js'; | ||
let rest; | ||
|
||
const persistKeys = { | ||
deviceId: 'ably.push.deviceId', | ||
deviceSecret: 'ably.push.deviceSecret', | ||
deviceIdentityToken: 'ably.push.deviceIdentityToken', | ||
pushRecipient: 'ably.push.pushRecipient', | ||
activationState: 'ably.push.activationState', | ||
}; | ||
|
||
const messageChannel = new MessageChannel(); | ||
|
||
/** | ||
* These tests don't work in CI for various reasons (below) but should work when running locally via `npm run test:webserver`, provided | ||
* that you have enabled notification permissions for the origin serving the test ui. | ||
* | ||
* chromium, firefox - don't support push notifications in incognito | ||
* webkit - doesn't have a way to launch programatically with notification permissions granted | ||
*/ | ||
if (Notification.permission === 'granted') { | ||
describe('browser/push', function () { | ||
this.timeout(60 * 1000); | ||
|
||
before(function (done) { | ||
helper.setupApp(function () { | ||
rest = helper.AblyRest({ | ||
pushServiceWorkerUrl: swUrl, | ||
plugins: { Push: PushPlugin }, | ||
}); | ||
done(); | ||
}); | ||
}); | ||
|
||
beforeEach(async function () { | ||
Object.values(persistKeys).forEach((key) => { | ||
Ably.Realtime.Platform.Config.push.storage.remove(key); | ||
}); | ||
|
||
const worker = await navigator.serviceWorker.getRegistration(swUrl); | ||
|
||
if (worker) { | ||
await worker.unregister(); | ||
} | ||
}); | ||
|
||
afterEach(async function () { | ||
await rest.push.deactivate(); | ||
}); | ||
|
||
/* | ||
* RSH2a | ||
*/ | ||
it('push_activation_succeeds', async function () { | ||
await rest.push.activate(); | ||
expect(rest.device.deviceIdentityToken).to.be.ok; | ||
}); | ||
|
||
// no spec item | ||
it('direct_publish_device_id', async function () { | ||
await rest.push.activate(); | ||
|
||
const pushRecipient = { | ||
deviceId: rest.device.id, | ||
}; | ||
|
||
const pushPayload = { | ||
notification: { title: 'Test message', body: 'Test message body' }, | ||
data: { foo: 'bar' }, | ||
}; | ||
|
||
const sw = await navigator.serviceWorker.getRegistration(swUrl); | ||
|
||
sw.active.postMessage({ type: 'INIT_PORT' }, [messageChannel.port2]); | ||
|
||
const receivedPushPayload = await new Promise((resolve, reject) => { | ||
messageChannel.port1.onmessage = function (event) { | ||
resolve(event.data.payload); | ||
}; | ||
|
||
rest.push.admin.publish(pushRecipient, pushPayload).catch(reject); | ||
}); | ||
|
||
expect(receivedPushPayload.data).to.deep.equal(pushPayload.data); | ||
expect(receivedPushPayload.notification.title).to.equal(pushPayload.notification.title); | ||
expect(receivedPushPayload.notification.body).to.equal(pushPayload.notification.body); | ||
}); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
let port; | ||
|
||
self.addEventListener('push', (event) => { | ||
const res = event.data.json(); | ||
port.postMessage({ payload: res }); | ||
}); | ||
|
||
self.addEventListener('message', (event) => { | ||
if (event.data.type === 'INIT_PORT') { | ||
port = event.ports[0]; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters