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';
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
}),
});
Notifications.setNotificationCategoryAsync('myCategory',
[
{
identifier: 'replay',
buttonTitle: 'Replay',
textInput: { submitButtonTitle: 'Enviar', placeholder: 'Replay...' },
},
{
identifier: 'mark_as_read',
buttonTitle: 'Mark as read'
}
]);
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));
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
setNotification(notification);
});
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
if (response.notification.request.content.categoryIdentifier === 'myCategory') {
if (response.actionIdentifier === 'replay') {
console.log('Replay: ', response.actionText)
}
if (response.actionIdentifier === 'mark_as_read') {
console.log('read')
}
}
Notifications.dismissNotificationAsync(response.notification.request.identifier);
});
return () => {
Notifications.removeNotificationSubscription(notificationListener.current);
Notifications.removeNotificationSubscription(responseListener.current);
};
}, []);
return (
<View
style={{
flex: 1,
alignItems: 'center',
justifyContent: 'space-around',
}}>
<Text>Your expo push token: {expoPushToken}</Text>
<View style={{ alignItems: 'center', justifyContent: 'center' }}>
<Text>Title: {notification && notification.request.content.title} </Text>
<Text>Body: {notification && notification.request.content.body}</Text>
</View>
<Button
title="Press to schedule a notification"
onPress={async () => {
await schedulePushNotification();
}}
/>
</View>
);
}
async function schedulePushNotification() {
await Notifications.scheduleNotificationAsync({
content: {
title: "Notification",
body: 'Learning push notification with expo...',
categoryIdentifier: 'myCategory',
},
trigger: null,
identifier: 'txt_input'
});
}
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;
}
token = (await Notifications.getExpoPushTokenAsync()).data;
console.log(token);
} else {
alert('Must use physical device for Push Notifications');
}
return token;
}
Faz um:
console.log(expoPushToken) e console.log(notification)
Pra mim ver os dados que eles trazem.
LOG expoPushToken: ExponentPushToken[B7ZhkiJkAIDPAilZks-71k]
LOG notification: {"date": 1687702359706, "request": {"content": {"autoDismiss": true, "badge": null, "body": "Learning push notification with expo...", "categoryIdentifier": "myCategory", "data": null, "sound": "default", "sticky": false, "subtitle": null, "title": "Notification"}, "identifier": "txt_input", "trigger": null}}
LOG expoPushToken: ExponentPushToken[B7ZhkiJkAIDPAilZks-71k]
LOG notification: {"date": 1687702359706, "request": {"content": {"autoDismiss": true, "badge": null, "body": "Learning push notification with expo...", "categoryIdentifier": "myCategory", "data": null, "sound": "default", "sticky": false, "subtitle": null, "title": "Notification"}, "identifier": "txt_input", "trigger": null}}
LOG expoPushToken: ExponentPushToken[B7ZhkiJkAIDPAilZks-71k]
LOG notification: {"date": 1687702359706, "request": {"content": {"autoDismiss": true, "badge": null, "body": "Learning push notification with expo...", "categoryIdentifier": "myCategory", "data": null, "sound": "default", "sticky": false, "subtitle": null, "title": "Notification"}, "identifier": "txt_input", "trigger": null}}
LOG expoPushToken: ExponentPushToken[B7ZhkiJkAIDPAilZks-71k]
Acima está o console para console.log(expoPushToken) e console.log(notification)
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.