-
-
Save vital-tech-results/3b14e221225fec8cf53f2f83ea1f49c6 to your computer and use it in GitHub Desktop.
Simple local notification with Expo
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, {Component} from 'react'; | |
import {TextInput, View, Keyboard} from 'react-native'; | |
import {Constants, Notifications, Permissions} from 'expo'; | |
export default class Timer extends Component { | |
onSubmit(e) { | |
Keyboard.dismiss(); | |
const localNotification = { | |
title: 'done', | |
body: 'done!' | |
}; | |
const schedulingOptions = { | |
time: (new Date()).getTime() + Number(e.nativeEvent.text) | |
} | |
// Notifications show only when app is not active. | |
// (ie. another app being used or device's screen is locked) | |
Notifications.scheduleLocalNotificationAsync( | |
localNotification, schedulingOptions | |
); | |
} | |
handleNotification() { | |
console.warn('ok! got your notif'); | |
} | |
async componentDidMount() { | |
// We need to ask for Notification permissions for ios devices | |
let result = await Permissions.askAsync(Permissions.NOTIFICATIONS); | |
if (Constants.isDevice && result.status === 'granted') { | |
console.log('Notification permissions granted.') | |
} | |
// If we want to do something with the notification when the app | |
// is active, we need to listen to notification events and | |
// handle them in a callback | |
Notifications.addListener(this.handleNotification); | |
} | |
render() { | |
return ( | |
<View style={{flex: 1, flexDirection: 'row', justifyContent: 'center'}}> | |
<TextInput | |
onSubmitEditing={this.onSubmit} | |
placeholder={'time in ms'} | |
/> | |
</View> | |
); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment