Expo React Native - Recive push notifications for a specific user - (Expo Managed Workflow)

Please provide the following:

  1. SDK Version:last
  2. Platforms(iOS):

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
    );
    
    }
1 Like

I also came across that problem. Have you solved it somehow?
Essentially, I would like to throw the subscription away once the user fired log out event.

@mattbolo If I understand your question correctly, on the front end, you only listen to notifications, period. You don’t need to try to filter notifications on the front end, since it will only receive notifications that are sent to the unique ExponentPushToken that was generated for this instance/installation of the app on this device. So, as long as you are filtering on the backend and making sure you only send the push notification to this ExponentPushToken that represents this single user you want to target, then the user’s device should be the only one to get the notification.

Also, the notification handler is only needed if you want to handle the notification when the app is currently open and in the device’s foreground. It will be handled automatically by the OS, otherwise.

Hope this information was helpful.

Hi @esdrasevt, you have understud perfectly. I have resolved this problem months ago. I have adopted this solution. When a user do a login in the app, send automatically his token on the server. And then the server send a push notification only for that token

thank you

1 Like