Expo React Native-特定用户的接收推送通知-(Expo托管工作流)

时间:2020-08-21 15:32:57

标签: react-native push-notification apple-push-notifications expo

我正在尝试将推送通知发送给单个用户,在服务器端,我存储ExponentPushToken和用户的ID。

但是在React Native方面,我如何只听取来自特定用户的通知?

目前,就像在博览会指南中一样,我听某个博览会令牌的所有通知,但我只希望单个用户的通知。

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

0 个答案:

没有答案