@bigbinary/neeto-commons-rn 1.4.8 → 1.4.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.usePushNotification=void 0;var _asyncToGenerator2=_interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));var _slicedToArray2=_interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));var _react=require("react");var _reactNative=require("react-native");var _dayjs=_interopRequireDefault(require("dayjs"));var _reactNativeNotifications=require("react-native-notifications");var _reactNativePermissions=require("react-native-permissions");var _reactNativePushNotification=_interopRequireDefault(require("react-native-push-notification"));var _bugsnag=require("../../config/bugsnag");var _constants=require("../../constants");var _asyncStore=require("../asyncStore");var _useNotification2=require("../../hooks/query/useNotification");var _useAppState=require("../../hooks/useAppState");var _store=require("../../store");var usePushNotification=exports.usePushNotification=function usePushNotification(isLoggedIn){var _ref=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{handleClick:function handleClick(){}},handleClick=_ref.handleClick;var appState=(0,_useAppState.useAppState)();var _useState=(0,_react.useState)(false),_useState2=(0,_slicedToArray2.default)(_useState,2),notificationPermission=_useState2[0],setNotificationPermission=_useState2[1];var _useNotification=(0,_useNotification2.useNotification)(),useRegisterTokenMutation=_useNotification.useRegisterTokenMutation,useMarkReadUnreadNotification=_useNotification.useMarkReadUnreadNotification;var _useRegisterTokenMuta=useRegisterTokenMutation(),registerToken=_useRegisterTokenMuta.mutate;var _useMarkReadUnreadNot=useMarkReadUnreadNotification(),markReadUnreadNotification=_useMarkReadUnreadNot.mutate;(0,_react.useEffect)(function(){var checkPermission=function(){var _ref2=(0,_asyncToGenerator2.default)(function*(){try{if(_reactNative.Platform.OS==="android"&&_reactNative.Platform.Version<33){setNotificationPermission(true);return;}var _yield$checkNotificat=yield(0,_reactNativePermissions.checkNotifications)(),status=_yield$checkNotificat.status;setNotificationPermission(status===_reactNativePermissions.RESULTS.GRANTED);}catch(error){(0,_bugsnag.bugsnagErrorHandler)(JSON.stringify(error),"notification");}});return function checkPermission(){return _ref2.apply(this,arguments);};}();if(appState==="active"){checkPermission();}},[appState]);var registerNotificationEvents=(0,_react.useCallback)(function(){var foregroundEvent=_reactNativeNotifications.Notifications.events().registerNotificationReceivedForeground(function(notification,completion){if(_reactNative.Platform.OS==="android"){var _notification$payload,_notification$payload2,_notification$payload3,_notification$payload4,_notification$payload5;var notificationTitle=(notification==null?void 0:(_notification$payload=notification.payload)==null?void 0:_notification$payload["gcm.notification.title"])||(notification==null?void 0:(_notification$payload2=notification.payload)==null?void 0:_notification$payload2.title)||"New Notification";var notificationBody=(notification==null?void 0:(_notification$payload3=notification.payload)==null?void 0:_notification$payload3["gcm.notification.body"])||(notification==null?void 0:(_notification$payload4=notification.payload)==null?void 0:_notification$payload4.message)||"You have a new message";_reactNativeNotifications.Notifications.postLocalNotification({body:notificationBody,title:notificationTitle,url:notification==null?void 0:(_notification$payload5=notification.payload)==null?void 0:_notification$payload5.url,channelId:_reactNative.Platform.OS==="android"?"default":undefined,android_channel_id:"default"});}completion({alert:true,sound:true,badge:false});});var backgroundEvent=_reactNativeNotifications.Notifications.events().registerNotificationReceivedBackground(function(notification,completion){var _notification$payload6,_notification$payload7,_notification$payload8,_notification$payload9,_notification$payload0;var notificationTitle=(notification==null?void 0:(_notification$payload6=notification.payload)==null?void 0:_notification$payload6["gcm.notification.title"])||(notification==null?void 0:(_notification$payload7=notification.payload)==null?void 0:_notification$payload7.title)||"New Notification";var notificationBody=(notification==null?void 0:(_notification$payload8=notification.payload)==null?void 0:_notification$payload8["gcm.notification.body"])||(notification==null?void 0:(_notification$payload9=notification.payload)==null?void 0:_notification$payload9.message)||"You have a new message";_reactNativeNotifications.Notifications.postLocalNotification({body:notificationBody,title:notificationTitle,url:notification==null?void 0:(_notification$payload0=notification.payload)==null?void 0:_notification$payload0.url,channelId:_reactNative.Platform.OS==="android"?"default":undefined,android_channel_id:"default"});completion({alert:true,sound:true,badge:false});});var openedEvent=_reactNativeNotifications.Notifications.events().registerNotificationOpened(function(notification,completion){markNotificationsAsRead(notification);completion();});if(_reactNative.Platform.OS==="android"){var defaultNotificationChannel={channelId:"default",name:"Default Channel",importance:5,sound:"default",enableVibration:true,enableLights:true,showBadge:true};var channel01NotificationChannel={channelId:"channel_01",name:"Channel 01",importance:5,enableLights:true,enableVibration:true,showBadge:true};_reactNativeNotifications.Notifications.setNotificationChannel(defaultNotificationChannel);_reactNativeNotifications.Notifications.setNotificationChannel(channel01NotificationChannel);}return[foregroundEvent,backgroundEvent,openedEvent];},[]);(0,_react.useEffect)(function(){var events=registerNotificationEvents();return function(){events.forEach(function(event){return event.remove();});};},[registerNotificationEvents]);(0,_react.useEffect)(function(){if(isLoggedIn&&appState==="active"&¬ificationPermission){initializePN();}},[initializePN,isLoggedIn,appState,notificationPermission]);var registerForRemoteNotifications=(0,_react.useCallback)(function(){_reactNativeNotifications.Notifications.registerRemoteNotifications();var registerDeviceOnce=function(){var isRegistered=false;return function(){var _ref3=(0,_asyncToGenerator2.default)(function*(event){var _Store$getState,_Store$getState$auth;if(isRegistered)return;var isLoggedIn=((_Store$getState=_store.Store.getState())==null?void 0:(_Store$getState$auth=_Store$getState.auth)==null?void 0:_Store$getState$auth.isLoggedIn)||false;if(isLoggedIn&&event.deviceToken){isRegistered=true;var payload={device:{device_token:event.deviceToken,platform:_reactNative.Platform.OS}};var notificationDeviceId=yield(0,_asyncStore.getItem)(_constants.STORAGE_KEYS.DEVICE_TOKEN);if(typeof notificationDeviceId!=="string"){registerToken(payload,{onSuccess:function(){var _onSuccess=(0,_asyncToGenerator2.default)(function*(){yield(0,_asyncStore.setItem)(_constants.STORAGE_KEYS.DEVICE_REGISTERED,true);});function onSuccess(){return _onSuccess.apply(this,arguments);}return onSuccess;}(),onError:function onError(){isRegistered=false;}});}}});return function(_x){return _ref3.apply(this,arguments);};}();}();_reactNativeNotifications.Notifications.events().registerRemoteNotificationsRegistered(function(event){registerDeviceOnce(event);});_reactNativeNotifications.Notifications.events().registerRemoteNotificationsRegistrationFailed(function(event){(0,_bugsnag.bugsnagErrorHandler)(JSON.stringify(event),"notification");});},[registerToken]);var initializePN=(0,_react.useCallback)((0,_asyncToGenerator2.default)(function*(){try{var isRegisteredAlready=yield _reactNativeNotifications.Notifications.isRegisteredForRemoteNotifications();var storedDeviceToken=yield(0,_asyncStore.getItem)(_constants.STORAGE_KEYS.DEVICE_TOKEN);var deviceRegistered=yield(0,_asyncStore.getItem)(_constants.STORAGE_KEYS.DEVICE_REGISTERED);if(isRegisteredAlready&&storedDeviceToken&&deviceRegistered)return;registerForRemoteNotifications();}catch(error){(0,_bugsnag.bugsnagErrorHandler)(JSON.stringify(error),"notification");}}),[registerForRemoteNotifications]);var markNotificationsAsRead=function markNotificationsAsRead(notification){var _notification$payload1,_notification$payload10,_notification$payload11,_notification$payload12;var notificationId=(notification==null?void 0:(_notification$payload1=notification.payload)==null?void 0:_notification$payload1.notification_id)||(notification==null?void 0:(_notification$payload10=notification.payload)==null?void 0:(_notification$payload11=_notification$payload10.aps)==null?void 0:(_notification$payload12=_notification$payload11.alert)==null?void 0:_notification$payload12.notification_id);handleClick(notification);if(notificationId){markReadUnreadNotification({readAt:(0,_dayjs.default)().toISOString(),notificationId:notificationId});}};_reactNativePushNotification.default.configure({onNotification:function onNotification(notification){return handleClick(notification);}});return[];};
|
|
1
|
+
var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.usePushNotification=void 0;var _asyncToGenerator2=_interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));var _slicedToArray2=_interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));var _react=require("react");var _reactNative=require("react-native");var _dayjs=_interopRequireDefault(require("dayjs"));var _reactNativeNotifications=require("react-native-notifications");var _reactNativePermissions=require("react-native-permissions");var _reactNativePushNotification=_interopRequireDefault(require("react-native-push-notification"));var _bugsnag=require("../../config/bugsnag");var _constants=require("../../constants");var _asyncStore=require("../asyncStore");var _useNotification2=require("../../hooks/query/useNotification");var _useAppState=require("../../hooks/useAppState");var _store=require("../../store");var usePushNotification=exports.usePushNotification=function usePushNotification(isLoggedIn){var _ref=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{handleClick:function handleClick(){}},handleClick=_ref.handleClick;var appState=(0,_useAppState.useAppState)();var _useState=(0,_react.useState)(false),_useState2=(0,_slicedToArray2.default)(_useState,2),notificationPermission=_useState2[0],setNotificationPermission=_useState2[1];var _useNotification=(0,_useNotification2.useNotification)(),useRegisterTokenMutation=_useNotification.useRegisterTokenMutation,useMarkReadUnreadNotification=_useNotification.useMarkReadUnreadNotification;var _useRegisterTokenMuta=useRegisterTokenMutation(),registerToken=_useRegisterTokenMuta.mutate;var _useMarkReadUnreadNot=useMarkReadUnreadNotification(),markReadUnreadNotification=_useMarkReadUnreadNot.mutate;(0,_react.useEffect)(function(){var checkPermission=function(){var _ref2=(0,_asyncToGenerator2.default)(function*(){try{if(_reactNative.Platform.OS==="android"&&_reactNative.Platform.Version<33){setNotificationPermission(true);return;}var _yield$checkNotificat=yield(0,_reactNativePermissions.checkNotifications)(),status=_yield$checkNotificat.status;setNotificationPermission(status===_reactNativePermissions.RESULTS.GRANTED);}catch(error){(0,_bugsnag.bugsnagErrorHandler)(JSON.stringify(error),"notification");}});return function checkPermission(){return _ref2.apply(this,arguments);};}();if(appState==="active"){checkPermission();}},[appState]);var registerNotificationEvents=(0,_react.useCallback)(function(){var foregroundEvent=_reactNativeNotifications.Notifications.events().registerNotificationReceivedForeground(function(notification,completion){if(_reactNative.Platform.OS==="android"){var _notification$payload,_notification$payload2,_notification$payload3,_notification$payload4,_notification$payload5;var notificationTitle=(notification==null?void 0:(_notification$payload=notification.payload)==null?void 0:_notification$payload["gcm.notification.title"])||(notification==null?void 0:(_notification$payload2=notification.payload)==null?void 0:_notification$payload2.title)||"New Notification";var notificationBody=(notification==null?void 0:(_notification$payload3=notification.payload)==null?void 0:_notification$payload3["gcm.notification.body"])||(notification==null?void 0:(_notification$payload4=notification.payload)==null?void 0:_notification$payload4.message)||"You have a new message";_reactNativeNotifications.Notifications.postLocalNotification({body:notificationBody,title:notificationTitle,url:notification==null?void 0:(_notification$payload5=notification.payload)==null?void 0:_notification$payload5.url,channelId:_reactNative.Platform.OS==="android"?"default":undefined,android_channel_id:"default"});}completion({alert:true,sound:true,badge:false});});var backgroundEvent=_reactNativeNotifications.Notifications.events().registerNotificationReceivedBackground(function(notification,completion){if(_reactNative.Platform.OS==="android"){var _notification$payload6,_notification$payload7,_notification$payload8,_notification$payload9,_notification$payload0;var notificationTitle=(notification==null?void 0:(_notification$payload6=notification.payload)==null?void 0:_notification$payload6["gcm.notification.title"])||(notification==null?void 0:(_notification$payload7=notification.payload)==null?void 0:_notification$payload7.title)||"New Notification";var notificationBody=(notification==null?void 0:(_notification$payload8=notification.payload)==null?void 0:_notification$payload8["gcm.notification.body"])||(notification==null?void 0:(_notification$payload9=notification.payload)==null?void 0:_notification$payload9.message)||"You have a new message";_reactNativeNotifications.Notifications.postLocalNotification({body:notificationBody,title:notificationTitle,url:notification==null?void 0:(_notification$payload0=notification.payload)==null?void 0:_notification$payload0.url,channelId:_reactNative.Platform.OS==="android"?"default":undefined,android_channel_id:"default"});}completion({alert:true,sound:true,badge:false});});var openedEvent=_reactNativeNotifications.Notifications.events().registerNotificationOpened(function(notification,completion){markNotificationsAsRead(notification);completion();});if(_reactNative.Platform.OS==="android"){var defaultNotificationChannel={channelId:"default",name:"Default Channel",importance:5,sound:"default",enableVibration:true,enableLights:true,showBadge:true};var channel01NotificationChannel={channelId:"channel_01",name:"Channel 01",importance:5,enableLights:true,enableVibration:true,showBadge:true};_reactNativeNotifications.Notifications.setNotificationChannel(defaultNotificationChannel);_reactNativeNotifications.Notifications.setNotificationChannel(channel01NotificationChannel);}return[foregroundEvent,backgroundEvent,openedEvent];},[]);(0,_react.useEffect)(function(){var events=registerNotificationEvents();return function(){events.forEach(function(event){return event.remove();});};},[registerNotificationEvents]);(0,_react.useEffect)(function(){if(isLoggedIn&&appState==="active"&¬ificationPermission){initializePN();}},[initializePN,isLoggedIn,appState,notificationPermission]);var registerForRemoteNotifications=(0,_react.useCallback)(function(){_reactNativeNotifications.Notifications.registerRemoteNotifications();var registerDeviceOnce=function(){var isRegistered=false;return function(){var _ref3=(0,_asyncToGenerator2.default)(function*(event){var _Store$getState,_Store$getState$auth;if(isRegistered)return;var isLoggedIn=((_Store$getState=_store.Store.getState())==null?void 0:(_Store$getState$auth=_Store$getState.auth)==null?void 0:_Store$getState$auth.isLoggedIn)||false;if(isLoggedIn&&event.deviceToken){isRegistered=true;var payload={device:{device_token:event.deviceToken,platform:_reactNative.Platform.OS}};var notificationDeviceId=yield(0,_asyncStore.getItem)(_constants.STORAGE_KEYS.DEVICE_TOKEN);if(typeof notificationDeviceId!=="string"){registerToken(payload,{onSuccess:function(){var _onSuccess=(0,_asyncToGenerator2.default)(function*(){yield(0,_asyncStore.setItem)(_constants.STORAGE_KEYS.DEVICE_REGISTERED,true);});function onSuccess(){return _onSuccess.apply(this,arguments);}return onSuccess;}(),onError:function onError(){isRegistered=false;}});}}});return function(_x){return _ref3.apply(this,arguments);};}();}();_reactNativeNotifications.Notifications.events().registerRemoteNotificationsRegistered(function(event){registerDeviceOnce(event);});_reactNativeNotifications.Notifications.events().registerRemoteNotificationsRegistrationFailed(function(event){(0,_bugsnag.bugsnagErrorHandler)(JSON.stringify(event),"notification");});},[registerToken]);var initializePN=(0,_react.useCallback)((0,_asyncToGenerator2.default)(function*(){try{var isRegisteredAlready=yield _reactNativeNotifications.Notifications.isRegisteredForRemoteNotifications();var storedDeviceToken=yield(0,_asyncStore.getItem)(_constants.STORAGE_KEYS.DEVICE_TOKEN);var deviceRegistered=yield(0,_asyncStore.getItem)(_constants.STORAGE_KEYS.DEVICE_REGISTERED);if(isRegisteredAlready&&storedDeviceToken&&deviceRegistered)return;registerForRemoteNotifications();}catch(error){(0,_bugsnag.bugsnagErrorHandler)(JSON.stringify(error),"notification");}}),[registerForRemoteNotifications]);var markNotificationsAsRead=function markNotificationsAsRead(notification){var _notification$payload1,_notification$payload10,_notification$payload11,_notification$payload12;var notificationId=(notification==null?void 0:(_notification$payload1=notification.payload)==null?void 0:_notification$payload1.notification_id)||(notification==null?void 0:(_notification$payload10=notification.payload)==null?void 0:(_notification$payload11=_notification$payload10.aps)==null?void 0:(_notification$payload12=_notification$payload11.alert)==null?void 0:_notification$payload12.notification_id);handleClick(notification);if(notificationId){markReadUnreadNotification({readAt:(0,_dayjs.default)().toISOString(),notificationId:notificationId});}};_reactNativePushNotification.default.configure({onNotification:function onNotification(notification){return handleClick(notification);}});return[];};
|