Skip to content

Instantly share code, notes, and snippets.

@MichaelWashburnJr
Last active December 10, 2019 13:20
Show Gist options
  • Save MichaelWashburnJr/8c40dc2a75f5d77aae4def1218ec57a5 to your computer and use it in GitHub Desktop.
Save MichaelWashburnJr/8c40dc2a75f5d77aae4def1218ec57a5 to your computer and use it in GitHub Desktop.
react-native-push-notification-ios example
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;
'use es6';
import { useEffect } from 'react';
import PushNotificationIOS from "@react-native-community/push-notification-ios";
import useNotificationRegistration from './useNotificationRegistration';
import useNotificationHandler from './useNotificationHandler';
const NotificationProvider = ({ children }) => {
useNotificationRegistration();
useNotificationHandler();
return children
};
export default NotificationProvider;
'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,
);
};
}, []);
}
'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