Skip to content

Instantly share code, notes, and snippets.

@dhei
Last active September 26, 2017 18:16
Show Gist options
  • Select an option

  • Save dhei/fca76754c9c7c6510636b9eb0ddd0c02 to your computer and use it in GitHub Desktop.

Select an option

Save dhei/fca76754c9c7c6510636b9eb0ddd0c02 to your computer and use it in GitHub Desktop.
Sample react native app using mobile center
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React from 'react';
import {
AppRegistry,
Text,
View,
StyleSheet,
TouchableOpacity
} from 'react-native';
import Analytics from 'mobile-center-analytics';
import Crashes from 'mobile-center-crashes';
export default class newapp extends React.Component {
constructor() {
super();
this.state = {
analyticsEnabled: false,
crashesEnabled: false
};
this.toggleAnalyticsEnabled = this.toggleAnalyticsEnabled.bind(this);
this.toggleCrashesEnabled = this.toggleCrashesEnabled.bind(this);
}
async componentDidMount() {
const component = this;
const analyticsEnabled = await Analytics.isEnabled();
component.setState({ analyticsEnabled });
const crashesEnabled = await Crashes.isEnabled();
component.setState({ crashesEnabled });
}
async toggleAnalyticsEnabled() {
await Analytics.setEnabled(!this.state.analyticsEnabled);
const analyticsEnabled = await Analytics.isEnabled();
this.setState({ analyticsEnabled });
}
async toggleCrashesEnabled() {
await Crashes.setEnabled(!this.state.crashesEnabled);
const crashesEnabled = await Crashes.isEnabled();
this.setState({ crashesEnabled });
}
render() {
return (
<View style={SharedStyles.container}>
<Text style={SharedStyles.heading}>
Sampler of Mobile Center
</Text>
<Text style={SharedStyles.enabledText}>
Crashes enabled: {this.state.crashesEnabled ? 'yes' : 'no'}
</Text>
<TouchableOpacity onPress={this.toggleCrashesEnabled}>
<Text style={SharedStyles.toggleEnabled}>
toggle
</Text>
</TouchableOpacity>
<Text style={SharedStyles.enabledText}>
Analytics enabled: {this.state.analyticsEnabled ? 'yes' : 'no'}
</Text>
<TouchableOpacity onPress={this.toggleAnalyticsEnabled}>
<Text style={SharedStyles.toggleEnabled}>
toggle
</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => Analytics.trackEvent('newapp event', { message: 'MC analytics works' })}>
<Text style={SharedStyles.button}>
Track Event
</Text>
</TouchableOpacity>
</View>
);
}
}
const SharedStyles = StyleSheet.create({
heading: {
fontSize: 24,
textAlign: 'center',
marginBottom: 20,
},
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
button: {
color: '#4444FF',
fontSize: 18,
textAlign: 'center',
margin: 10,
},
enabledText: {
fontSize: 14,
textAlign: 'center',
},
toggleEnabled: {
color: '#4444FF',
fontSize: 14,
textAlign: 'center',
marginBottom: 10,
},
});
AppRegistry.registerComponent('newapp', () => newapp);
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React from 'react';
import {
AppRegistry,
Text,
View,
StyleSheet,
TouchableOpacity
} from 'react-native';
import Analytics from 'mobile-center-analytics';
import Crashes from 'mobile-center-crashes';
import Push from 'mobile-center-push';
export default class newapp extends React.Component {
constructor() {
super();
this.state = {
analyticsEnabled: false,
crashesEnabled: false,
pushEnabled: false
};
this.toggleAnalyticsEnabled = this.toggleAnalyticsEnabled.bind(this);
this.toggleCrashesEnabled = this.toggleCrashesEnabled.bind(this);
this.togglePushEnabled = this.togglePushEnabled.bind(this);
}
async componentDidMount() {
const component = this;
const analyticsEnabled = await Analytics.isEnabled();
component.setState({ analyticsEnabled });
const crashesEnabled = await Crashes.isEnabled();
component.setState({ crashesEnabled });
const pushEnabled = await Push.isEnabled();
component.setState({ pushEnabled });
}
async toggleAnalyticsEnabled() {
await Analytics.setEnabled(!this.state.analyticsEnabled);
const analyticsEnabled = await Analytics.isEnabled();
this.setState({ analyticsEnabled });
}
async toggleCrashesEnabled() {
await Crashes.setEnabled(!this.state.crashesEnabled);
const crashesEnabled = await Crashes.isEnabled();
this.setState({ crashesEnabled });
}
async togglePushEnabled() {
await Push.setEnabled(!this.state.pushEnabled);
const pushEnabled = await Push.isEnabled();
this.setState({ pushEnabled });
}
render() {
return (
<View style={SharedStyles.container}>
<Text style={SharedStyles.heading}>
Sampler of Mobile Center
</Text>
<Text style={SharedStyles.enabledText}>
Crashes enabled: {this.state.crashesEnabled ? 'yes' : 'no'}
</Text>
<TouchableOpacity onPress={this.toggleCrashesEnabled}>
<Text style={SharedStyles.toggleEnabled}>
toggle
</Text>
</TouchableOpacity>
<Text style={SharedStyles.enabledText}>
Push enabled: {this.state.pushEnabled ? 'yes' : 'no'}
</Text>
<TouchableOpacity onPress={this.togglePushEnabled}>
<Text style={SharedStyles.toggleEnabled}>
toggle
</Text>
</TouchableOpacity>
<Text style={SharedStyles.enabledText}>
Analytics enabled: {this.state.analyticsEnabled ? 'yes' : 'no'}
</Text>
<TouchableOpacity onPress={this.toggleAnalyticsEnabled}>
<Text style={SharedStyles.toggleEnabled}>
toggle
</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => Analytics.trackEvent('newapp event', { message: 'MC analytics works' })}>
<Text style={SharedStyles.button}>
Track Event
</Text>
</TouchableOpacity>
</View>
);
}
}
const SharedStyles = StyleSheet.create({
heading: {
fontSize: 24,
textAlign: 'center',
marginBottom: 20,
},
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
button: {
color: '#4444FF',
fontSize: 18,
textAlign: 'center',
margin: 10,
},
enabledText: {
fontSize: 14,
textAlign: 'center',
},
toggleEnabled: {
color: '#4444FF',
fontSize: 14,
textAlign: 'center',
marginBottom: 10,
},
});
AppRegistry.registerComponent('newapp', () => newapp);
@dhei

dhei commented Sep 19, 2017

Copy link
Copy Markdown
Author

The purpose of test-react-native-link.sh script is to automate linking mobile center modules to a brand new react native app.

Basic usage:

  1. download the test-react-native-link.sh script
  2. [optional] replace android-appsecret and ios-appsecret with real app secrets if you want to test against a real mobile center app
  3. run bash .\test-react-native-link.sh. Because Xcode 9 requires iOS apps to be signed before you can build, you might need to open Xcode and configure automatic signing before running react-native run-ios.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment