-
-
Save farhan-ibrahim/468b61a6422c4a3ef67606fcf97a02cf to your computer and use it in GitHub Desktop.
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 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