-
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.
♻️ notifications: use react-onesignal
- Loading branch information
Showing
3 changed files
with
83 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
importScripts("https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.sw.js"); |
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,77 @@ | ||
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) { | ||
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; | ||
} |