Skip to content

Instantly share code, notes, and snippets.

@cruzach
Created July 9, 2020 22:20
Show Gist options
  • Save cruzach/eadbf47990e57b9ff4c808dd377d44c7 to your computer and use it in GitHub Desktop.
Save cruzach/eadbf47990e57b9ff4c808dd377d44c7 to your computer and use it in GitHub Desktop.
import Constants from "expo-constants";
import * as Notifications from "expo-notifications";
import * as Permissions from "expo-permissions";
import React, { useState, useEffect, useRef } from "react";
import { Text, View, Button, Platform } from "react-native";
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
// This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed)
Notifications.addNotificationResponseReceivedListener((response) => {
alert(JSON.stringify(response));
});
export default function App() {
const [expoPushToken, setExpoPushToken] = useState("");
const [notification, setNotification] = useState(false);
const notificationListener = useRef();
const responseListener = useRef();
useEffect(() => {
registerForPushNotificationsAsync().then((token) =>
setExpoPushToken(token)
);
return () => {
Notifications.removeAllNotificationListeners();
};
}, []);
return (
<View
style={{
flex: 1,
alignItems: "center",
justifyContent: "space-around",
}}
>
<Text>Your expo push token: {expoPushToken}</Text>
<View style={{ alignItems: "center", justifyContent: "center" }}>
<Text>Press the button below and kill your app</Text>
</View>
<Button
title="Press to Send Notification"
onPress={async () => {
await sendPushNotification(expoPushToken);
}}
/>
</View>
);
}
// Can use this function below, OR use Expo's Push Notification Tool-> https://expo.io/dashboard/notifications
async function sendPushNotification(expoPushToken) {
await Notifications.scheduleNotificationAsync({
content: {
title: "1 You've got mail! 📬",
subtitle: "this is the sub",
body: "What would you like to do with it?",
vibrate: false,
},
trigger: { seconds: 5 },
});
}
async function registerForPushNotificationsAsync() {
let token;
if (Constants.isDevice) {
const { status: existingStatus } = await Permissions.getAsync(
Permissions.NOTIFICATIONS
);
let finalStatus = existingStatus;
if (existingStatus !== "granted") {
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}
if (finalStatus !== "granted") {
alert("Failed to get push token for push notification!");
return;
}
token = (
await Notifications.getExpoPushTokenAsync({
experienceId: "@charliecruzan/myapp",
})
).data;
console.log(token);
} else {
alert("Must use physical device for Push Notifications");
}
if (Platform.OS === "android") {
Notifications.setNotificationChannelAsync("default", {
name: "default",
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: "#FF231F7C",
});
}
return token;
}
@farhan-ibrahim
Copy link

I have a question, if I call the listener outside the component, how can I trigger a method (ex: to navigate to desired screen )inside the component?

@mesameen
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment