Created
October 25, 2017 14:26
-
-
Save aniltirola/df68c33f12f4028fe162fca658c3c5c8 to your computer and use it in GitHub Desktop.
debug expo push notifications on ios distribution build
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { AsyncStorage, Platform, StyleSheet, Text, View, ScrollView } from 'react-native'; | |
import { Permissions, Notifications } from 'expo'; | |
export default class App extends React.Component { | |
constructor(props) { | |
super(props); | |
let tempLogArray = []; | |
tempLogArray.push(consolePrefix() + ' app start...'); | |
this.state = { | |
pushToken: 'none', | |
logArray: tempLogArray, | |
fontsWithSerif: this._getFontsWithSerif() | |
}; | |
} | |
componentDidMount() { | |
this._logPush('comp did mount', true); | |
this._logPush(this._printInfoAboutNotification(this.props.exp), true); | |
this._notificationSubscription = this._registerForPushNotifications(); | |
} | |
componentWillUnmount() { | |
console.log(consolePrefix(), 'componentWillUnmount'); | |
this._notificationSubscription && this._notificationSubscription.remove(); | |
} | |
render() { | |
return ( | |
<View style={{ flex: 1, backgroundColor: '#fff', paddingTop: 30 }}> | |
<ScrollView> | |
<Text> | |
{'OS:' + Platform.OS + ', version:' + Platform.Version} | |
</Text> | |
<Text selectable={true} style={{fontFamily: this.state.fontsWithSerif}}> | |
push-token:{"\n"}{this.state.pushToken}{"\n"} | |
</Text> | |
<Text selectable={true} style={{fontFamily: this.state.fontsWithSerif}}> | |
{this._logGet()} | |
</Text> | |
</ScrollView> | |
</View> | |
); | |
} | |
_registerForPushNotifications() { | |
console.log(consolePrefix(), ' registerForPushNotificationsAsync...'); | |
this.registerForPushNotificationsAsync() | |
this._notificationSubscription = Notifications.addListener( | |
this._handleNotification | |
); | |
} | |
async registerForPushNotificationsAsync() { | |
let { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS); | |
if (status !== 'granted') { | |
return; | |
} | |
let token = await Notifications.getExpoPushTokenAsync(); | |
this._logPush(token, true); | |
this.setState({ | |
pushToken: token | |
}); | |
return new Promise(function(resolve, reject) { | |
if (token !== undefined && token !== null) { | |
resolve(token); | |
} | |
else { | |
reject(Error('no push token available')); | |
} | |
}); | |
} | |
_handleNotification = ({ origin, data }) => { | |
this._logPush(`Push notification ${origin} with data: ${JSON.stringify(data)}`, true); | |
}; | |
_logPush(info, printToConsole) { | |
if (printToConsole) { | |
console.log(consolePrefix(), ' ' + info); | |
} | |
var tempArray = this.state.logArray.slice() | |
tempArray.push(consolePrefix() + ' ' + info) | |
this.setState({ logArray: tempArray }) | |
} | |
_logGet(info) { | |
return this.state.logArray.join("\n"); | |
} | |
_printInfoAboutNotification(_propsExp) { | |
try { | |
if (_propsExp === undefined || _propsExp === null) { | |
throw ('the argument to the function is invalid'); | |
} | |
if (!_propsExp.hasOwnProperty('notification')) { | |
return (" this.props.exp has NO Object named 'notification'"); | |
} | |
else { | |
let info = "Notification available:\n"; | |
let notification = _propsExp.notification; | |
if (!notification.hasOwnProperty('data')) { | |
info += "-Dataobject: NONE\n"; | |
} | |
else { | |
info += "-Dataobject: YES\n"; | |
let _type = typeof notification.data; | |
info += '-Dataobject type:' + _type + "\n"; | |
if (_type === 'string') { | |
info += notification.data; | |
} | |
else { | |
info += JSON.stringify(notification.data, null, " "); | |
} | |
} | |
return info; | |
} | |
} | |
catch (err) { | |
return(" this.props.exp has NO Object named 'notification'"); | |
} | |
} | |
_getFontsWithSerif() { | |
if (Platform.OS === 'ios') { | |
return 'AmericanTypewriter'; | |
} | |
else { | |
return 'serif'; | |
} | |
} | |
} | |
function consolePrefix () { | |
let date = new Date(); | |
let hours = stringLeadingZeros(2, date.getHours()); | |
let minutes = stringLeadingZeros(2, date.getMinutes()); | |
let seconds = stringLeadingZeros(2, date.getSeconds()); | |
let millis = stringLeadingZeros(3, date.getMilliseconds()); | |
let prefix = hours + ':' + minutes + ':' + seconds + ':' + millis; | |
return prefix; | |
} | |
function stringLeadingZeros (targetLengthOfString, stringToFormat) { | |
let leadingZeros = ''; | |
for (let i=1; i<=targetLengthOfString; ++i) { | |
leadingZeros += '0'; | |
} | |
return (leadingZeros + stringToFormat).slice(-targetLengthOfString); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment