diff --git a/packages/react-native/src/types/NotificationAndroid.ts b/packages/react-native/src/types/NotificationAndroid.ts index e3ea13d2..67ed5b0c 100644 --- a/packages/react-native/src/types/NotificationAndroid.ts +++ b/packages/react-native/src/types/NotificationAndroid.ts @@ -753,6 +753,17 @@ export interface AndroidMessagingStyleMessage { person?: AndroidPerson; } + +export enum DefaultActionId { + ANSWER = 'answer', + DECLINE = 'decline', + HANG_UP = 'hangUp', +} + +export interface CallStyleAction { + pressAction: Omit & { id?: string }; +} + export const enum AndroidCallType { INCOMING = 1, ONGOING = 2, @@ -761,19 +772,19 @@ export const enum AndroidCallType { export interface AndroidCallTypeIncoming { callType: AndroidCallType.INCOMING; - answerAction: AndroidAction; - declineAction: AndroidAction; + answerAction?: CallStyleAction; + declineAction?: CallStyleAction; } export interface AndroidCallTypeOngoing { callType: AndroidCallType.ONGOING; - hangUpAction: AndroidAction; + hangUpAction?: CallStyleAction; } export interface AndroidCallTypeScreening { callType: AndroidCallType.SCREENING; - answerAction: AndroidAction; - hangUpAction: AndroidAction; + answerAction?: CallStyleAction; + hangUpAction?: CallStyleAction; } /** diff --git a/packages/react-native/src/validators/validateAndroidStyle.ts b/packages/react-native/src/validators/validateAndroidStyle.ts index a28aa385..7bf5c6ca 100644 --- a/packages/react-native/src/validators/validateAndroidStyle.ts +++ b/packages/react-native/src/validators/validateAndroidStyle.ts @@ -12,9 +12,11 @@ import { AndroidCallStyle, AndroidStyle, AndroidCallType, + CallStyleAction, + DefaultActionId, } from '../types/NotificationAndroid'; import { objectHasProperty, isArray, isBoolean, isNumber, isObject, isString } from '../utils'; -import validateAndroidAction from './validateAndroidAction'; +import validateAndroidPressAction from './validateAndroidPressAction'; /** * Validates a BigPictureStyle @@ -345,8 +347,8 @@ export function validateAndroidCallStyle(style: AndroidCallStyle): AndroidCallSt switch (style.callTypeActions.callType) { case AndroidCallType.INCOMING: { - const answerAction = validateAndroidAction(style.callTypeActions.answerAction) - const declineAction = validateAndroidAction(style.callTypeActions.declineAction) + const answerAction = validateCallStyleAction(style.callTypeActions.answerAction, DefaultActionId.ANSWER) + const declineAction = validateCallStyleAction(style.callTypeActions.declineAction, DefaultActionId.DECLINE) return { type: AndroidStyle.CALL, person, @@ -358,7 +360,7 @@ export function validateAndroidCallStyle(style: AndroidCallStyle): AndroidCallSt }; } case AndroidCallType.ONGOING: { - const hangUpAction = validateAndroidAction(style.callTypeActions.hangUpAction) + const hangUpAction = validateCallStyleAction(style.callTypeActions.hangUpAction, DefaultActionId.HANG_UP) return { type: AndroidStyle.CALL, person, @@ -369,8 +371,8 @@ export function validateAndroidCallStyle(style: AndroidCallStyle): AndroidCallSt }; } case AndroidCallType.SCREENING: { - const answerAction = validateAndroidAction(style.callTypeActions.answerAction) - const hangUpAction = validateAndroidAction(style.callTypeActions.hangUpAction) + const answerAction = validateCallStyleAction(style.callTypeActions.answerAction, DefaultActionId.ANSWER) + const hangUpAction = validateCallStyleAction(style.callTypeActions.hangUpAction, DefaultActionId.HANG_UP) return { type: AndroidStyle.CALL, person, @@ -384,3 +386,13 @@ export function validateAndroidCallStyle(style: AndroidCallStyle): AndroidCallSt default: throw new Error("'callType' expected a value of 0, 1 or 2."); } } + +function validateCallStyleAction(action: CallStyleAction | undefined, defaultPressActionId: string): CallStyleAction { + const { pressAction } = (action || { pressAction : { id: defaultPressActionId }}); + try { + const out = validateAndroidPressAction({...pressAction, id: pressAction.id || defaultPressActionId}); + return { pressAction: out }; + } catch (e: any) { + throw new Error(`'action' ${e.message}.`); + } +} diff --git a/tests_react_native/example/notifications.ts b/tests_react_native/example/notifications.ts index 40b89fce..8cbeb9e9 100644 --- a/tests_react_native/example/notifications.ts +++ b/tests_react_native/example/notifications.ts @@ -25,8 +25,8 @@ export const notifications: { key: string; notification: Notification | Notifica person: { name: 'John Doe' }, callTypeActions: { callType: 1, - answerAction: { title: 'Answer', pressAction: { id: 'answer' } }, - declineAction: { title: 'Decline', pressAction: { id: 'decline' } }, + answerAction: { pressAction: { id: 'answer' } }, + declineAction: { pressAction: { id: 'stop' } }, }, }, },