Skip to content

Instantly share code, notes, and snippets.

@mangowi
Forked from cruzach/App.js
Created December 4, 2020 15:14
Show Gist options
  • Save mangowi/08782b00ed6ec53e05af08d9a6087ac8 to your computer and use it in GitHub Desktop.
Save mangowi/08782b00ed6ec53e05af08d9a6087ac8 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment