diff --git a/App.js b/App.js index d15c5c4..9d9858a 100644 --- a/App.js +++ b/App.js @@ -9,11 +9,14 @@ import FlashStackNavigator from './components/navigators/FlashStackNavigator'; import reducers from './reducers'; +import { prepareNotification } from './utils/notification'; + export default class App extends Component { render() { const store = createStore(reducers); + prepareNotification(); return ( diff --git a/utils/notification.js b/utils/notification.js new file mode 100644 index 0000000..f218380 --- /dev/null +++ b/utils/notification.js @@ -0,0 +1,40 @@ +import { Notifications, Permissions } from 'expo'; + +import { getScores } from '../services'; + +export async function prepareNotification() { + const scores = await getScores(); + const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS); + + if (scores.length === 0) { + //notificar logo + } else { + + scores.sort((a, b) => { + return b.date - a.date + }) + + const score = scores[0]; + + const _24hago = 24 * 60 * 60 * 1000; + const now = Date.now(); + const threshold = (now - _24hago) > score.date; + + if(threshold){ + Notifications.scheduleLocalNotificationAsync({ + title: 'Quiz time!', + body: "Improve your skills practicing every day!", + android: { + sound: true, + priority: 'max', + vibrate: true, + sticky: false + } + }) + } else { + Notifications.cancelAllScheduledNotificationsAsync(); + } + + } + +} \ No newline at end of file