import React, {useState, useEffect} from 'react';
import {
View,
TextInput,
TouchableOpacity,
Text,
StyleSheet,
Image,
SafeAreaView,
ScrollView,
StatusBar,
Alert,
Switch,
Platform,
} from 'react-native';
import {check, PERMISSIONS, request, RESULTS} from 'react-native-permissions';
import MCReactModule, {CustomEvent} from 'react-native-marketingcloudsdk';
import Toast from 'react-native-root-toast';
const HomeScreen = ({ navigation }: any) => {
return (
);
};
const Push = () => {
const [isPushEnabled, setPushEnabled] = useState(false);
const [pushToken, setPushToken] = useState('');
const requestNotificationPermission = async () => {
try {
if (Platform.OS === 'android') {
let checkPerm = await check(PERMISSIONS.ANDROID.POST_NOTIFICATIONS);
if(checkPerm != "granted"){
let result = await request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS);
if (result === RESULTS.GRANTED) {
console.log('Notification permission granted');
} else {
console.log('Notification permission denied');
}
}
}
} catch (err) {
console.warn('requestNotificationPermission error: ', err);
}
};
const updatePushData = async () => {
let enabled = await MCReactModule.isPushEnabled();
let systemToken = await MCReactModule.getSystemToken();
setPushEnabled(enabled);
setPushToken(systemToken || '');
};
const handleSystemTokenPress = async () => {
let systemToken = await MCReactModule.getSystemToken();
setPushToken(systemToken || '');
Toast.show('System Token: ' + systemToken);
};
const togglePush = async () => {
if (isPushEnabled) {
MCReactModule.disablePush();
Toast.show('Push Disabled');
setPushEnabled(false);
} else {
MCReactModule.enablePush();
Toast.show('Push Enabled');
setPushEnabled(true);
}
let systemToken = await MCReactModule.getSystemToken();
setPushToken(systemToken || '');
};
useEffect(() => {
updatePushData();
requestNotificationPermission();
MCReactModule.enableLogging();
}, []);
return (
Push Token: {pushToken}
Get System Token
{isPushEnabled ? 'Disable Push' : 'Enable Push'}
);
};
const Tags = () => {
const [inputText, setInputText] = useState('');
const [tags, setTags] = useState([]);
useEffect(() => {
updateTags();
}, []);
const updateTags = async (msg?: string) => {
return Promise.resolve()
.then(MCReactModule.getTags)
.then(val => {
setTags(val || []);
msg && Toast.show(msg);
return val;
});
};
const removeTag = async () => {
MCReactModule.removeTag(inputText);
setInputText('');
updateTags('Tag removed');
};
const handleTags = async () => {
MCReactModule.addTag(inputText);
setInputText('');
updateTags('Tag added');
};
return (
Tags: {tags.join(', ')}
Add Tag
updateTags('Tags updated')}>
Get Tags
Remove Tag
);
};
const ContactKey = () => {
const [inputText, setInputText] = useState('');
const [contactKey, setContactKey] = useState('');
useEffect(() => {
updateContactKey();
}, []);
const updateContactKey = async (msg?: string) => {
return Promise.resolve()
.then(MCReactModule.getContactKey)
.then(val => {
setContactKey(val || '');
msg && Toast.show(msg);
return val;
});
};
const handleContactKey = async () => {
if (inputText === '') {
Toast.show('Please enter valid contact key');
return;
}
MCReactModule.setContactKey(inputText);
setInputText('');
updateContactKey('ContactKey is set');
};
return (
Contact Key: {contactKey}
Set Contact Key
updateContactKey('Contact Key updated')}>
Get Contact Key
);
};
const Attributes = () => {
const [attributes, setAttributes] = useState({});
useEffect(() => {
updateAttributes();
}, []);
const updateAttributes = async (msg?: string) => {
return Promise.resolve()
.then(MCReactModule.getAttributes)
.then(val => {
setAttributes(val || {});
msg && Toast.show(msg);
return val;
});
};
const handleAddKeyValue = async () => {
const newKey = keyInput.trim();
const newValue = valueInput.trim();
if (newKey && newValue && newKey !== '' && newValue !== '') {
MCReactModule.setAttribute(newKey, newValue);
setKeyInput('');
setValueInput('');
updateAttributes('Attribute added');
} else {
Toast.show('Please enter valid key and value');
}
};
const handleClearAttribute = async () => {
const newKey = keyInput.trim();
if (keyInput === '') {
Toast.show('Please enter key to remove');
return;
}
if (newKey && newKey !== '') {
MCReactModule.clearAttribute(newKey);
setKeyInput('');
setValueInput('');
updateAttributes('Attribute removed with key: ' + newKey);
} else {
Toast.show('Please enter key to remove');
}
};
const [keyInput, setKeyInput] = useState('');
const [valueInput, setValueInput] = useState('');
return (
attributes: {JSON.stringify(attributes)}
Add Attribute
Clear Attribute with Key
updateAttributes('Attributes updated')}>
Get Attributes
);
};
const FeatureToggle = () => {
const [isAnalyticsEnabled, setAnalyticsEnabledState] = useState(false);
const [isPiAnalyticsEnabled, setPiAnalyticsEnabledState] = useState(false);
useEffect(() => {
const fetchData = async () => {
setAnalyticsEnabledState(await MCReactModule.isAnalyticsEnabled());
setPiAnalyticsEnabledState(await MCReactModule.isPiAnalyticsEnabled());
};
fetchData();
}, []);
const toggleFeature = async (
featureName: string,
currentValue: boolean,
setterFunction: React.Dispatch>,
toggleFunction: (enabled: boolean) => void
) => {
toggleFunction(!currentValue);
setterFunction(!currentValue);
Toast.show(`${featureName} is ${!currentValue ? 'Enabled' : 'Disabled'}`);
};
return (
Analytics
toggleFeature('Analytics', isAnalyticsEnabled, setAnalyticsEnabledState, MCReactModule.setAnalyticsEnabled)}
value={isAnalyticsEnabled}
/>
PI Analytics
toggleFeature('PI Analytics', isPiAnalyticsEnabled, setPiAnalyticsEnabledState, MCReactModule.setPiAnalyticsEnabled)}
value={isPiAnalyticsEnabled}
/>
);
};
const Logging = ( {navigation}: any) => {
const [deviceId, setDeviceId] = useState('');
useEffect(() => {
MCReactModule.getDeviceId().then(val => {
setDeviceId(val || '');
});
}, []);
const handleEnableLogging = async () => {
MCReactModule.enableLogging();
Toast.show('Logging Enabled');
};
const handleDisableLogging = async () => {
await MCReactModule.disableLogging();
Toast.show('Logging Disabled ');
};
const handleSDKState = async () => {
MCReactModule.logSdkState();
Alert.alert('Please check the platform logs for SDK State.');
};
const handleDeviceId = async () => {
let deviceIdentifier = await MCReactModule.getDeviceId();
setDeviceId(deviceIdentifier || '');
Toast.show('Device Id: ' + deviceIdentifier, {
duration: Toast.durations.LONG,
});
};
const handleTrack = async () => {
let event = new CustomEvent('Purchase', {Total: 1234});
MCReactModule.track(event);
Toast.show('Purchase event Tracked');
};
const handleMessages = async () => {
navigation.navigate('MessageScreen');
};
return (
DeviceId: {deviceId}
Get Device Id
Enable Logging
Disable Logging
Log SDK State
Track Screen Viewed - Home
Get Messages
);
};
interface SectionProps {
title: string;
children: React.ReactNode;
}
const Section: React.FC = ({title, children}) => {
return (
{title}
{children}
);
};
const Header = () => {
return (
);
};
const styles = StyleSheet.create({
scrollView: {
flex: 1,
width: '100%',
},
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
color: '#000000',
},
section: {
width: '100%',
alignItems: 'center',
marginBottom: 40,
marginTop: 2,
color: '#000',
},
sectionTitle: {
fontSize: 18,
fontWeight: 'bold',
alignItems: 'center',
color: '#000',
},
sectionContent: {
width: '80%',
},
input: {
borderWidth: 1,
borderColor: '#3F51B5',
borderRadius: 4,
padding: 10,
backgroundColor: '#FFF',
marginBottom: 10,
color: '#000',
},
button: {
backgroundColor: '#1A89CE',
paddingHorizontal: 20,
paddingVertical: 10,
borderRadius: 4,
marginBottom: 20,
},
buttonText: {
color: '#FFF',
fontSize: 14,
textAlign: 'center',
fontWeight: 'bold',
},
textContainer: {
alignItems: 'center',
},
label: {
fontSize: 14,
fontWeight: 'bold',
alignSelf: 'flex-start',
marginTop: 16,
marginBottom: 20,
color: '#000000',
},
text: {
fontSize: 16,
},
headerContainer: {
alignItems: 'center',
marginBottom: 20,
},
headerImage: {
height: 100,
width: undefined,
aspectRatio: 1,
resizeMode: 'contain',
},
headerTitle: {
fontSize: 24,
fontWeight: 'bold',
marginTop: 10,
},
toggleContainer: {
flex: 1,
justifyContent: 'center',
paddingHorizontal: 20,
marginTop: 20,
},
toggleRow: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingVertical: 10,
},
toggleLabel: {
fontSize: 16,
fontWeight: '600',
color: '#000',
},
});
export default HomeScreen;