-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { EventEmitter, Subscription, CodedError } from '@unimodules/core'; | ||
import NotificationsHandlerModule, { NotificationBehavior } from './NotificationsHandlerModule'; | ||
|
||
type Notification = any; | ||
|
||
export class NotificationTimeoutError extends CodedError { | ||
info: { notification: Notification; id: string }; | ||
constructor(notificationId: string, notification: Notification) { | ||
super('ERR_NOTIFICATION_TIMEOUT', `Notification handling timed out for ID ${notificationId}.`); | ||
this.info = { id: notificationId, notification }; | ||
} | ||
} | ||
|
||
export type NotificationHandlingError = NotificationTimeoutError | Error; | ||
|
||
export interface NotificationDelegate { | ||
handleNotification: (notification: Notification) => Promise<NotificationBehavior>; | ||
handleSuccess?: (notificationId: string) => void; | ||
handleError?: (error: NotificationHandlingError) => void; | ||
} | ||
|
||
type HandleNotificationEvent = { | ||
id: string; | ||
notification: Notification; | ||
}; | ||
|
||
type HandleNotificationTimeoutEvent = HandleNotificationEvent; | ||
|
||
// Web uses SyntheticEventEmitter | ||
const notificationEmitter = new EventEmitter(NotificationsHandlerModule); | ||
|
||
const handleNotificationEventName = 'onHandleNotification'; | ||
const handleNotificationTimeoutEventName = 'onHandleNotificationTimeout'; | ||
|
||
let handleSubscription: Subscription | null = null; | ||
let handleTimeoutSubscription: Subscription | null = null; | ||
|
||
export function setNotificationDelegate(delegate: NotificationDelegate): void { | ||
if (handleSubscription) { | ||
handleSubscription.remove(); | ||
handleSubscription = null; | ||
} | ||
if (handleTimeoutSubscription) { | ||
handleTimeoutSubscription.remove(); | ||
handleTimeoutSubscription = null; | ||
} | ||
|
||
handleSubscription = notificationEmitter.addListener<HandleNotificationEvent>( | ||
handleNotificationEventName, | ||
async ({ id, notification }) => { | ||
try { | ||
const requestedBehavior = await delegate.handleNotification(notification); | ||
await NotificationsHandlerModule.handleNotificationAsync(id, requestedBehavior); | ||
// TODO: Remove eslint-disable once we upgrade to a version that supports ?. notation. | ||
// eslint-disable-next-line | ||
delegate.handleSuccess?.(id); | ||
} catch (error) { | ||
// TODO: Remove eslint-disable once we upgrade to a version that supports ?. notation. | ||
// eslint-disable-next-line | ||
delegate.handleError?.(error); | ||
} | ||
} | ||
); | ||
|
||
handleTimeoutSubscription = notificationEmitter.addListener<HandleNotificationTimeoutEvent>( | ||
handleNotificationTimeoutEventName, | ||
({ id, notification }) => delegate.handleError?.(new NotificationTimeoutError(id, notification)) | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { NativeModulesProxy, ProxyNativeModule } from '@unimodules/core'; | ||
|
||
export interface NotificationBehavior { | ||
shouldShowAlert: boolean; | ||
shouldPlaySound: boolean; | ||
shouldSetBadge: boolean; | ||
} | ||
|
||
export interface NotificationsHandlerModule extends ProxyNativeModule { | ||
handleNotificationAsync: ( | ||
notificationId: string, | ||
notificationBehavior: NotificationBehavior | ||
) => Promise<void>; | ||
} | ||
|
||
export default (NativeModulesProxy.ExpoNotificationsHandlerModule as any) as NotificationsHandlerModule; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { default as getDevicePushTokenAsync } from './getDevicePushTokenAsync'; | ||
export * from './TokenEmitter'; | ||
export * from './NotificationsEmitter'; | ||
export * from './NotificationsHandler'; |