Created
January 16, 2024 10:24
-
-
Save uncledevhq/daeb45efbd28bb67d53d4e257daad32f to your computer and use it in GitHub Desktop.
Local-Push-Notifications-App.tsx
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
import { StatusBar } from "expo-status-bar"; | |
import { Button, Platform, StyleSheet, Text, View } from "react-native"; | |
// notifications imports | |
import * as Device from "expo-device"; | |
import * as Notifications from "expo-notifications"; | |
import Constants from "expo-constants"; // Optional | |
// Initialize the notification service | |
Notifications.setNotificationHandler({ | |
handleNotification: async () => ({ | |
shouldShowAlert: true, | |
shouldPlaySound: false, | |
shouldSetBadge: false, | |
}), | |
}); | |
// Register for push notifications [TODO: You can move this to a separate file] | |
export async function registerForPushNotificationsAsync() { | |
let token; | |
if (Platform.OS === "android") { | |
// Don't forget to import Platform from react-native | |
await Notifications.setNotificationChannelAsync("default", { | |
name: "default", | |
importance: Notifications.AndroidImportance.MAX, | |
vibrationPattern: [0, 250, 250, 250], | |
lightColor: "#FF231F7C", | |
}); | |
} | |
if (Device.isDevice) { | |
const { status: existingStatus } = | |
await Notifications.getPermissionsAsync(); | |
let finalStatus = existingStatus; | |
if (existingStatus !== "granted") { | |
const { status } = await Notifications.requestPermissionsAsync(); | |
finalStatus = status; | |
} | |
if (finalStatus !== "granted") { | |
alert("Failed to get push token for push notification!"); | |
return; | |
} | |
// Learn more about projectId: | |
// https://docs.expo.dev/push-notifications/push-notifications-setup/#configure-projectid | |
if (Constants.easConfig?.projectId) { | |
token = ( | |
await Notifications.getExpoPushTokenAsync({ | |
projectId: Constants.easConfig.projectId, // you can hard code project id if you dont want to use expo Constants | |
}) | |
).data; | |
console.log(token); | |
} | |
} else { | |
alert("Must use physical device for Push Notifications"); | |
} | |
return token; | |
} | |
// Send the notification [TODO: You can move this to a separate file or export it as a function to be called from anywhere] | |
async function schedulePushNotification() { | |
await Notifications.scheduleNotificationAsync({ | |
content: { | |
title: "You've got notification! 🔔", | |
body: "Here is the notification body", | |
data: { data: "goes here" }, | |
}, | |
trigger: { seconds: 2 }, // You can change this to any time interval you want | |
}); | |
} | |
export default function App() { | |
return ( | |
<View style={styles.container}> | |
<Text>Open up App.tsx to start working on your app!</Text> | |
{/* Test button to schedule notifications */} | |
<Button | |
title="Send Notification" | |
onPress={async () => { | |
await schedulePushNotification(); | |
}} | |
/> | |
<StatusBar style="auto" /> | |
</View> | |
); | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
backgroundColor: "#fff", | |
alignItems: "center", | |
justifyContent: "center", | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment