1+ import { Notifications , Permissions } from 'expo'
2+ import { AsyncStorage } from 'react-native'
3+
4+ const NOTIFICATION_KEY = 'FlashCard:notifications'
5+
6+ export function isEmpty ( obj ) {
7+ //Source: https://stackoverflow.com/a/4994244/2848021
8+
9+ // null and undefined are "empty"
10+ if ( obj == null ) return true ;
11+
12+ // Assume if it has a length property with a non-zero value
13+ // that that property is correct.
14+ if ( obj . length > 0 ) return false ;
15+ if ( obj . length === 0 ) return true ;
16+
17+ // If it isn't an object at this point
18+ // it is empty, but it can't be anything *but* empty
19+ // Is it empty? Depends on your application.
20+ if ( typeof obj !== "object" ) return true ;
21+
22+ // Otherwise, does it have any properties of its own?
23+ // Note that this doesn't handle
24+ // toString and valueOf enumeration bugs in IE < 9
25+ for ( var key in obj ) {
26+ if ( hasOwnProperty . call ( obj , key ) ) return false ;
27+ }
28+
29+ return true ;
30+ }
31+
32+ export function clearLocalNotification ( ) {
33+ return AsyncStorage . removeItem ( NOTIFICATION_KEY )
34+ . then ( Notifications . cancelAllScheduledNotificationsAsync )
35+ }
36+
37+ function createNotification ( ) {
38+ return {
39+ title : 'Time to Study!' ,
40+ body : "Don't forget to try some Quiz today!" ,
41+ ios : {
42+ sound : true ,
43+ } ,
44+ android : {
45+ sound : true ,
46+ priority : 'high' ,
47+ sticky : false ,
48+ vibrate : true ,
49+ }
50+ }
51+ }
52+
53+ export function setLocalNotification ( ) {
54+ AsyncStorage . getItem ( NOTIFICATION_KEY )
55+ . then ( JSON . parse )
56+ . then ( ( data ) => {
57+ if ( data === null ) {
58+ Permissions . askAsync ( Permissions . NOTIFICATIONS )
59+ . then ( ( { status } ) => {
60+ if ( status === 'granted' ) {
61+ Notifications . cancelAllScheduledNotificationsAsync ( )
62+
63+ let tomorrow = new Date ( )
64+ tomorrow . setDate ( tomorrow . getDate ( ) + 1 )
65+ tomorrow . setHours ( 20 )
66+ tomorrow . setMinutes ( 0 )
67+
68+ Notifications . scheduleLocalNotificationAsync (
69+ createNotification ( ) ,
70+ {
71+ time : tomorrow ,
72+ repeat : 'day' ,
73+ }
74+ )
75+
76+ AsyncStorage . setItem ( NOTIFICATION_KEY , JSON . stringify ( true ) )
77+ }
78+ } )
79+ }
80+ } )
81+ }
0 commit comments