Skip to content

Commit 32e19a0

Browse files
authored
Fix for OneSignal init in Chrome 69 on http (#405)
1 parent 890e264 commit 32e19a0

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ export function isPushNotificationsSupported() {
5555
const browser = redetectBrowserUserAgent();
5656
let userAgent = navigator.userAgent || '';
5757

58+
// Chrome 69+ returns undefined for serviceWorker in insecure context but our workaround will work.
59+
// Returning true.
60+
if ((browser.chrome || (<any>browser).chromium) &&
61+
window.isSecureContext === false && typeof navigator.serviceWorker === "undefined") {
62+
return true;
63+
}
64+
5865
if (!browser.safari && typeof navigator.serviceWorker === "undefined") {
5966
/**
6067
* Browsers like Firefox Extended Support Release don't support service workers

test/support/sdk/TestEnvironment.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export enum BrowserUserAgent {
5656
ChromeAndroidSupported = "Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/42 Mobile Safari/535.19",
5757
ChromeWindowsSupported = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2228.0 Safari/537.36",
5858
ChromeMacSupported = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.1636.0 Safari/537.36",
59+
ChromeMacSupported69 = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36",
5960
ChromeLinuxSupported = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.1636.0 Safari/537.36",
6061
ChromeTabletSupported = "Mozilla/5.0 (Linux; Android 4.3; Nexus 10 Build/JWR66Y) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.1547.72 Safari/537.36",
6162
ChromeAndroidUnsupported = "Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/41 Mobile Safari/535.19",
@@ -166,10 +167,13 @@ export class TestEnvironment {
166167
if (!config)
167168
config = {};
168169
let url: string | undefined = undefined;
170+
let isSecureContext: boolean | undefined = undefined;
169171
if (config.httpOrHttps == HttpHttpsEnvironment.Http) {
170172
url = 'http://localhost:3000/webpush/sandbox?http=1';
173+
isSecureContext = false;
171174
} else {
172175
url = 'https://localhost:3001/webpush/sandbox?https=1';
176+
isSecureContext = true;
173177
}
174178
if (config.url) {
175179
url = config.url.toString();
@@ -212,6 +216,7 @@ export class TestEnvironment {
212216
const { TextEncoder, TextDecoder } = require('text-encoding');
213217
(windowDef as any).TextEncoder = TextEncoder;
214218
(windowDef as any).TextDecoder = TextDecoder;
219+
(windowDef as any).isSecureContext = isSecureContext;
215220
TestEnvironment.addCustomEventPolyfill(windowDef);
216221

217222
let topWindow = config.initializeAsIframe ? {

test/unit/modules/browserSupport.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { isPushNotificationsSupported } from "../../../src/utils";
55
import { setUserAgent } from '../../support/tester/browser';
66

77

8-
function shouldSupport(t, userAgent: BrowserUserAgent) {
8+
function shouldSupport(t: any, userAgent: BrowserUserAgent) {
99
setUserAgent(userAgent);
1010
t.true(isPushNotificationsSupported(), `Expected ${BrowserUserAgent[userAgent]} to be supported`)
1111
}
12-
function shouldNotSupport(t, userAgent: BrowserUserAgent) {
12+
function shouldNotSupport(t: any, userAgent: BrowserUserAgent) {
1313
setUserAgent(userAgent);
1414
t.false(isPushNotificationsSupported(), `Expected ${BrowserUserAgent[userAgent]} to be unsupported`)
1515
}
@@ -95,9 +95,27 @@ test('should not support environments without service workers (except Safari)',
9595
writable: true,
9696
value: undefined
9797
});
98+
9899
setUserAgent(BrowserUserAgent.ChromeMacSupported);
99100
t.false(isPushNotificationsSupported(), `Expected push to be unsupported if service workers don't exist in a non-Safari browser`);
100101

101102
setUserAgent(BrowserUserAgent.SafariSupportedMac);
102103
t.true(isPushNotificationsSupported(), `Expected push to be supported if service workers don't exist in Safari`)
103104
});
105+
106+
test('should support Chrome 69+ on http', async t => {
107+
(global as any).BrowserUserAgent = BrowserUserAgent;
108+
await TestEnvironment.stubDomEnvironment({
109+
httpOrHttps: HttpHttpsEnvironment.Http
110+
});
111+
// Remove serviceWorker from navigator for testing
112+
Object.defineProperty(navigator, 'serviceWorker', {
113+
enumerable: true,
114+
configurable: true,
115+
writable: true,
116+
value: undefined
117+
});
118+
119+
setUserAgent(BrowserUserAgent.ChromeMacSupported69);
120+
t.true(isPushNotificationsSupported(), `Expected push to be supported in Chrome 69 on http`);
121+
});

0 commit comments

Comments
 (0)