Please provide the following:
- SDK Version: last
- Platforms(iOSl):
I’m trying to send push notifications to a single user, on the server side I store the ExponentPushToken and the user’s id.
But on the react native side, how can I listen to notifications only from a certain user?
Currently, as in the expo guide, I listen to all the notifications of a certain expo token, but I would like only those of the individual user.
import ...............
import * as Notifications from 'expo-notifications';
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
export default function CustomerHome(props) {
// notifications
const [notification, setNotification] = useState(false);
const notificationListener = useRef();
const responseListener = useRef();
/**
* NOTIFICATIONS
* */
useEffect(() => {
registerForPushNotificationsAsync(userId).then(
() => {
}
);
// This listener is fired whenever a notification is received while the app is foregrounded
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
setNotification(notification);
console.log(notification);
});
// This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed)
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
console.log(response);
});
return () => {
Notifications.removeNotificationSubscription(notificationListener);
Notifications.removeNotificationSubscription(responseListener);
};
}, []);
const registerForPushNotificationsAsync = async (userId) => {
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') {
console.log('Failed to get push token for push notification!');
return;
}
token = (await Notifications.getExpoPushTokenAsync()).data;
if(token !== undefined && token !== "undefined"){
saveTokenOnServer(token, userId);
}
} else {
console.log("Must use physical device for Push Notifications");
}
if (Platform.OS === 'android') {
Notifications.createChannelAndroidAsync('default', {
name: 'default',
sound: true,
priority: 'max',
vibrate: [0, 250, 250, 250],
});
}
};
return(
// my View
);
}