UNPKG

@selfcommunity/react-core

Version:

React Core Components useful for integrating UI Community components (react-ui).

144 lines (143 loc) • 7.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = require("react"); const SCContextProvider_1 = require("../components/provider/SCContextProvider"); const SCUserProvider_1 = require("../components/provider/SCUserProvider"); const utils_1 = require("@selfcommunity/utils"); const Errors_1 = require("../constants/Errors"); const api_services_1 = require("@selfcommunity/api-services"); const Device_1 = require("../constants/Device"); const notification_1 = require("../utils/notification"); /** :::info This custom hook is used to manage native push notification: - register device (Android/iOS) - unregister device (Android/iOS) !important: the device is registered only if exist in the global window object (or as keys in the localstorage) the follow: - _platform: `<android-iOS>` - _notification_service: `<gcm-fcm-apns>` - _registration_id: `<registration_id>` - _device_id: `<device_id>` ::: */ function useSCMobileNativePushMessaging() { const scContext = (0, SCContextProvider_1.useSCContext)(); const scUserContext = (0, SCUserProvider_1.useSCUser)(); const [mnpmInstance, setMnpmInstance] = (0, react_1.useState)(null); const [isLoading, setLoading] = (0, react_1.useState)(false); /** * Validate notification service * @param data */ const isValid = (data) => { return (data && data.registration_id && ((data.platform === Device_1.PLATFORM.ANDROID && (data.notification_service === Device_1.ANDROID_PUSH_NOTIFICATION_GCM_DEVICE_TYPE || data.notification_service === Device_1.ANDROID_PUSH_NOTIFICATION_FCM_DEVICE_TYPE)) || (data.platform === Device_1.PLATFORM.IOS && (data.notification_service === Device_1.IOS_PUSH_NOTIFICATION_IOS_DEVICE_TYPE || data.notification_service === Device_1.ANDROID_PUSH_NOTIFICATION_FCM_DEVICE_TYPE)))); }; /** * Perform device registration * @param data * @param remove */ const performUpdateDevice = (data, remove = false) => { const url = remove ? api_services_1.Endpoints.DeleteDevice.url({ type: data.notification_service, id: data.registration_id }) : api_services_1.Endpoints.NewDevice.url({ type: data.notification_service }); const method = remove ? api_services_1.Endpoints.DeleteDevice.method : api_services_1.Endpoints.NewDevice.method; setLoading(true); return api_services_1.http .request(Object.assign({ url, method }, (remove ? {} : { data: Object.assign({ registration_id: data.registration_id, cloud_message_type: data.notification_service.toUpperCase(), name: `${data.platform} device` }, (data.device_id ? { device_id: data.device_id } : {})), }))) .then((res) => { if (res.status >= 300) { return Promise.reject(res); } setLoading(false); return Promise.resolve(res.data); }) .catch((e) => { setLoading(false); return Promise.reject(e); }); }; /** * Collect data */ const getDataInstance = () => { if (window && window[Device_1.PLATFORM_KEY] && Device_1.PLATFORMS.includes(window[Device_1.PLATFORM_KEY]) && window[Device_1.REGISTRATION_ID_KEY] && window[Device_1.NOTIFICATION_SERVICE_KEY] && Device_1.NOTIFICATIONS_SERVICES.includes(window[Device_1.NOTIFICATION_SERVICE_KEY])) { return Object.assign({ platform: window[Device_1.PLATFORM_KEY], registration_id: window[Device_1.REGISTRATION_ID_KEY], notification_service: window[Device_1.NOTIFICATION_SERVICE_KEY] }, (window[Device_1.DEVICE_ID_KEY] ? { device_id: window[Device_1.DEVICE_ID_KEY] } : {})); } else if (utils_1.LocalStorageDB.checkifSupport() && utils_1.LocalStorageDB.get(Device_1.PLATFORM_KEY) && Device_1.PLATFORMS.includes(utils_1.LocalStorageDB.get(Device_1.PLATFORM_KEY)) && utils_1.LocalStorageDB.get(Device_1.REGISTRATION_ID_KEY) && utils_1.LocalStorageDB.get(Device_1.NOTIFICATION_SERVICE_KEY) && Device_1.NOTIFICATIONS_SERVICES.includes(utils_1.LocalStorageDB.get(Device_1.NOTIFICATION_SERVICE_KEY))) { return Object.assign({ platform: utils_1.LocalStorageDB.get(Device_1.PLATFORM_KEY), registration_id: utils_1.LocalStorageDB.get(Device_1.REGISTRATION_ID_KEY), notification_service: utils_1.LocalStorageDB.get(Device_1.NOTIFICATION_SERVICE_KEY) }, (window[Device_1.DEVICE_ID_KEY] ? { device_id: window[Device_1.DEVICE_ID_KEY] } : {})); } else { return null; } }; /** * Unsubscribe device */ const unsubscribeDevice = () => { if (mnpmInstance && !isLoading) { utils_1.Logger.info(Errors_1.SCOPE_SC_CORE, 'Mobile native notification is disabled. Unregister the device.'); performUpdateDevice(mnpmInstance, true) .then((res) => { setMnpmInstance(null); utils_1.Logger.info(Errors_1.SCOPE_SC_CORE, 'Device unregistration successful. Your device will not be able to receive mobile push notifications.'); }) .catch(() => { setMnpmInstance(null); }); } }; /** * Check if there is a currently active session and a * instance when the provider is mounted for the first time. */ (0, react_1.useEffect)(() => { if (scUserContext.user && (0, notification_1.isMobileNativeNotificationEnabled)() && !scContext.settings.notifications.mobileNativePushMessaging.disable) { const _data = getDataInstance(); if (isValid(_data)) { if ((!mnpmInstance || (mnpmInstance && mnpmInstance.registration_id !== _data.registration_id)) && !isLoading) { utils_1.Logger.info(Errors_1.SCOPE_SC_CORE, 'Mobile native notification is enabled. Checking and validate data...'); // Register the device only if app-platform and app-registrationId and app-notificationService // exists in window/localStorage utils_1.Logger.info(Errors_1.SCOPE_SC_CORE, 'Data is valid to register the device for receive mobile push notification.'); performUpdateDevice(_data).then((res) => { setMnpmInstance(Object.assign(Object.assign({}, _data), { id: res.id })); utils_1.Logger.info(Errors_1.SCOPE_SC_CORE, 'Device registration successful. Your device will now be able to receive mobile push notifications.'); }); } } else { utils_1.Logger.warn(Errors_1.SCOPE_SC_CORE, 'Invalid data. Unable to register the device for native push notification.'); unsubscribeDevice(); } } else { unsubscribeDevice(); } }); return { mnpmInstance, setMnpmInstance }; } exports.default = useSCMobileNativePushMessaging;