Skip to content

Instantly share code, notes, and snippets.

@axemclion
Last active September 5, 2020 14:31
Show Gist options
  • Save axemclion/3777f43d257b7c286253a78dc66cabf5 to your computer and use it in GitHub Desktop.
Save axemclion/3777f43d257b7c286253a78dc66cabf5 to your computer and use it in GitHub Desktop.
CodePush + Push Notifications for A/B Testing
import Push from 'mobile-center-push';
import CodePush from 'react-native-code-push';
import { AppState } from 'react-native';
export default class MyApp extends Component {
// Component Logic ...
}
Push.setEventListener({
pushNotificationReceived: function (pushNotification) {
let {message, title} = pushNotification; // Use these to display a message to the user if required
let deploymentKey;
if (pushNotification.customProperties && Object.keys(pushNotification.customProperties).length > 0) {
deploymentKey = pushNotification.customProperties.codePushDeploymentKey;
// Store the deployment key in Async Storage. Use this in the codepush.sync call if you use CodePush when the app starts up.
AsyncStorage.setItem('deploymentKey', deploymentKey);
// You may also want to removeItem from asyncStorage to clear the A/B test and revert to original version of the app.
}
if (AppState.currentState === 'active') {
CodePush.sync({deploymentKey});
}
else {
// Sometimes the push callback is received shortly before the app is fully active in the foreground.
// Since we store the deployment key in AsyncStorage, codepush sync will be called when the app starts again, and the A/B test will run
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment