问题描述
我正在尝试将推送通知发送给单个用户,在服务器端,我存储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],});
}
};
return(
// my View
);
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)