import React from 'react'; import { Alert, ScrollView } from 'react-native'; import { Notifications } from 'expo'; import * as Permissions from 'expo-permissions'; import HeadingText from '../components/HeadingText'; import ListButton from '../components/ListButton'; import registerForPushNotificationsAsync from '../api/registerForPushNotificationsAsync'; export default class NotificationScreen extends React.Component { static navigationOptions = { title: 'Notifications', }; render() { return ( Local Notifications Push Notifications Custom notification categories Badge Number ); } _obtainUserFacingNotifPermissionsAsync = async () => { let permission = await Permissions.getAsync( Permissions.USER_FACING_NOTIFICATIONS ); if (permission.status !== 'granted') { permission = await Permissions.askAsync( Permissions.USER_FACING_NOTIFICATIONS ); if (permission.status !== 'granted') { Alert.alert(`We don't have permission to present notifications.`); } } return permission; } _obtainRemoteNotifPermissionsAsync = async () => { let permission = await Permissions.getAsync(Permissions.NOTIFICATIONS); if (permission.status !== 'granted') { permission = await Permissions.askAsync(Permissions.NOTIFICATIONS); if (permission.status !== 'granted') { Alert.alert( `We don't have permission to receive remote notifications.` ); } } return permission; } _presentLocalNotificationAsync = async () => { await this._obtainUserFacingNotifPermissionsAsync(); Notifications.presentLocalNotificationAsync({ title: 'Here is a local notification!', body: 'This is the body', data: { hello: 'there', }, ios: { sound: true, }, }); } _scheduleLocalNotificationAsync = async () => { await this._obtainUserFacingNotifPermissionsAsync(); Notifications.scheduleLocalNotificationAsync( { title: 'Here is a scheduled notifiation!', body: 'This is the body', data: { hello: 'there', future: 'self', }, ios: { sound: true, }, }, { time: new Date().getTime() + 10000, } ); } _createCategoryAsync = () => Notifications.createCategoryAsync('message', [ { actionId: 'dismiss', buttonTitle: 'Dismiss notification', isDestructive: true, isAuthenticationRequired: false, }, { actionId: 'respond', buttonTitle: 'Respond', isDestructive: false, isAuthenticationRequired: true, textInput: { submitButtonTitle: 'Send', placeholder: 'Response', }, }, ]) _deleteCategoryAsync = () => Notifications.deleteCategoryAsync('message'); _scheduleLocalNotificationWithCategoryAsync = async () => { await this._obtainUserFacingNotifPermissionsAsync(); await Notifications.scheduleLocalNotificationAsync( { title: 'Expo sent you a message!', body: 'Howdy, fella!', ios: { sound: true, }, categoryId: 'message', }, { time: new Date().getTime() + 10000, } ); } _scheduleLocalNotificationAndCancelAsync = async () => { await this._obtainUserFacingNotifPermissionsAsync(); const notificationId = await Notifications.scheduleLocalNotificationAsync( { title: 'This notification should not appear', body: 'It should have been cancelled. :(', ios: { sound: true, }, }, { time: new Date().getTime() + 10000, } ); await Notifications.cancelScheduledNotificationAsync(notificationId); } _scheduleLegacyNotificationAsync = async () => { await this._obtainUserFacingNotifPermissionsAsync(); return Notifications.scheduleLocalNotificationAsync( { title: 'Repeating notification', body: `I repeat every minute starting from ${new Date().toLocaleTimeString()}`, data: { repeatingEvery: 'minute', scheduledAt: new Date().toLocaleTimeString(), }, ios: { sound: true, }, }, { time: new Date().getTime() + 2000, repeat: 'minute', } ); } _scheduleAndCancelLegacyNotificationAsync = async () => { const notificationId = await this._scheduleLegacyNotificationAsync(); await Notifications.cancelScheduledNotificationAsync(notificationId); } _incrementIconBadgeNumberAsync = async () => { const currentNumber = await Notifications.getBadgeNumberAsync(); await Notifications.setBadgeNumberAsync(currentNumber + 1); const actualNumber = await Notifications.getBadgeNumberAsync(); Alert.alert(`Set the badge number to ${actualNumber}`); } _clearIconBadgeAsync = async () => { await Notifications.setBadgeNumberAsync(0); Alert.alert(`Cleared the badge`); } _sendNotificationAsync = async () => { const permission = await this._obtainRemoteNotifPermissionsAsync(); if (permission.status === 'granted') { registerForPushNotificationsAsync(); } } }