Skip to content

Instantly share code, notes, and snippets.

@toluolatubosun
Last active March 11, 2023 19:11
Show Gist options
  • Save toluolatubosun/9ba7ffad35bbe3d77794e3920941d1f3 to your computer and use it in GitHub Desktop.
Save toluolatubosun/9ba7ffad35bbe3d77794e3920941d1f3 to your computer and use it in GitHub Desktop.
React Native Expo, Expo notification implementation
import React from "react";
import { Text } from "react-native";
import * as Device from "expo-device";
import * as Notifications from "expo-notifications";
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
}),
});
const App = () => {
const [expoPushToken, setExpoPushToken] = React.useState("");
const [notification, setNotification] = React.useState(false);
const notificationListener = React.useRef();
const responseListener = React.useRef();
React.useEffect(() => {
registerForPushNotificationsAsync().then((token) => setExpoPushToken(token));
// This listener is fired whenever a notification is received while the app is foregrounded
notificationListener.current = Notifications.addNotificationReceivedListener((notification) => {
setNotification(notification);
});
// This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed)
responseListener.current = Notifications.addNotificationResponseReceivedListener((response) => {
const {
notification: {
request: {
content: {
data: { screen },
},
},
},
} = response;
// When the user taps on the notification, this line checks if they //are suppose to be taken to a particular screen
if (screen) {
props.navigation.navigate(screen);
}
});
return () => {
Notifications.removeNotificationSubscription(notificationListener.current);
Notifications.removeNotificationSubscription(responseListener.current);
};
}, []);
return <Text>Push Notifications Test App</Text>;
};
async function registerForPushNotificationsAsync() {
let token;
if (Device.isDevice) {
const { status: existingStatus } = await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== "granted") {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
console.log("existingStatus", existingStatus);
}
if (finalStatus !== "granted") {
alert("Failed to get push token for push notification!");
console.log("finalStatus", finalStatus);
return;
}
// Project ID can be found in app.json | app.config.js; extra > eas > projectId
// token = (await Notifications.getExpoPushTokenAsync({ projectId: "YOUR_PROJECT_ID" })).data;
token = (await Notifications.getExpoPushTokenAsync()).data;
// The token should be sent to the server so that it can be used to send push notifications to the device
console.log(token);
} else {
alert("Must use physical device for Push Notifications");
}
if (Platform.OS === "android") {
await Notifications.setNotificationChannelAsync("default", {
name: "default",
showBadge: true,
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: "#FE9018",
});
}
return token;
}
export default App;
- Default expo packages :)
- expo-notifications
- expo-device
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment