Skip to content

Instantly share code, notes, and snippets.

@giacomocerquone
Last active April 3, 2021 18:59
Show Gist options
  • Save giacomocerquone/060de72e3d5b870a29c84ebd1ce80feb to your computer and use it in GitHub Desktop.
Save giacomocerquone/060de72e3d5b870a29c84ebd1ce80feb to your computer and use it in GitHub Desktop.
Useful hooks
import { useState, useEffect } from 'react';
import { Platform } from 'react-native';
import codePush from 'react-native-code-push';
import env from '../constants/env';
const { CODEPUSH_IOS_KEY, CODEPUSH_ANDROID_KEY } = env;
export default function useCodePush() {
const [ready, setReady] = useState(false);
useEffect(() => {
// We don't handle the UPDATE_INSTALLED status since
// when this function returns, the sync method will reload the app
// and the next time the status will be UP_TO_DATE
if (!__DEV__) {
codePush.sync(
{
deploymentKey:
Platform.OS === 'ios' ? CODEPUSH_IOS_KEY : CODEPUSH_ANDROID_KEY,
installMode: codePush.InstallMode.IMMEDIATE,
},
status => {
if (status === codePush.SyncStatus.UP_TO_DATE) {
setReady(true);
} else if (status === codePush.SyncStatus.UNKNOWN_ERROR) {
setReady(true);
}
return true;
},
);
} else {
setReady(true);
}
}, []);
return ready;
}
import { useRef, useLayoutEffect, useCallback } from 'react';
import { BackHandler } from 'react-native';
function useDisableBack(navigation) {
const _didFocusSub = useRef();
const _didBlurSub = useRef();
const _handler = useCallback(() => true, []);
useLayoutEffect(() => {
_didFocusSub.current = navigation.addListener('didFocus', () => {
BackHandler.addEventListener('hardwareBackPress', _handler);
});
_didBlurSub.current = navigation.addListener('didBlur', () => {
BackHandler.removeEventListener('hardwareBackPress', _handler);
});
return () => {
_didFocusSub.current.remove();
_didBlurSub.current.remove();
};
}, []);
return null;
}
export default useDisableBack;
import {useCallback, useEffect, useState} from 'react';
import {oneSignalAppId} from '@constants/keys';
import OneSignal from 'react-native-onesignal';
export default function useOneSignal() {
const [ids, setIds] = useState(null);
const [status, setStatus] = useState('idle');
console.log(oneSignalAppId);
const onReceived = useCallback((notification) => {
console.log('Notification received: ', notification);
}, []);
const onOpened = useCallback((openResult) => {
console.log('Message: ', openResult.notification.payload.body);
console.log('Data: ', openResult.notification.payload.additionalData);
console.log('isActive: ', openResult.notification.isAppInFocus);
console.log('openResult: ', openResult);
}, []);
const onIds = useCallback((device) => {
setIds(device);
setStatus('fullfilled');
}, []);
useEffect(() => {
OneSignal.setLogLevel(6, 0);
setStatus('pending');
OneSignal.init(oneSignalAppId, {
kOSSettingsKeyAutoPrompt: false,
kOSSettingsKeyInAppLaunchURL: false,
kOSSettingsKeyInFocusDisplayOption: 2,
});
OneSignal.inFocusDisplaying(2); // Controls what should happen if a notification is received while the app is open. 2 means that the notification will go directly to the device's notification center.
OneSignal.addEventListener('received', onReceived);
OneSignal.addEventListener('opened', onOpened);
OneSignal.addEventListener('ids', onIds);
return () => {
OneSignal.removeEventListener('received', onReceived);
OneSignal.removeEventListener('opened', onOpened);
OneSignal.removeEventListener('ids', onIds);
};
}, [onIds, onOpened, onReceived]);
return {...ids, status};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment