-
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
7156282
commit e1ff454
Showing
7 changed files
with
202 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { ActivationStateMachine, LocalDevice } from 'plugins/push/pushactivation'; | ||
import Resource from 'common/lib/client/resource'; | ||
import ErrorInfo from 'common/lib/types/errorinfo'; | ||
import Defaults from 'common/lib/util/defaults'; | ||
|
||
function toBase64Url(arrayBuffer: ArrayBuffer) { | ||
const buffer = new Uint8Array(arrayBuffer.slice(0, arrayBuffer.byteLength)); | ||
return btoa(String.fromCharCode.apply(null, Array.from(buffer))); | ||
} | ||
|
||
function urlBase64ToUint8Array(base64String: string) { | ||
const padding = '='.repeat((4 - (base64String.length % 4)) % 4); | ||
const base64 = (base64String + padding).replace(/-/g, '+').replace(/_/g, '/'); | ||
const rawData = window.atob(base64); | ||
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 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.rest.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.rest.options, { format: 'text' }); | ||
const appServerKey = (await Resource.get(machine.rest, '/push/publicVapidKey', headers, {}, null, true)) | ||
.body as string; | ||
|
||
if (!worker.active) { | ||
await navigator.serviceWorker.ready; | ||
} | ||
|
||
const subscription = await worker.pushManager.subscribe({ | ||
userVisibleOnly: true, | ||
applicationServerKey: urlBase64ToUint8Array(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 key = [p256dh, auth].map(toBase64Url).join(':'); | ||
|
||
const device = machine.rest.device as LocalDevice; | ||
device.push.recipient = { | ||
transportType: 'web', | ||
targetUrl: btoa(endpoint), | ||
encryptionKey: key, | ||
}; | ||
device.persist(); | ||
|
||
machine.handleEvent(new GotPushDeviceDetails()); | ||
} catch (err) { | ||
machine.handleEvent( | ||
new GettingPushDeviceDetailsFailed( | ||
new ErrorInfo('failed to register service worker', 50000, 500, err as Error | ErrorInfo), | ||
), | ||
); | ||
} | ||
} |
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,83 @@ | ||
'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(); | ||
|
||
describe('browser/push', function () { | ||
this.timeout(60 * 1000); | ||
|
||
before(function (done) { | ||
helper.setupApp(function () { | ||
done(); | ||
}); | ||
|
||
rest = helper.AblyRest({ | ||
pushServiceWorkerUrl: swUrl, | ||
plugins: { push: PushPlugin }, | ||
}); | ||
}); | ||
|
||
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(); | ||
}); | ||
|
||
it('push_activation_succeeds', async function () { | ||
await rest.push.activate(); | ||
expect(rest.device.deviceIdentityToken).to.be.ok; | ||
}); | ||
|
||
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