@a16n/web-sim 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -2
- package/bin/cli.ts +255 -0
- package/bin/web-sim.js +39 -0
- package/overrides/@backpackapp-io/react-native-toast/overrides.ts +15 -0
- package/overrides/@react-native-firebase/app/overrides.ts +34 -0
- package/overrides/@tiktok-for-business/react-native-sdk/NativeTiktokSdk.ts +29 -0
- package/overrides/@tiktok-for-business/react-native-sdk/overrides.ts +9 -0
- package/overrides/expo-application/ExpoApplication.ts +81 -0
- package/overrides/expo-application/overrides.ts +16 -0
- package/overrides/expo-background-task/ExpoBackgroundTaskModule.ts +30 -0
- package/overrides/expo-background-task/overrides.ts +9 -0
- package/overrides/expo-constants/ExponentConstants.ts +66 -0
- package/overrides/expo-constants/overrides.ts +9 -0
- package/overrides/expo-device/ExpoDevice.ts +104 -0
- package/overrides/expo-device/overrides.ts +9 -0
- package/overrides/expo-glass-effect/GlassContainer.tsx +9 -0
- package/overrides/expo-glass-effect/GlassView.tsx +95 -0
- package/overrides/expo-glass-effect/availability.ts +14 -0
- package/overrides/expo-glass-effect/overrides.ts +14 -0
- package/overrides/expo-haptics/Haptics.ts +67 -0
- package/overrides/expo-haptics/overrides.ts +5 -0
- package/overrides/expo-mail-composer/ExpoMailComposer.ts +40 -0
- package/overrides/expo-mail-composer/overrides.ts +9 -0
- package/overrides/expo-mesh-gradient/MeshGradientView.tsx +44 -0
- package/overrides/expo-mesh-gradient/overrides.ts +9 -0
- package/overrides/expo-modules-core/overrides.ts +12 -0
- package/overrides/expo-navigation-bar/ExpoNavigationBar.ts +29 -0
- package/overrides/expo-navigation-bar/NavigationBar.ts +43 -0
- package/overrides/expo-navigation-bar/overrides.ts +12 -0
- package/overrides/expo-router/NativeStackView.tsx +74 -0
- package/overrides/expo-router/NativeTabsView.tsx +263 -0
- package/overrides/expo-router/SafeAreaProvider.ts +6 -0
- package/overrides/expo-router/overrides.ts +23 -0
- package/overrides/expo-router/statusBarRequests.ts +51 -0
- package/overrides/expo-screen-orientation/ExpoScreenOrientation.ts +173 -0
- package/overrides/expo-screen-orientation/overrides.ts +21 -0
- package/overrides/expo-sharing/SharingNativeModule.ts +21 -0
- package/overrides/expo-sharing/overrides.ts +9 -0
- package/overrides/expo-sms/ExpoSMS.ts +24 -0
- package/overrides/expo-sms/overrides.ts +9 -0
- package/overrides/expo-splash-screen/SplashScreen.ts +25 -0
- package/overrides/expo-splash-screen/overrides.ts +5 -0
- package/overrides/expo-status-bar/StatusBar.tsx +44 -0
- package/overrides/expo-status-bar/overrides.ts +5 -0
- package/overrides/expo-store-review/ExpoStoreReview.ts +18 -0
- package/overrides/expo-store-review/overrides.ts +9 -0
- package/overrides/expo-symbols/SymbolView.tsx +192 -0
- package/overrides/expo-symbols/overrides.ts +9 -0
- package/overrides/expo-system-ui/ExpoSystemUI.ts +34 -0
- package/overrides/expo-system-ui/overrides.ts +9 -0
- package/overrides/expo-task-manager/ExpoTaskManager.ts +39 -0
- package/overrides/expo-task-manager/overrides.ts +9 -0
- package/overrides/expo-tracking-transparency/ExpoTrackingTransparency.ts +30 -0
- package/overrides/expo-tracking-transparency/overrides.ts +11 -0
- package/overrides/expo-video/ExpoVideo.ts +16 -0
- package/overrides/expo-video/overrides.ts +9 -0
- package/overrides/lottie-react-native/LottieView.tsx +87 -0
- package/overrides/lottie-react-native/overrides.ts +9 -0
- package/overrides/react-native-pager-view/PagerViewNativeComponent.tsx +254 -0
- package/overrides/react-native-pager-view/overrides.ts +9 -0
- package/overrides/react-native-reanimated/overrides.ts +28 -0
- package/overrides/react-native-safe-area-context/NativeSafeAreaProvider.tsx +40 -0
- package/overrides/react-native-safe-area-context/overrides.ts +12 -0
- package/overrides/react-native-share/NativeRNShare.ts +22 -0
- package/overrides/react-native-share/overrides.ts +12 -0
- package/overrides/react-native-share/platform.ts +14 -0
- package/overrides/react-native-turbo-image/TurboImage.tsx +101 -0
- package/overrides/react-native-turbo-image/overrides.ts +9 -0
- package/overrides/react-native-web/RefreshControl.tsx +57 -0
- package/overrides/react-native-web/overrides.ts +153 -0
- package/overrides/react-native-web/useElementLayout.ts +140 -0
- package/package.json +78 -4
- package/plugins/battery/overrides/expo-battery/ExpoBattery.ts +57 -0
- package/plugins/battery/overrides/expo-battery/overrides.ts +9 -0
- package/plugins/battery/plugin.ts +8 -0
- package/plugins/battery/shell.tsx +56 -0
- package/plugins/battery/state.ts +11 -0
- package/plugins/biometrics/overrides/expo-local-authentication/ExpoLocalAuthentication.ts +90 -0
- package/plugins/biometrics/overrides/expo-local-authentication/overrides.ts +11 -0
- package/plugins/biometrics/plugin.ts +8 -0
- package/plugins/biometrics/shell.tsx +88 -0
- package/plugins/biometrics/state.ts +41 -0
- package/plugins/brightness/app.ts +5 -0
- package/plugins/brightness/overrides/expo-brightness/Brightness.ts +98 -0
- package/plugins/brightness/overrides/expo-brightness/overrides.ts +10 -0
- package/plugins/brightness/plugin.ts +9 -0
- package/plugins/brightness/shell.tsx +55 -0
- package/plugins/brightness/state.ts +19 -0
- package/plugins/calendar/calendar.ts +299 -0
- package/plugins/calendar/overrides/expo-calendar/ExpoCalendar.ts +358 -0
- package/plugins/calendar/overrides/expo-calendar/LegacyExpoCalendar.ts +163 -0
- package/plugins/calendar/overrides/expo-calendar/overrides.ts +14 -0
- package/plugins/calendar/plugin.ts +8 -0
- package/plugins/calendar/shell.tsx +123 -0
- package/plugins/camera/overrides/expo-camera/ExpoCamera.tsx +269 -0
- package/plugins/camera/overrides/expo-camera/ExpoCameraManager.ts +56 -0
- package/plugins/camera/overrides/expo-camera/overrides.ts +12 -0
- package/plugins/camera/plugin.ts +9 -0
- package/plugins/camera/shell.tsx +107 -0
- package/plugins/camera/state.ts +61 -0
- package/plugins/clipboard/overrides/expo-clipboard/ExpoClipboard.ts +138 -0
- package/plugins/clipboard/overrides/expo-clipboard/overrides.ts +9 -0
- package/plugins/clipboard/plugin.ts +8 -0
- package/plugins/clipboard/shell.tsx +122 -0
- package/plugins/clipboard/state.ts +93 -0
- package/plugins/clock/app.ts +25 -0
- package/plugins/clock/plugin.ts +8 -0
- package/plugins/clock/shell.tsx +225 -0
- package/plugins/clock/state.ts +12 -0
- package/plugins/contacts/contacts.ts +125 -0
- package/plugins/contacts/overrides/expo-contacts/ExpoContacts.ts +153 -0
- package/plugins/contacts/overrides/expo-contacts/ExpoContactsNext.ts +232 -0
- package/plugins/contacts/overrides/expo-contacts/overrides.ts +12 -0
- package/plugins/contacts/plugin.ts +8 -0
- package/plugins/contacts/shell.tsx +109 -0
- package/plugins/home-indicator/app.ts +5 -0
- package/plugins/home-indicator/plugin.ts +9 -0
- package/plugins/home-indicator/requests.ts +48 -0
- package/plugins/home-indicator/shell.tsx +68 -0
- package/plugins/home-indicator/state.ts +31 -0
- package/plugins/keep-awake/app.ts +5 -0
- package/plugins/keep-awake/overrides/expo-keep-awake/ExpoKeepAwake.ts +27 -0
- package/plugins/keep-awake/overrides/expo-keep-awake/overrides.ts +9 -0
- package/plugins/keep-awake/plugin.ts +9 -0
- package/plugins/keep-awake/shell.tsx +80 -0
- package/plugins/keep-awake/state.ts +12 -0
- package/plugins/keyboard/Keyboard.tsx +354 -0
- package/plugins/keyboard/app.ts +90 -0
- package/plugins/keyboard/controller.ts +384 -0
- package/plugins/keyboard/fields.ts +244 -0
- package/plugins/keyboard/icons.tsx +283 -0
- package/plugins/keyboard/keyboard.css +362 -0
- package/plugins/keyboard/layouts/android.ts +175 -0
- package/plugins/keyboard/layouts/index.ts +10 -0
- package/plugins/keyboard/layouts/ios.ts +219 -0
- package/plugins/keyboard/layouts/types.ts +80 -0
- package/plugins/keyboard/overrides/react-native-keyboard-controller/KeyboardController.tsx +470 -0
- package/plugins/keyboard/overrides/react-native-keyboard-controller/overrides.ts +9 -0
- package/plugins/keyboard/plugin.ts +13 -0
- package/plugins/keyboard/shell.tsx +133 -0
- package/plugins/keyboard/state.ts +92 -0
- package/plugins/keyboard/suggestions.ts +63 -0
- package/plugins/keyboard/taps.ts +77 -0
- package/plugins/keyboard/typing.ts +112 -0
- package/plugins/locale/app.ts +232 -0
- package/plugins/locale/locale.ts +143 -0
- package/plugins/locale/overrides/expo-localization/ExpoLocalization.ts +41 -0
- package/plugins/locale/overrides/expo-localization/overrides.ts +9 -0
- package/plugins/locale/plugin.ts +9 -0
- package/plugins/locale/shell.tsx +147 -0
- package/plugins/locale/state.ts +36 -0
- package/plugins/location/geo.ts +56 -0
- package/plugins/location/overrides/expo-location/ExpoLocation.ts +235 -0
- package/plugins/location/overrides/expo-location/overrides.ts +11 -0
- package/plugins/location/places.ts +38 -0
- package/plugins/location/plugin.ts +8 -0
- package/plugins/location/shell.tsx +188 -0
- package/plugins/location/state.ts +51 -0
- package/plugins/motion/overrides/expo-sensors/DeviceSensor.ts +169 -0
- package/plugins/motion/overrides/expo-sensors/ExponentPedometer.ts +54 -0
- package/plugins/motion/overrides/expo-sensors/overrides.ts +14 -0
- package/plugins/motion/plugin.ts +8 -0
- package/plugins/motion/sensors.ts +116 -0
- package/plugins/motion/shell.tsx +136 -0
- package/plugins/motion/state.ts +46 -0
- package/plugins/network/app.ts +400 -0
- package/plugins/network/overrides/expo-network/ExpoNetwork.ts +51 -0
- package/plugins/network/overrides/expo-network/overrides.ts +9 -0
- package/plugins/network/plugin.ts +8 -0
- package/plugins/network/shell.tsx +67 -0
- package/plugins/network/state.ts +29 -0
- package/plugins/notifications/center.ts +255 -0
- package/plugins/notifications/content.ts +36 -0
- package/plugins/notifications/overrides/expo-notifications/NotificationsModule.ts +175 -0
- package/plugins/notifications/overrides/expo-notifications/channels.ts +108 -0
- package/plugins/notifications/overrides/expo-notifications/getDevicePushTokenAsync.ts +19 -0
- package/plugins/notifications/overrides/expo-notifications/getExpoPushTokenAsync.ts +16 -0
- package/plugins/notifications/overrides/expo-notifications/overrides.ts +36 -0
- package/plugins/notifications/plugin.ts +8 -0
- package/plugins/notifications/shell.tsx +192 -0
- package/plugins/notifications/state.ts +56 -0
- package/plugins/notifications/tokens.ts +47 -0
- package/plugins/notifications/trigger.ts +107 -0
- package/plugins/permissions/plugin.ts +6 -0
- package/plugins/permissions/shell.tsx +50 -0
- package/plugins/permissions/state.ts +38 -0
- package/plugins/photos/appLibrary.ts +114 -0
- package/plugins/photos/library.ts +322 -0
- package/plugins/photos/overrides/expo-image-picker/ExponentImagePicker.ts +153 -0
- package/plugins/photos/overrides/expo-image-picker/overrides.ts +13 -0
- package/plugins/photos/overrides/expo-media-library/ExpoMediaLibrary.ts +235 -0
- package/plugins/photos/overrides/expo-media-library/NativeMediaLibraryModule.ts +318 -0
- package/plugins/photos/overrides/expo-media-library/overrides.ts +19 -0
- package/plugins/photos/plugin.ts +8 -0
- package/plugins/photos/samples.ts +367 -0
- package/plugins/photos/shell.tsx +164 -0
- package/plugins/photos/state.ts +88 -0
- package/plugins/storage/files.ts +351 -0
- package/plugins/storage/overrides/expo-file-system/ExpoFileSystem.ts +770 -0
- package/plugins/storage/overrides/expo-file-system/ExponentFileSystem.ts +196 -0
- package/plugins/storage/overrides/expo-file-system/overrides.ts +13 -0
- package/plugins/storage/overrides/expo-secure-store/ExpoSecureStore.ts +75 -0
- package/plugins/storage/overrides/expo-secure-store/overrides.ts +9 -0
- package/plugins/storage/plugin.ts +8 -0
- package/plugins/storage/reset.ts +136 -0
- package/plugins/storage/secure.ts +71 -0
- package/plugins/storage/shell.tsx +365 -0
- package/plugins/storage/state.ts +13 -0
- package/src/client/app/appDocument.ts +113 -0
- package/src/client/app/appFocus.ts +97 -0
- package/src/client/app/console.ts +118 -0
- package/src/client/app/fonts.ts +59 -0
- package/src/client/app/main.tsx +99 -0
- package/src/client/app/routeContext.ts +45 -0
- package/src/client/app/touch.ts +640 -0
- package/src/client/entry.ts +20 -0
- package/src/client/modules.d.ts +8 -0
- package/src/client/react-native/app.ts +57 -0
- package/src/client/react-native/deep.ts +13 -0
- package/src/client/react-native/keyboard.tsx +192 -0
- package/src/client/react-native/library.ts +256 -0
- package/src/client/shell/Cookies.tsx +95 -0
- package/src/client/shell/LogDrawer.tsx +415 -0
- package/src/client/shell/PhotoPicker.tsx +174 -0
- package/src/client/shell/PluginOverlays.tsx +42 -0
- package/src/client/shell/PluginPanels.tsx +82 -0
- package/src/client/shell/ResetApp.tsx +87 -0
- package/src/client/shell/Resizer.tsx +99 -0
- package/src/client/shell/Shell.tsx +531 -0
- package/src/client/shell/SystemBars.tsx +376 -0
- package/src/client/shell/SystemCamera.tsx +181 -0
- package/src/client/shell/SystemOverlay.tsx +422 -0
- package/src/client/shell/SystemSheets.tsx +462 -0
- package/src/client/shell/backdrop.ts +229 -0
- package/src/client/shell/control.ts +91 -0
- package/src/client/shell/events.ts +226 -0
- package/src/client/shell/geometry.ts +68 -0
- package/src/client/shell/host.ts +492 -0
- package/src/client/shell/icons.tsx +34 -0
- package/src/client/shell/logFeed.ts +102 -0
- package/src/client/shell/main.tsx +22 -0
- package/src/client/shell/permissionCopy.ts +169 -0
- package/src/client/shell/plugins.ts +266 -0
- package/src/client/shell/popout.ts +185 -0
- package/src/client/shell/popover.ts +27 -0
- package/src/client/shell/shell.css +878 -0
- package/src/client/shell/statusTime.ts +31 -0
- package/src/client/shell/tooltips.ts +104 -0
- package/src/client/virtual.d.ts +21 -0
- package/src/devices/catalog.ts +223 -0
- package/src/devices/types.ts +83 -0
- package/src/node/appInfo.ts +186 -0
- package/src/node/appSource.ts +138 -0
- package/src/node/config.ts +180 -0
- package/src/node/control.ts +144 -0
- package/src/node/doctor.ts +199 -0
- package/src/node/events/cli.ts +206 -0
- package/src/node/events/shared.ts +238 -0
- package/src/node/events/store.ts +266 -0
- package/src/node/index.ts +5 -0
- package/src/node/overrides/define.ts +56 -0
- package/src/node/overrides/packages.ts +136 -0
- package/src/node/overrides/platformPatch.ts +56 -0
- package/src/node/overrides/plugin.ts +166 -0
- package/src/node/overrides/registry.ts +258 -0
- package/src/node/paths.ts +18 -0
- package/src/node/platform.ts +171 -0
- package/src/node/plugins.ts +98 -0
- package/src/node/proxy.ts +421 -0
- package/src/node/server.ts +253 -0
- package/src/plugin/index.ts +33 -0
- package/src/plugin/types.ts +127 -0
- package/src/plugin/ui.tsx +562 -0
- package/src/runtime/clock.ts +112 -0
- package/src/runtime/draw.ts +196 -0
- package/src/runtime/index.ts +513 -0
- package/src/runtime/media.ts +379 -0
- package/src/runtime/records.ts +76 -0
- package/src/runtime/timezone.ts +81 -0
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { getHost, phoneNow, realDelayUntil } from 'web-sim/runtime'
|
|
2
|
+
import type { PluginContext } from 'web-sim/shell'
|
|
3
|
+
import { normalizeContent } from './content.ts'
|
|
4
|
+
import { clone, DEFAULT_ACTION, type Delivered, type Json, type NotificationRequest, type NotificationsState, type Push } from './state.ts'
|
|
5
|
+
import { nextDate, repeats } from './trigger.ts'
|
|
6
|
+
|
|
7
|
+
/** How long the app's notification handler has to answer, as on iOS and Android. */
|
|
8
|
+
const HANDLER_TIMEOUT_MS = 3000
|
|
9
|
+
/** setTimeout's longest delay; a later trigger re-arms when it runs out. */
|
|
10
|
+
const MAX_DELAY_MS = 2 ** 31 - 1
|
|
11
|
+
|
|
12
|
+
type Context = PluginContext<NotificationsState>
|
|
13
|
+
|
|
14
|
+
const CALENDAR_TRIGGERS = ['daily', 'weekly', 'monthly', 'yearly', 'calendar']
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The phone's delivery of notifications, in the shell: a timer for each scheduled request in the state (so a request
|
|
18
|
+
* scheduled before an app reload still fires, and one scheduled before a shell reload is re-armed), the app's
|
|
19
|
+
* notification handler, the banner, and the response when it is tapped. Events reach the override's listeners
|
|
20
|
+
* through the plugin's channel.
|
|
21
|
+
*/
|
|
22
|
+
export class NotificationCenter {
|
|
23
|
+
private timers = new Map<string, { next: number; timer: ReturnType<typeof setTimeout> }>()
|
|
24
|
+
private pending = new Map<string, (behavior: Json | null) => void>()
|
|
25
|
+
private readonly context: Context
|
|
26
|
+
|
|
27
|
+
constructor(context: Context) {
|
|
28
|
+
this.context = context
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
start(): () => void {
|
|
32
|
+
const removals = [
|
|
33
|
+
this.context.subscribe(() => this.sync()),
|
|
34
|
+
// Calendar triggers keep the phone's wall-clock time when its time zone changes.
|
|
35
|
+
getHost().plugins.subscribe('locale', () => this.rezone()),
|
|
36
|
+
// Triggers are in the phone's time: when its clock is set, frozen or sped up, each is re-armed, and one the clock
|
|
37
|
+
// jumped past fires.
|
|
38
|
+
getHost().plugins.subscribe('clock', () => this.rearm()),
|
|
39
|
+
this.context.on('present', (request) => void this.deliver(clone(request as NotificationRequest))),
|
|
40
|
+
this.context.on('handled', (payload) => {
|
|
41
|
+
const { id, behavior } = payload as { id: string; behavior: Json }
|
|
42
|
+
this.pending.get(id)?.(clone(behavior))
|
|
43
|
+
this.pending.delete(id)
|
|
44
|
+
}),
|
|
45
|
+
]
|
|
46
|
+
this.sync()
|
|
47
|
+
return () => {
|
|
48
|
+
removals.forEach((remove) => remove())
|
|
49
|
+
this.timers.forEach(({ timer }) => clearTimeout(timer))
|
|
50
|
+
this.timers.clear()
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A new app run: it was launched from the home screen, and the last run's handler will never answer. */
|
|
55
|
+
appStarted(): void {
|
|
56
|
+
this.pending.forEach((resolve) => resolve(null))
|
|
57
|
+
this.pending.clear()
|
|
58
|
+
this.context.set({ lastResponse: null })
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private rezone(): void {
|
|
62
|
+
const { scheduled } = this.context.state()
|
|
63
|
+
const now = phoneNow()
|
|
64
|
+
const moved = scheduled.map((item) => {
|
|
65
|
+
if (!CALENDAR_TRIGGERS.includes(String(item.request.trigger?.type))) {
|
|
66
|
+
return item
|
|
67
|
+
}
|
|
68
|
+
const next = nextDate(item.request.trigger!, now)
|
|
69
|
+
return next === null || next === item.next ? item : { ...item, next }
|
|
70
|
+
})
|
|
71
|
+
if (moved.some((item, index) => item !== scheduled[index])) {
|
|
72
|
+
this.context.set({ scheduled: moved })
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private rearm(): void {
|
|
77
|
+
this.timers.forEach(({ timer }) => clearTimeout(timer))
|
|
78
|
+
this.timers.clear()
|
|
79
|
+
this.sync()
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Arms a timer for each scheduled request, and drops the timers of those cancelled. */
|
|
83
|
+
private sync(): void {
|
|
84
|
+
const { scheduled } = this.context.state()
|
|
85
|
+
for (const [id, { timer }] of this.timers) {
|
|
86
|
+
if (!scheduled.some((item) => item.request.identifier === id)) {
|
|
87
|
+
clearTimeout(timer)
|
|
88
|
+
this.timers.delete(id)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
for (const { request, next } of scheduled) {
|
|
92
|
+
const armed = this.timers.get(request.identifier)
|
|
93
|
+
if (armed?.next === next) {
|
|
94
|
+
continue
|
|
95
|
+
}
|
|
96
|
+
if (armed !== undefined) {
|
|
97
|
+
clearTimeout(armed.timer)
|
|
98
|
+
}
|
|
99
|
+
// Real milliseconds until the phone's clock reaches it; none while the clock is frozen short of it.
|
|
100
|
+
const delay = realDelayUntil(next)
|
|
101
|
+
if (delay === null) {
|
|
102
|
+
this.timers.delete(request.identifier)
|
|
103
|
+
continue
|
|
104
|
+
}
|
|
105
|
+
this.timers.set(request.identifier, { next, timer: setTimeout(() => this.due(request.identifier), Math.min(delay, MAX_DELAY_MS)) })
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
private due(identifier: string): void {
|
|
110
|
+
const item = this.context.state().scheduled.find((candidate) => candidate.request.identifier === identifier)
|
|
111
|
+
this.timers.delete(identifier)
|
|
112
|
+
if (item === undefined) {
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
if (item.next > phoneNow()) {
|
|
116
|
+
// A trigger further off than setTimeout reaches, or a clock slowed since it was armed.
|
|
117
|
+
this.sync()
|
|
118
|
+
return
|
|
119
|
+
}
|
|
120
|
+
this.fire(identifier)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Delivers a scheduled request now, and moves it on to its next time, or off the schedule. */
|
|
124
|
+
fire(identifier: string): void {
|
|
125
|
+
const { scheduled } = this.context.state()
|
|
126
|
+
const item = scheduled.find((candidate) => candidate.request.identifier === identifier)
|
|
127
|
+
if (item === undefined) {
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
const next = repeats(item.request.trigger!) ? nextDate(item.request.trigger!, Math.max(item.next, phoneNow())) : null
|
|
131
|
+
this.context.set({
|
|
132
|
+
scheduled:
|
|
133
|
+
next === null
|
|
134
|
+
? scheduled.filter((candidate) => candidate !== item)
|
|
135
|
+
: scheduled.map((candidate) => (candidate === item ? { ...candidate, next } : candidate)),
|
|
136
|
+
})
|
|
137
|
+
void this.deliver(item.request)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** A remote notification, arriving as APNs or FCM delivers one. */
|
|
141
|
+
push(push: Push): NotificationRequest {
|
|
142
|
+
const platform = this.context.sim().device.platform
|
|
143
|
+
const count = this.context.state().pushCount + 1
|
|
144
|
+
const content = normalizeContent(
|
|
145
|
+
{ title: push.title, subtitle: push.subtitle, body: push.body, data: push.data ?? {}, badge: push.badge, sound: push.sound === undefined ? 'default' : push.sound },
|
|
146
|
+
platform,
|
|
147
|
+
)
|
|
148
|
+
const data = Object.fromEntries(Object.entries(push.data ?? {}).map(([key, value]) => [key, typeof value === 'string' ? value : JSON.stringify(value)]))
|
|
149
|
+
const trigger: Json =
|
|
150
|
+
platform === 'ios'
|
|
151
|
+
? {
|
|
152
|
+
type: 'push',
|
|
153
|
+
payload: {
|
|
154
|
+
aps: {
|
|
155
|
+
alert: { title: push.title, subtitle: push.subtitle, body: push.body },
|
|
156
|
+
...(push.badge !== undefined ? { badge: push.badge } : {}),
|
|
157
|
+
sound: push.sound === undefined ? 'default' : push.sound,
|
|
158
|
+
},
|
|
159
|
+
...(push.data ?? {}),
|
|
160
|
+
},
|
|
161
|
+
}
|
|
162
|
+
: {
|
|
163
|
+
type: 'push',
|
|
164
|
+
remoteMessage: {
|
|
165
|
+
collapseKey: null,
|
|
166
|
+
data,
|
|
167
|
+
from: null,
|
|
168
|
+
messageId: `0:${count}`,
|
|
169
|
+
messageType: null,
|
|
170
|
+
originalPriority: 2,
|
|
171
|
+
priority: 2,
|
|
172
|
+
sentTime: phoneNow(),
|
|
173
|
+
to: null,
|
|
174
|
+
ttl: 2419200,
|
|
175
|
+
notification: {
|
|
176
|
+
title: push.title ?? null,
|
|
177
|
+
body: push.body ?? null,
|
|
178
|
+
channelId: push.channelId ?? null,
|
|
179
|
+
notificationCount: push.badge ?? null,
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
}
|
|
183
|
+
const request: NotificationRequest = { identifier: `push-${count}`, content, trigger }
|
|
184
|
+
this.context.set({ pushCount: count })
|
|
185
|
+
void this.deliver(request, push.badge)
|
|
186
|
+
return request
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Whether the app may post notifications. Android asks from Android 13 (API 33); before it, they are on from install. */
|
|
190
|
+
private allowed(): boolean {
|
|
191
|
+
const status = getHost().getPermission('notifications')
|
|
192
|
+
const { platform, osVersion } = this.context.sim().device
|
|
193
|
+
return status === 'granted' || (platform === 'android' && Number(osVersion) < 33 && status !== 'denied')
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* As the OS delivers one: nothing without permission; otherwise the app's handler (or, with none, the default that
|
|
198
|
+
* shows everything) decides on the banner and the notification centre, and the app hears of it. A remote push sets
|
|
199
|
+
* the badge itself.
|
|
200
|
+
*/
|
|
201
|
+
private async deliver(request: NotificationRequest, remoteBadge?: number): Promise<void> {
|
|
202
|
+
if (!this.allowed()) {
|
|
203
|
+
return
|
|
204
|
+
}
|
|
205
|
+
const notification: Delivered = { date: phoneNow(), request }
|
|
206
|
+
const behavior = await this.behaviorFor(notification)
|
|
207
|
+
const listed = behavior !== null && (behavior.shouldShowList === true || behavior.shouldShowAlert === true)
|
|
208
|
+
const banner = behavior !== null && (behavior.shouldShowBanner === true || behavior.shouldShowAlert === true)
|
|
209
|
+
const { delivered } = this.context.state()
|
|
210
|
+
const others = delivered.filter((item) => item.request.identifier !== request.identifier)
|
|
211
|
+
const badge = remoteBadge ?? (behavior?.shouldSetBadge === true && typeof request.content.badge === 'number' ? request.content.badge : undefined)
|
|
212
|
+
this.context.set({ delivered: listed ? [...others, notification] : others, ...(badge !== undefined ? { badge: Math.max(0, badge) } : {}) })
|
|
213
|
+
this.context.emit('onDidReceiveNotification', notification)
|
|
214
|
+
if (!banner) {
|
|
215
|
+
return
|
|
216
|
+
}
|
|
217
|
+
const { content } = request
|
|
218
|
+
const tapped = await getHost().notify({
|
|
219
|
+
id: request.identifier,
|
|
220
|
+
title: text(content.title),
|
|
221
|
+
subtitle: this.context.sim().device.platform === 'ios' ? text(content.subtitle) : undefined,
|
|
222
|
+
body: text(content.body),
|
|
223
|
+
data: content.data,
|
|
224
|
+
})
|
|
225
|
+
if (tapped) {
|
|
226
|
+
const lastResponse = { notification, actionIdentifier: DEFAULT_ACTION }
|
|
227
|
+
this.context.set({ lastResponse, delivered: this.context.state().delivered.filter((item) => item.request.identifier !== request.identifier) })
|
|
228
|
+
this.context.emit('onDidReceiveNotificationResponse', lastResponse)
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private behaviorFor(notification: Delivered): Promise<Json | null> {
|
|
233
|
+
if (this.context.listenerCount('onHandleNotification') === 0) {
|
|
234
|
+
return Promise.resolve({ shouldShowBanner: true, shouldShowList: true, shouldPlaySound: true, shouldSetBadge: true })
|
|
235
|
+
}
|
|
236
|
+
const id = `${notification.request.identifier}:${notification.date}`
|
|
237
|
+
return new Promise((resolve) => {
|
|
238
|
+
const timer = setTimeout(() => {
|
|
239
|
+
if (this.pending.delete(id)) {
|
|
240
|
+
this.context.emit('onHandleNotificationTimeout', { id, notification })
|
|
241
|
+
resolve(null)
|
|
242
|
+
}
|
|
243
|
+
}, HANDLER_TIMEOUT_MS)
|
|
244
|
+
this.pending.set(id, (behavior) => {
|
|
245
|
+
clearTimeout(timer)
|
|
246
|
+
resolve(behavior)
|
|
247
|
+
})
|
|
248
|
+
this.context.emit('onHandleNotification', { id, notification })
|
|
249
|
+
})
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function text(value: unknown): string | undefined {
|
|
254
|
+
return typeof value === 'string' && value !== '' ? value : undefined
|
|
255
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Platform } from 'web-sim/runtime'
|
|
2
|
+
import type { Json } from './state.ts'
|
|
3
|
+
|
|
4
|
+
/** The content as the native module reports it back: every field present, `sound: true` as 'default'. */
|
|
5
|
+
export function normalizeContent(input: Json, platform: Platform): Json {
|
|
6
|
+
const sound = input.sound === true ? 'default' : typeof input.sound === 'string' ? input.sound : null
|
|
7
|
+
const common = {
|
|
8
|
+
title: input.title ?? null,
|
|
9
|
+
subtitle: input.subtitle ?? null,
|
|
10
|
+
body: input.body ?? null,
|
|
11
|
+
data: input.data ?? {},
|
|
12
|
+
categoryIdentifier: input.categoryIdentifier ?? null,
|
|
13
|
+
sound: sound === null || ['default', 'defaultCritical', 'defaultRingtone'].includes(sound) ? sound : 'custom',
|
|
14
|
+
}
|
|
15
|
+
if (platform === 'android') {
|
|
16
|
+
return {
|
|
17
|
+
...common,
|
|
18
|
+
badge: input.badge ?? null,
|
|
19
|
+
color: input.color ?? null,
|
|
20
|
+
priority: input.priority ?? 'default',
|
|
21
|
+
vibrationPattern: input.vibrate ?? null,
|
|
22
|
+
autoDismiss: input.autoDismiss ?? true,
|
|
23
|
+
sticky: input.sticky ?? false,
|
|
24
|
+
threadIdentifier: input.threadIdentifier ?? null,
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
...common,
|
|
29
|
+
badge: input.badge ?? null,
|
|
30
|
+
launchImageName: input.launchImageName ?? null,
|
|
31
|
+
attachments: input.attachments ?? [],
|
|
32
|
+
threadIdentifier: input.threadIdentifier ?? null,
|
|
33
|
+
interruptionLevel: input.interruptionLevel ?? 'active',
|
|
34
|
+
targetContentIdentifier: null,
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { CodedError } from 'expo'
|
|
2
|
+
import { getHost, getPermission, getState, permissionResponse, platform, plugin, requestPermission, type PermissionResponse } from 'web-sim/runtime'
|
|
3
|
+
import { normalizeContent } from '../../content.ts'
|
|
4
|
+
import { clone, INITIAL_NOTIFICATIONS, type Json, type NotificationsState } from '../../state.ts'
|
|
5
|
+
import { nextDate } from '../../trigger.ts'
|
|
6
|
+
import { devicePushToken } from './getDevicePushTokenAsync'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Every native module expo-notifications' JS layer calls (the emitter, the handler, the scheduler, the presenter,
|
|
10
|
+
* permissions, push tokens, categories and the badge), as one object. Each module file of the package is replaced
|
|
11
|
+
* with this one; the JS layer only calls the methods that module has on the device. The schedule, the notification
|
|
12
|
+
* centre, the badge and the categories are the notifications plugin's state; its shell module keeps the timers and
|
|
13
|
+
* delivers, and its events reach the listeners here.
|
|
14
|
+
*/
|
|
15
|
+
const ID = 'notifications'
|
|
16
|
+
const notifications = () => plugin<NotificationsState>(ID, INITIAL_NOTIFICATIONS)
|
|
17
|
+
|
|
18
|
+
/** Android 13 (API 33) added the notifications permission; before it, notifications are allowed from install. */
|
|
19
|
+
function promptsForPermission(): boolean {
|
|
20
|
+
return platform() === 'ios' || Number(getState().device.osVersion) >= 33
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function withPlatformDetails(response: PermissionResponse) {
|
|
24
|
+
if (platform() === 'android') {
|
|
25
|
+
// AndroidImportance: DEFAULT 5, NONE 2.
|
|
26
|
+
return { ...response, android: { importance: response.granted ? 5 : 2, interruptionFilter: 1 } }
|
|
27
|
+
}
|
|
28
|
+
const granted = response.granted
|
|
29
|
+
return {
|
|
30
|
+
...response,
|
|
31
|
+
ios: {
|
|
32
|
+
// IosAuthorizationStatus: NOT_DETERMINED 0, DENIED 1, AUTHORIZED 2.
|
|
33
|
+
status: response.status === 'undetermined' ? 0 : granted ? 2 : 1,
|
|
34
|
+
allowsDisplayInNotificationCenter: granted,
|
|
35
|
+
allowsDisplayOnLockScreen: granted,
|
|
36
|
+
allowsDisplayInCarPlay: null,
|
|
37
|
+
allowsAlert: granted,
|
|
38
|
+
allowsBadge: granted,
|
|
39
|
+
allowsSound: granted,
|
|
40
|
+
allowsCriticalAlerts: null,
|
|
41
|
+
alertStyle: granted ? 1 : 0,
|
|
42
|
+
allowsPreviews: granted ? 1 : null,
|
|
43
|
+
providesAppNotificationSettings: false,
|
|
44
|
+
allowsAnnouncements: null,
|
|
45
|
+
},
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const NotificationsModule = {
|
|
50
|
+
// Events: the emitter's, the handler's and the push token's, from the plugin's shell module.
|
|
51
|
+
addListener(event: string, listener: (payload: unknown) => void) {
|
|
52
|
+
// Payloads come from the shell's window: cloned into this one, so they are this window's objects and arrays.
|
|
53
|
+
const remove = notifications().on(event, (payload) => listener(clone(payload)))
|
|
54
|
+
return { remove }
|
|
55
|
+
},
|
|
56
|
+
removeListener() {},
|
|
57
|
+
removeListeners() {},
|
|
58
|
+
removeAllListeners() {},
|
|
59
|
+
listenerCount(event: string) {
|
|
60
|
+
return getHost().plugins.listenerCount(ID, event, 'app')
|
|
61
|
+
},
|
|
62
|
+
emit(event: string, payload?: unknown) {
|
|
63
|
+
// To this module's own listeners, as the native emitter does.
|
|
64
|
+
getHost().plugins.emit(ID, event, payload, 'shell')
|
|
65
|
+
},
|
|
66
|
+
// NotificationsEmitterModule
|
|
67
|
+
getLastNotificationResponse() {
|
|
68
|
+
return clone(notifications().get().lastResponse)
|
|
69
|
+
},
|
|
70
|
+
clearLastNotificationResponse() {
|
|
71
|
+
notifications().set({ lastResponse: null })
|
|
72
|
+
},
|
|
73
|
+
// NotificationsHandlerModule
|
|
74
|
+
async handleNotificationAsync(id: string, behavior: Json) {
|
|
75
|
+
notifications().emit('handled', { id, behavior: clone(behavior) })
|
|
76
|
+
},
|
|
77
|
+
// NotificationScheduler
|
|
78
|
+
async scheduleNotificationAsync(identifier: string, content: Json, trigger: Json | null) {
|
|
79
|
+
if (trigger?.type === 'timeInterval') {
|
|
80
|
+
const seconds = trigger.seconds as number
|
|
81
|
+
if (!(seconds > 0)) {
|
|
82
|
+
throw new CodedError('ERR_NOTIFICATIONS_FAILED_TO_SCHEDULE', 'The time interval must be greater than 0.')
|
|
83
|
+
}
|
|
84
|
+
if (platform() === 'ios' && trigger.repeats === true && seconds < 60) {
|
|
85
|
+
throw new CodedError('ERR_NOTIFICATIONS_FAILED_TO_SCHEDULE', 'The time interval must be at least 60 if repeating.')
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (trigger?.type === 'date' && (trigger.timestamp as number) <= Date.now()) {
|
|
89
|
+
throw new CodedError('ERR_NOTIFICATIONS_FAILED_TO_SCHEDULE', 'The date must be in the future.')
|
|
90
|
+
}
|
|
91
|
+
const request = { identifier, content: normalizeContent(clone(content), platform()), trigger: clone(trigger) }
|
|
92
|
+
const scheduled = notifications().get().scheduled.filter((item) => item.request.identifier !== identifier)
|
|
93
|
+
if (trigger === null || trigger.type === 'channel') {
|
|
94
|
+
notifications().set({ scheduled })
|
|
95
|
+
notifications().emit('present', request)
|
|
96
|
+
return identifier
|
|
97
|
+
}
|
|
98
|
+
const next = nextDate(request.trigger!, Date.now())
|
|
99
|
+
notifications().set({ scheduled: next === null ? scheduled : [...scheduled, { request, next }] })
|
|
100
|
+
return identifier
|
|
101
|
+
},
|
|
102
|
+
async getAllScheduledNotificationsAsync() {
|
|
103
|
+
return clone(notifications().get().scheduled.map((item) => item.request))
|
|
104
|
+
},
|
|
105
|
+
async cancelScheduledNotificationAsync(identifier: string) {
|
|
106
|
+
notifications().set({ scheduled: notifications().get().scheduled.filter((item) => item.request.identifier !== identifier) })
|
|
107
|
+
},
|
|
108
|
+
async cancelAllScheduledNotificationsAsync() {
|
|
109
|
+
notifications().set({ scheduled: [] })
|
|
110
|
+
},
|
|
111
|
+
async getNextTriggerDateAsync(trigger: Json | null) {
|
|
112
|
+
return trigger === null ? null : nextDate(clone(trigger), Date.now())
|
|
113
|
+
},
|
|
114
|
+
// NotificationPresenterModule
|
|
115
|
+
async getPresentedNotificationsAsync() {
|
|
116
|
+
return clone(notifications().get().delivered)
|
|
117
|
+
},
|
|
118
|
+
async dismissNotificationAsync(identifier: string) {
|
|
119
|
+
notifications().set({ delivered: notifications().get().delivered.filter((item) => item.request.identifier !== identifier) })
|
|
120
|
+
},
|
|
121
|
+
async dismissAllNotificationsAsync() {
|
|
122
|
+
notifications().set({ delivered: [] })
|
|
123
|
+
},
|
|
124
|
+
// NotificationPermissionsModule
|
|
125
|
+
async getPermissionsAsync() {
|
|
126
|
+
const response = getPermission('notifications')
|
|
127
|
+
if (!promptsForPermission() && response.status === 'undetermined') {
|
|
128
|
+
return withPlatformDetails(permissionResponse('granted'))
|
|
129
|
+
}
|
|
130
|
+
return withPlatformDetails(response)
|
|
131
|
+
},
|
|
132
|
+
async requestPermissionsAsync() {
|
|
133
|
+
if (!promptsForPermission()) {
|
|
134
|
+
return NotificationsModule.getPermissionsAsync()
|
|
135
|
+
}
|
|
136
|
+
return withPlatformDetails(await requestPermission('notifications'))
|
|
137
|
+
},
|
|
138
|
+
async getExactAlarmPermissionsAsync() {
|
|
139
|
+
return { status: 'granted', granted: true, canAskAgain: true, expires: 'never' }
|
|
140
|
+
},
|
|
141
|
+
async requestExactAlarmPermissionsAsync() {
|
|
142
|
+
return { status: 'granted', granted: true, canAskAgain: true, expires: 'never' }
|
|
143
|
+
},
|
|
144
|
+
// PushTokenManager
|
|
145
|
+
async getDevicePushTokenAsync() {
|
|
146
|
+
return devicePushToken()
|
|
147
|
+
},
|
|
148
|
+
async unregisterForNotificationsAsync() {
|
|
149
|
+
notifications().set({ tokenGeneration: notifications().get().tokenGeneration + 1 })
|
|
150
|
+
},
|
|
151
|
+
// NotificationCategoriesModule
|
|
152
|
+
async getNotificationCategoriesAsync() {
|
|
153
|
+
return clone(Object.values(notifications().get().categories))
|
|
154
|
+
},
|
|
155
|
+
async setNotificationCategoryAsync(identifier: string, actions: Json[], options?: Json) {
|
|
156
|
+
const category = clone({ identifier, actions, options: options ?? {} })
|
|
157
|
+
notifications().set({ categories: { ...notifications().get().categories, [identifier]: category } })
|
|
158
|
+
return category
|
|
159
|
+
},
|
|
160
|
+
async deleteNotificationCategoryAsync(identifier: string) {
|
|
161
|
+
const { [identifier]: existed, ...categories } = notifications().get().categories
|
|
162
|
+
notifications().set({ categories })
|
|
163
|
+
return existed !== undefined
|
|
164
|
+
},
|
|
165
|
+
// BadgeModule
|
|
166
|
+
async getBadgeCountAsync() {
|
|
167
|
+
return notifications().get().badge
|
|
168
|
+
},
|
|
169
|
+
async setBadgeCountAsync(count: number) {
|
|
170
|
+
notifications().set({ badge: Math.max(0, Math.floor(count)) })
|
|
171
|
+
return true
|
|
172
|
+
},
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export default NotificationsModule
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { platform, plugin } from 'web-sim/runtime'
|
|
2
|
+
import { clone, INITIAL_NOTIFICATIONS, type Json, type NotificationsState } from '../../state.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Android's notification channels and channel groups. The package's own non-Android files answer as iOS does
|
|
6
|
+
* (nothing), and its `.android.js` files never load in a browser, so each channel file is replaced with this one,
|
|
7
|
+
* which keeps them in the notifications plugin's state on Android (the panel's push form offers them) and does
|
|
8
|
+
* nothing on iOS.
|
|
9
|
+
*/
|
|
10
|
+
const notifications = () => plugin<NotificationsState>('notifications', INITIAL_NOTIFICATIONS)
|
|
11
|
+
|
|
12
|
+
type Kind = 'channels' | 'channelGroups'
|
|
13
|
+
|
|
14
|
+
function read(kind: Kind): Record<string, Json> {
|
|
15
|
+
return { ...notifications().get()[kind] }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function write(kind: Kind, value: Record<string, Json>): void {
|
|
19
|
+
notifications().set({ [kind]: value })
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function channelFrom(id: string, input: Json, existing?: Json): Json {
|
|
23
|
+
const sound = input.sound === undefined ? (existing?.sound ?? 'default') : input.sound === null ? null : input.sound === 'default' ? 'default' : 'custom'
|
|
24
|
+
return {
|
|
25
|
+
id,
|
|
26
|
+
name: null,
|
|
27
|
+
// AndroidImportance.DEFAULT
|
|
28
|
+
importance: 5,
|
|
29
|
+
bypassDnd: false,
|
|
30
|
+
description: null,
|
|
31
|
+
groupId: null,
|
|
32
|
+
lightColor: '#000000',
|
|
33
|
+
// AndroidNotificationVisibility.UNKNOWN: the system default.
|
|
34
|
+
lockscreenVisibility: 0,
|
|
35
|
+
showBadge: true,
|
|
36
|
+
vibrationPattern: null,
|
|
37
|
+
enableLights: false,
|
|
38
|
+
enableVibrate: false,
|
|
39
|
+
...existing,
|
|
40
|
+
...input,
|
|
41
|
+
sound,
|
|
42
|
+
audioAttributes: {
|
|
43
|
+
// AndroidAudioUsage.NOTIFICATION, AndroidAudioContentType.SONIFICATION
|
|
44
|
+
usage: 5,
|
|
45
|
+
contentType: 4,
|
|
46
|
+
flags: { enforceAudibility: false, requestHardwareAudioVideoSynchronization: false },
|
|
47
|
+
...(existing?.audioAttributes as Json | undefined),
|
|
48
|
+
...(input.audioAttributes as Json | undefined),
|
|
49
|
+
},
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export async function setNotificationChannelAsync(channelId: string, channel: Json) {
|
|
54
|
+
if (platform() !== 'android') {
|
|
55
|
+
return null
|
|
56
|
+
}
|
|
57
|
+
const channels = read('channels')
|
|
58
|
+
channels[channelId] = channelFrom(channelId, clone(channel ?? {}), channels[channelId])
|
|
59
|
+
write('channels', channels)
|
|
60
|
+
return channels[channelId]
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export async function getNotificationChannelsAsync() {
|
|
64
|
+
return platform() === 'android' ? Object.values(read('channels')) : []
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function getNotificationChannelAsync(channelId: string) {
|
|
68
|
+
return platform() === 'android' ? (read('channels')[channelId] ?? null) : null
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function deleteNotificationChannelAsync(channelId: string) {
|
|
72
|
+
if (platform() === 'android') {
|
|
73
|
+
const channels = read('channels')
|
|
74
|
+
delete channels[channelId]
|
|
75
|
+
write('channels', channels)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function groupWithChannels(group: Json): Json {
|
|
80
|
+
return { ...group, channels: Object.values(read('channels')).filter((channel) => channel.groupId === group.id) }
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function setNotificationChannelGroupAsync(groupId: string, group: Json) {
|
|
84
|
+
if (platform() !== 'android') {
|
|
85
|
+
return null
|
|
86
|
+
}
|
|
87
|
+
const groups = read('channelGroups')
|
|
88
|
+
groups[groupId] = { id: groupId, name: group?.name ?? null, description: group?.description ?? null, isBlocked: false }
|
|
89
|
+
write('channelGroups', groups)
|
|
90
|
+
return groupWithChannels(groups[groupId])
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export async function getNotificationChannelGroupsAsync() {
|
|
94
|
+
return platform() === 'android' ? Object.values(read('channelGroups')).map(groupWithChannels) : []
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export async function getNotificationChannelGroupAsync(groupId: string) {
|
|
98
|
+
const group = platform() === 'android' ? read('channelGroups')[groupId] : undefined
|
|
99
|
+
return group === undefined ? null : groupWithChannels(group)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export async function deleteNotificationChannelGroupAsync(groupId: string) {
|
|
103
|
+
if (platform() === 'android') {
|
|
104
|
+
const groups = read('channelGroups')
|
|
105
|
+
delete groups[groupId]
|
|
106
|
+
write('channelGroups', groups)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { DeviceEventEmitter } from 'react-native'
|
|
2
|
+
import { app, platform, plugin } from 'web-sim/runtime'
|
|
3
|
+
import { INITIAL_NOTIFICATIONS, type NotificationsState } from '../../state.ts'
|
|
4
|
+
import { devicePushToken as tokenFor } from '../../tokens.ts'
|
|
5
|
+
|
|
6
|
+
/** The device token in the platform's form, the one the notifications panel shows. */
|
|
7
|
+
export function devicePushToken(): string {
|
|
8
|
+
return tokenFor(app().slug, platform(), plugin<NotificationsState>('notifications', INITIAL_NOTIFICATIONS).get().tokenGeneration)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The device's push token, typed for the simulated platform: an APNs token on iOS, an FCM token on Android. The web
|
|
13
|
+
* build would subscribe the browser to web push instead.
|
|
14
|
+
*/
|
|
15
|
+
export async function getDevicePushTokenAsync() {
|
|
16
|
+
const data = devicePushToken()
|
|
17
|
+
DeviceEventEmitter.emit('onDevicePushToken', { devicePushToken: data })
|
|
18
|
+
return { type: platform(), data }
|
|
19
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { app } from 'web-sim/runtime'
|
|
2
|
+
import { configProjectId, expoPushToken } from '../../tokens.ts'
|
|
3
|
+
import { getDevicePushTokenAsync } from './getDevicePushTokenAsync'
|
|
4
|
+
|
|
5
|
+
type Options = { devicePushToken?: { type: string; data: unknown }; projectId?: string; applicationId?: string }
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* An Expo push token, made on the device instead of fetched from Expo's servers: `ExponentPushToken[...]`, the same
|
|
9
|
+
* for the same device token and project, as the notifications panel shows it. Push sent to it goes nowhere; send one
|
|
10
|
+
* from the panel instead.
|
|
11
|
+
*/
|
|
12
|
+
export async function getExpoPushTokenAsync(options: Options = {}) {
|
|
13
|
+
const device = options.devicePushToken ?? (await getDevicePushTokenAsync())
|
|
14
|
+
const data = typeof device.data === 'string' ? device.data : JSON.stringify(device.data)
|
|
15
|
+
return { type: 'expo' as const, data: expoPushToken(data, options.projectId ?? configProjectId(app().config)) }
|
|
16
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { defineOverrides } from '../../../../src/node/overrides/define.ts'
|
|
2
|
+
|
|
3
|
+
const MODULES = [
|
|
4
|
+
'build/NotificationsEmitterModule.js',
|
|
5
|
+
'build/NotificationsHandlerModule.js',
|
|
6
|
+
'build/NotificationScheduler.js',
|
|
7
|
+
'build/NotificationPresenterModule.js',
|
|
8
|
+
'build/NotificationPermissionsModule.js',
|
|
9
|
+
'build/PushTokenManager.js',
|
|
10
|
+
'build/NotificationCategoriesModule.js',
|
|
11
|
+
'build/BadgeModule.web.js',
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
const CHANNELS = [
|
|
15
|
+
'setNotificationChannelAsync',
|
|
16
|
+
'getNotificationChannelsAsync',
|
|
17
|
+
'getNotificationChannelAsync',
|
|
18
|
+
'deleteNotificationChannelAsync',
|
|
19
|
+
'setNotificationChannelGroupAsync',
|
|
20
|
+
'getNotificationChannelGroupsAsync',
|
|
21
|
+
'getNotificationChannelGroupAsync',
|
|
22
|
+
'deleteNotificationChannelGroupAsync',
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
export default defineOverrides('expo-notifications', [
|
|
26
|
+
{
|
|
27
|
+
versions: '>=58 <59',
|
|
28
|
+
note: 'the notifications panel: scheduled and delivered notifications, remote pushes, the badge and push tokens; banners, the handler and listeners, the OS permission prompt',
|
|
29
|
+
files: {
|
|
30
|
+
...Object.fromEntries(MODULES.map((file) => [file, './NotificationsModule.ts'])),
|
|
31
|
+
...Object.fromEntries(CHANNELS.map((name) => [`build/${name}.js`, './channels.ts'])),
|
|
32
|
+
'build/getDevicePushTokenAsync.web.js': './getDevicePushTokenAsync.ts',
|
|
33
|
+
'build/getExpoPushTokenAsync.js': './getExpoPushTokenAsync.ts',
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
])
|