Last active
December 10, 2019 13:20
-
-
Save MichaelWashburnJr/8c40dc2a75f5d77aae4def1218ec57a5 to your computer and use it in GitHub Desktop.
react-native-push-notification-ios example
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
iump | |
import { View } from 'react-native'; | |
import NotificationProvider from './NotificationProvider'; | |
const App = () => { | |
return ( | |
<View> | |
<NotificationProvider /> | |
{/* Include NotificationProvider with the rest of your app */} | |
</View> | |
); | |
} | |
export default App; |
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
'use es6'; | |
import { useEffect } from 'react'; | |
import PushNotificationIOS from "@react-native-community/push-notification-ios"; | |
import { Toast } from 'native-base'; | |
function handleNotification(notification) { | |
Toast.show({ | |
text: notification.getMessage(), | |
duration: 3000, | |
position: 'top', | |
}); | |
} | |
export default function useNotifications() { | |
useEffect(() => { | |
PushNotificationIOS.addEventListener( | |
'notification', | |
handleNotification, | |
); | |
PushNotificationIOS.addEventListener( | |
'localNotification', | |
handleNotification, | |
); | |
return () => { | |
PushNotificationIOS.removeEventListener( | |
'notification', | |
handleNotification, | |
); | |
PushNotificationIOS.removeEventListener( | |
'localNotification', | |
handleNotification, | |
); | |
}; | |
}, []); | |
} |
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
'use es6'; | |
import { useState, useEffect, useCallback } from 'react'; | |
import PushNotificationIOS from "@react-native-community/push-notification-ios"; | |
import { captureException } from 'sentry-expo'; | |
export default function useNotificationRegistration() { | |
const [token, setToken] = useState(); | |
const onRegister = useCallback((pushNotificationsToken) => { | |
setToken(pushNotificationsToken); | |
}, [setToken]); | |
const onRegisterError = useCallback((error) => { | |
captureException(error); | |
}, []); | |
useEffect(() => { | |
PushNotificationIOS.addEventListener('register', onRegister); | |
return () => PushNotificationIOS.removeEventListener('register', onRegister); | |
}, [onRegister]); | |
useEffect(() => { | |
PushNotificationIOS.addEventListener('registrationError', onRegisterError); | |
return () => PushNotificationIOS.removeEventListener('registrationError', onRegisterError); | |
}, [onRegisterError]); | |
useEffect(() => { | |
PushNotificationIOS.requestPermissions(); | |
}, []); | |
PushNotificationIOS.checkPermissions(perms => console.log(perms)); | |
return token; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment