-
-
Save VeraZab/c3f13d51588bcfdf6799da65decf26fa to your computer and use it in GitHub Desktop.
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> | |
); | |
} | |
}; |
tried both @tomathosauce and @whiteclouds7 successfully with sdk40. Thank you for an awesome gist.
There aren't any changes in SDK version 43 compared to @whiteclouds7 proposed solution but here follows a refined example that is also working in TypeScript:
import { registerRootComponent } from 'expo';
import { PermissionStatus } from 'expo-modules-core';
import * as Notifications from 'expo-notifications';
import { Notification } from 'expo-notifications';
import React, { useEffect, useState } from 'react';
import { Button, StyleSheet, View } from 'react-native';
const App = () => {
const [notificationPermissions, setNotificationPermissions] = useState<PermissionStatus>(
PermissionStatus.UNDETERMINED,
);
const scheduleNotification = (seconds: number) => {
const schedulingOptions = {
content: {
title: 'This is a notification',
body: 'This is the body',
sound: true,
priority: Notifications.AndroidNotificationPriority.HIGH,
color: 'blue',
},
trigger: {
seconds: seconds,
},
};
Notifications.scheduleNotificationAsync(schedulingOptions);
};
const handleNotification = (notification: Notification) => {
const { title } = notification.request.content;
console.warn(title);
};
const requestNotificationPermissions = async () => {
const { status } = await Notifications.requestPermissionsAsync();
setNotificationPermissions(status);
return status;
};
useEffect(() => {
requestNotificationPermissions();
}, []);
useEffect(() => {
if (notificationPermissions !== PermissionStatus.GRANTED) return;
const listener = Notifications.addNotificationReceivedListener(handleNotification);
return () => listener.remove();
}, [notificationPermissions]);
return (
<View style={styles.container}>
<Button onPress={() => scheduleNotification(1)} title="Notify" />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
paddingLeft: 10,
paddingRight: 10,
},
});
registerRootComponent(App);
export default App;
The expo-constants isDevice recently became deprecated. Make sure to now use the Device library from Expo.
expo install expo-device
import * as Device from 'expo-device';
And replace Constants.isDevice with Device.isDevice
Hi, i'm trying to use @whiteclouds7 example but for some reason the notifications don't show when the app is in the background on a physical device, they do work properly on Xcode iphone Simulator, any clues on why this may be?
I have this exact same issue, the notifications register just fine on a non APK/bundle but the moment I bundle the app.. the scheduleNotificationsAsync
function just don't set the notification.
Hi,
I'm using SKD41 and Expo is currently moving the permissions to the specific dependency packages and probably won't support the "expo-permissions" in future releases. This means that you should use the
getPermissionsAsync()
andrequestPermissionsAsync()
from the expo-notifications package.I've included a one line change inside the askNotification function into the example provided by @tomathosauce
I hope this helps someone out 😊