Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | 8x 16x 16x 16x 1x 1x 16x 16x 16x 16x 3x 2x 2x 1x 4x 3x 2x 1x 5x 1x 4x 4x 4x 4x 4x 3x 1x 3x 1x 2x 1x 1x 2x 1x 1x 1x 3x 1x 2x | import notifee from '@notifee/react-native';
import { isAndroid } from '@repo/utils/platform';
import {
NOTIFICATION_CHANNEL,
NOTIFICATION_CONFIG,
NOTIFICATION_LOG,
} from '@/services/notification/constants';
import type { NotificationPayload, RemoteMessage } from '@/services/notification/types';
import { sentryService } from '@/services/sentryService';
import { NotificationData } from './types';
/**
* Extract notification data (category, ticketId) from raw data object
*/
export const extractNotificationData = (
data?: Record<string, unknown>,
): NotificationData | null => {
Iif (!data || typeof data !== 'object') return null;
let ticket = data.ticket;
if (typeof ticket === 'string') {
try {
ticket = JSON.parse(ticket);
} catch {
// Not a JSON string, keep as is
}
}
const ticketObj =
typeof ticket === 'object' && ticket ? (ticket as Record<string, unknown>) : undefined;
const category = (data.category ?? ticketObj?.category ?? '') as string;
const ticketId = (data.ticketId ?? ticketObj?.id ?? '') as string;
return category ? { category, ticketId: ticketId || undefined, ...data } : null;
};
/**
* Ensure Android notification channel exists
*/
export async function ensureNotificationChannel(): Promise<void> {
if (isAndroid()) {
try {
await notifee.createChannel({
id: NOTIFICATION_CHANNEL.ID,
name: NOTIFICATION_CHANNEL.NAME,
importance: NOTIFICATION_CHANNEL.IMPORTANCE,
sound: 'default',
vibration: true,
});
} catch (error) {
sentryService.captureException(error, {
tags: {
component: NOTIFICATION_LOG.COMPONENT,
action: NOTIFICATION_LOG.ACTIONS.CREATE_CHANNEL,
},
extra: {
channelId: NOTIFICATION_CHANNEL.ID,
channelName: NOTIFICATION_CHANNEL.NAME,
},
});
}
}
}
/**
* Extract body text from data object with fallback priority:
* body > message > text
*/
function extractBodyFromData(data: Record<string, unknown>): string | undefined {
if (typeof data.body === 'string') return data.body;
if (typeof data.message === 'string') return data.message;
if (typeof data.text === 'string') return data.text;
return undefined;
}
/**
* Convert remote message to notification payload
*/
export function extractNotificationFromMessage(
remoteMessage: RemoteMessage,
): NotificationPayload | null {
if (remoteMessage.notification) {
return {
title: remoteMessage.notification.title,
body: remoteMessage.notification.body,
data: remoteMessage.data as Record<string, unknown> | undefined,
};
}
Eif (remoteMessage.data) {
const data = remoteMessage.data as Record<string, unknown>;
const title = typeof data.title === 'string' ? data.title : undefined;
const body = extractBodyFromData(data);
// Only return notification if we have at least title or body
// This prevents blocking, but ensures we have something to display
if (title || body) {
return {
title,
body,
data,
};
}
}
return null;
}
/**
* Convert unknown value to Notifee-compatible data value.
* Notifee only accepts string, number, or object types.
*
* @param value - The value to convert
* @returns A Notifee-compatible value (string, number, or object)
*/
function convertToNotifeeDataValue(value: unknown): string | number | object {
if (typeof value === 'string' || typeof value === 'number') {
return value;
}
if (typeof value === 'object' && value !== null) {
return value;
}
// Convert other types (boolean, null, undefined, etc.) to string
return String(value);
}
/**
* Convert data object to Notifee-compatible format.
* All values in the data object are converted to Notifee-compatible types.
*
* @param data - The data object to convert
* @returns A Notifee-compatible data object
*/
function convertToNotifeeData(data: Record<string, unknown> | undefined): {
[key: string]: string | number | object;
} {
if (!data) {
return {};
}
const result: { [key: string]: string | number | object } = {};
for (const [key, value] of Object.entries(data)) {
result[key] = convertToNotifeeDataValue(value);
}
return result;
}
/**
* Create notification data for Notifee
*/
export function createNotificationData(
notification: NotificationPayload,
): Parameters<typeof notifee.displayNotification>[0] {
return {
title: notification.title || 'Notification',
body: notification.body || '',
data: convertToNotifeeData(notification.data),
android: NOTIFICATION_CONFIG.ANDROID,
ios: NOTIFICATION_CONFIG.IOS,
};
}
|