-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
132 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { useState, useEffect, useRef } from 'react'; | ||
import { Text, View, Button, Platform } from 'react-native'; | ||
import * as Device from 'expo-device'; | ||
import * as Notifications from 'expo-notifications'; | ||
import Constants from 'expo-constants'; | ||
|
||
Notifications.setNotificationHandler({ | ||
handleNotification: async () => ({ | ||
shouldShowAlert: true, | ||
shouldPlaySound: false, | ||
shouldSetBadge: false, | ||
}), | ||
}); | ||
|
||
export default function Notification() { | ||
const [expoPushToken, setExpoPushToken] = useState(''); | ||
const [channels, setChannels] = useState<Notifications.NotificationChannel[]>([]); | ||
const [notification, setNotification] = useState<Notifications.Notification | undefined>( | ||
undefined | ||
); | ||
const notificationListener = useRef<Notifications.Subscription>(); | ||
const responseListener = useRef<Notifications.Subscription>(); | ||
|
||
useEffect(() => { | ||
registerForPushNotificationsAsync().then(token => token && setExpoPushToken(token)); | ||
|
||
if (Platform.OS === 'android') { | ||
Notifications.getNotificationChannelsAsync().then(value => setChannels(value ?? [])); | ||
} | ||
notificationListener.current = Notifications.addNotificationReceivedListener(notification => { | ||
setNotification(notification); | ||
}); | ||
|
||
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => { | ||
console.log(response); | ||
}); | ||
|
||
return () => { | ||
notificationListener.current && | ||
Notifications.removeNotificationSubscription(notificationListener.current); | ||
responseListener.current && | ||
Notifications.removeNotificationSubscription(responseListener.current); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<View | ||
style={{ | ||
flex: 1, | ||
alignItems: 'center', | ||
justifyContent: 'space-around', | ||
}}> | ||
<Text>Your expo push token: {expoPushToken}</Text> | ||
<Text>{`Channels: ${JSON.stringify( | ||
channels.map(c => c.id), | ||
null, | ||
2 | ||
)}`}</Text> | ||
</View> | ||
); | ||
} | ||
|
||
async function registerForPushNotificationsAsync() { | ||
let token; | ||
|
||
if (Platform.OS === 'android') { | ||
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 | ||
// EAS projectId is used here. | ||
try { | ||
const projectId = | ||
Constants?.expoConfig?.extra?.eas?.projectId ?? Constants?.easConfig?.projectId; | ||
if (!projectId) { | ||
throw new Error('Project ID not found'); | ||
} | ||
token = ( | ||
await Notifications.getExpoPushTokenAsync({ | ||
projectId, | ||
}) | ||
).data; | ||
console.log(token); | ||
} catch (e) { | ||
token = `${e}`; | ||
} | ||
} else { | ||
alert('Must use physical device for Push Notifications'); | ||
} | ||
|
||
return token; | ||
} |
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
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