-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π onesignal: fix onesignal web being imported on android
- Loading branch information
Showing
3 changed files
with
113 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useState, useEffect, useRef } from "react"; | ||
import { OneSignal as RNOneSignal } from "react-native-onesignal"; | ||
|
||
import { oneSignalAPPId } from "./constants"; | ||
|
||
type OneSignalProperties = { | ||
userId?: string; | ||
}; | ||
|
||
type Instance = typeof RNOneSignal; | ||
|
||
export default function useOneSignal({ userId }: OneSignalProperties) { | ||
const instance = useRef<Instance | null>(null); | ||
const [initialized, setInitialized] = useState(false); | ||
|
||
useEffect(() => { | ||
const load = function () { | ||
if (!oneSignalAPPId) { | ||
setInitialized(true); | ||
return; | ||
} | ||
|
||
if (!initialized) { | ||
RNOneSignal.initialize(oneSignalAPPId); | ||
instance.current = RNOneSignal; | ||
|
||
setInitialized(true); | ||
} | ||
|
||
if (instance.current && userId) { | ||
instance.current.login(userId); | ||
} | ||
}; | ||
|
||
load(); | ||
|
||
return () => { | ||
if (!userId || !instance.current) { | ||
return; | ||
} | ||
|
||
instance.current.logout(); | ||
}; | ||
}, [userId, initialized]); | ||
|
||
return initialized; | ||
} |
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 |
---|---|---|
@@ -1,78 +1,18 @@ | ||
import { useState, useEffect, useRef } from "react"; | ||
import { Platform } from "react-native"; | ||
import { OneSignal as RNOneSignal } from "react-native-onesignal"; | ||
import ROneSignal from "react-onesignal"; | ||
|
||
import { oneSignalAPPId } from "./constants"; | ||
|
||
type OneSignalProperties = { | ||
userId?: string; | ||
}; | ||
|
||
type Instance = | ||
| { | ||
type: "native"; | ||
value: typeof RNOneSignal; | ||
} | ||
| { | ||
type: "web"; | ||
value: typeof ROneSignal; | ||
}; | ||
|
||
export default function useOneSignal({ userId }: OneSignalProperties) { | ||
const instance = useRef<Instance | null>(null); | ||
const [initialized, setInitialized] = useState(false); | ||
|
||
useEffect(() => { | ||
const load = async function () { | ||
if (!oneSignalAPPId) { | ||
setInitialized(true); | ||
return; | ||
} | ||
|
||
if (!initialized) { | ||
switch (Platform.OS) { | ||
case "web": { | ||
await ROneSignal.init({ | ||
appId: oneSignalAPPId, | ||
allowLocalhostAsSecureOrigin: true, | ||
}); | ||
instance.current = { type: "web", value: ROneSignal }; | ||
break; | ||
} | ||
case "ios": | ||
case "android": { | ||
RNOneSignal.initialize(oneSignalAPPId); | ||
instance.current = { type: "native", value: RNOneSignal }; | ||
break; | ||
} | ||
} | ||
|
||
setInitialized(true); | ||
} | ||
|
||
if (instance.current && userId) { | ||
await instance.current.value.login(userId); | ||
} | ||
}; | ||
|
||
load().catch(() => { | ||
setInitialized(true); | ||
}); | ||
|
||
return () => { | ||
if (!userId || !instance.current) { | ||
return; | ||
} | ||
|
||
const logout = instance.current.value.logout(); | ||
if (logout instanceof Promise) { | ||
logout.catch(() => { | ||
// ignore | ||
}); | ||
} | ||
}; | ||
}, [userId, initialized]); | ||
|
||
return initialized; | ||
} | ||
type OneSignal = (parameters: OneSignalProperties) => boolean; | ||
|
||
export default Platform.select<OneSignal>({ | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module | ||
web: require("./onesignal.web") as OneSignal, | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module | ||
android: require("./onesignal.native") as OneSignal, | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module | ||
ios: require("./onesignal.native") as OneSignal, | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module | ||
default: require("./onesignal.native") as OneSignal, | ||
}); |
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,54 @@ | ||
import { useState, useEffect, useRef } from "react"; | ||
import OneSignal from "react-onesignal"; | ||
|
||
import { oneSignalAPPId } from "./constants"; | ||
|
||
type OneSignalProperties = { | ||
userId?: string; | ||
}; | ||
|
||
type Instance = typeof OneSignal; | ||
|
||
export default function useOneSignal({ userId }: OneSignalProperties) { | ||
const instance = useRef<Instance | null>(null); | ||
const [initialized, setInitialized] = useState(false); | ||
|
||
useEffect(() => { | ||
const load = async function () { | ||
if (!oneSignalAPPId) { | ||
setInitialized(true); | ||
return; | ||
} | ||
|
||
if (!initialized) { | ||
await OneSignal.init({ | ||
appId: oneSignalAPPId, | ||
allowLocalhostAsSecureOrigin: true, | ||
}); | ||
instance.current = OneSignal; | ||
|
||
setInitialized(true); | ||
} | ||
|
||
if (instance.current && userId) { | ||
await instance.current.login(userId); | ||
} | ||
}; | ||
|
||
load().catch(() => { | ||
setInitialized(true); | ||
}); | ||
|
||
return () => { | ||
if (!userId || !instance.current) { | ||
return; | ||
} | ||
|
||
instance.current.logout().catch(() => { | ||
// ignore | ||
}); | ||
}; | ||
}, [userId, initialized]); | ||
|
||
return initialized; | ||
} |