import { DeviceEventEmitter, NativeEventEmitter, Platform } from 'react-native'; import SmartechPushReactNative from './specs/NativeSmartechPushReactNative'; const SmartechEventEmitter = Platform.select({ ios: new NativeEventEmitter(SmartechPushReactNative), android: DeviceEventEmitter }); // Store event subscriptions for cleanup const eventMap: { [key: string]: any } = {}; function defaultCallback(method: string, err: any, res: any) { if (err) { console.log('Smartech ' + method + ' default callback error', err); } else { console.log('Smartech ' + method + ' default callback result', res); } } // Used to handle callback function callWithCallback(this: any, method: string, args: any[] | null, callback: ((err: any, res: any) => void) | null) { if (typeof callback === 'undefined' || callback == null || typeof callback !== 'function') { callback = (err, res) => { defaultCallback(method, err, res); }; } if (args == null) { args = []; } args.push(callback); (SmartechPushReactNative as any)[method].apply(this, args); } const parseNestedJSON = (data: any) => { try { return JSON.parse(data); } catch (error) { return data; // Return the original string if parsing fails } }; const processNestedData = (data: any) => { for (const key in data) { if (typeof data[key] === 'object' && data[key] !== null) { // If the value is an object (nested), call the function recursively data[key] = processNestedData(data[key]); } else if (typeof data[key] === 'string') { // If the value is a string, attempt to parse it as JSON data[key] = parseNestedJSON(data[key]); } else { // Perform any necessary processing for non-string values data[key] = String(data[key]).toUpperCase(); } } return data; }; var SmartechPushReact = { // All the constants declared in the Smartech React Bridge. SmartechNotificationReceived: SmartechPushReactNative.getConstants().SmartechNotificationReceived, // This method is used to register listener. addListener: function (eventName: string, handler: (data: any) => void) { if (SmartechEventEmitter) { const subscription = SmartechEventEmitter.addListener(eventName, handler); // Store in eventMap for backward compatibility eventMap[eventName] = subscription; // Return subscription so user can remove it manually return subscription; } return undefined; }, // This method is used to unregister registered listener. removeListener: function (eventName: string) { if (eventMap[eventName]) { eventMap[eventName].remove(); delete eventMap[eventName]; } }, /** * Exposed method to request the Notification Permission * @param : Callback interface for getting status of Notification permission. */ requestNotificationPermission: function (callback: ((err: any, res: any) => void) | null) { callWithCallback('requestNotificationPermission', null, callback); }, /** * Exposed method to update the Notification Permission status * @param pnPermissionStatus: status of permission either enabled (1) or * disabled (0) */ updateNotificationPermission: function (pnPermissionStatus: number) { SmartechPushReactNative.updateNotificationPermission(pnPermissionStatus); }, // ----- GDPR Methods ----- /** * This method is used to opt push notifications. * If you call this method then we will opt in or opt out the user of recieving push notifications. */ optPushNotification: function (isPushNotificationOpted: boolean) { SmartechPushReactNative.optPushNotification(isPushNotificationOpted); }, /** * This method is used to get the current status of opt push notification. * If you call this method you will get the current status of the tracking which can be used to render the UI at app level. */ hasOptedPushNotification: function (callback: ((err: any, res: any) => void) | null) { callWithCallback('hasOptedPushNotification', null, callback); }, // ----- Helper Methods ----- /** * This method is used to get the device push token used by Smartech SDK. * If you call this method you will get the device push token which is used for sending push notification. */ getDevicePushToken: function (callback: ((err: any, res: any) => void) | null) { callWithCallback('getDevicePushToken', null, callback); }, /** * This method is used to set the device push token used by Smartech SDK. * If you call this method you will set the device push token which is used for sending push notification. */ setDevicePushToken: function (token: string) { SmartechPushReactNative.setDevicePushToken(token); }, /** * This method is used to handle notification by Smartech SDK. */ handlePushNotification: function (notificationData: object, callback: ((err: any, res: any) => void) | null) { // const _notificationdata = JSON.parse(notificationData); const parsedData = processNestedData(notificationData) callWithCallback('handlePushNotification', [JSON.stringify(parsedData)], callback); }, registerForPushNotificationWithAuthorizationOptions: function (alert: boolean, withBadge: boolean, andSound: boolean) { SmartechPushReactNative.registerForPushNotificationWithAuthorizationOptions(alert, withBadge, andSound); }, }; //module.exports = SmartechReact; export default SmartechPushReact;