Skip to content

Commit

Permalink
fix: unique device id
Browse files Browse the repository at this point in the history
  • Loading branch information
tangimds committed Feb 7, 2025
1 parent 81fe25e commit ea3e3d3
Show file tree
Hide file tree
Showing 7 changed files with 1,165 additions and 382 deletions.
10 changes: 5 additions & 5 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"react-native-svg": "15.10.1",
"react-native-swiper": "^1.6.0",
"react-native-tab-view": "^3.5.2",
"react-native-uuid": "^2.0.1",
"react-native-uuid": "^2.0.3",
"react-native-webview": "13.8.6",
"urijs": "^1.19.11",
"uuid": "^8.3.2"
Expand Down
10 changes: 8 additions & 2 deletions app/src/navigation/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ import ChooseIndicatorOrder from '../scenes/indicateurs/CreateIndicator/ChooseIn
import * as Notifications from 'expo-notifications';
import {registerForPushNotificationsAsync} from '../services/notifications-expo';
import * as Device from 'expo-device';
import AsyncStorage from '@react-native-async-storage/async-storage';
import uuid from 'react-native-uuid';

const Stack = createStackNavigator();

Expand Down Expand Up @@ -117,8 +119,12 @@ class Router extends React.Component {
RNBootsplash.hide({fade: true});

try {
// Get device ID
const deviceId = Device.deviceName + '_' + Device.modelName;
// Get or generate device ID
let deviceId = await AsyncStorage.getItem('deviceId');
if (!deviceId) {
deviceId = uuid.v4();
await AsyncStorage.setItem('deviceId', deviceId);
}

// Setup notification handler
const notificationListener = Notifications.addNotificationReceivedListener(notification => {
Expand Down
13 changes: 4 additions & 9 deletions app/src/scenes/reminder/reminder.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,10 @@ const Reminder = ({navigation, route, notifReminderTitle = "Comment ça va aujou
}, []);

const scheduleNotification = async (newReminder = new Date(Date.now() + 10 * 1000)) => {
NotificationService.cancelAll();
const fireDate = dayjs().set('hours', newReminder.getHours()).set('minutes', newReminder.getMinutes()).set('seconds', 0).toDate();
/*NotificationService.scheduleNotification({
date: fireDate,
title: notifReminderTitle,
message: notifReminderMessage,
repeatType: "day",
});*/

if (!(await NotificationService.hasToken())) return;

await API.put({
path: '/reminder',
body: {
Expand Down Expand Up @@ -125,7 +120,6 @@ const Reminder = ({navigation, route, notifReminderTitle = "Comment ça va aujou
};

const deleteReminder = async () => {
NotificationService.cancelAll();
setReminder('');
setReminderSetupVisible(false);
await AsyncStorage.removeItem(ReminderStorageKey);
Expand All @@ -134,6 +128,7 @@ const Reminder = ({navigation, route, notifReminderTitle = "Comment ça va aujou
logEvents.logReminderCancel();
deleteReminder();
if (!(await NotificationService.hasToken())) return;

await API.put({
path: '/reminder',
body: {
Expand Down Expand Up @@ -175,7 +170,7 @@ const Reminder = ({navigation, route, notifReminderTitle = "Comment ça va aujou
</TouchableOpacity>
</>
) : (
<Text style={styles.subtitle}>Un rappel permet de remplir plus souvent lapplication et obtenir des analyses plus pertinentes</Text>
<Text style={styles.subtitle}>Un rappel permet de remplir plus souvent l'application et obtenir des analyses plus pertinentes</Text>
)}
</View>
{!!route?.params?.onboarding && !!reminder && (
Expand Down
14 changes: 5 additions & 9 deletions app/src/scenes/reminder/reminderOnboarding.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,10 @@ const Reminder = ({navigation, route, notifReminderTitle = "Comment ça va aujou
}, []);

const scheduleNotification = async (newReminder = new Date(Date.now() + 10 * 1000)) => {
NotificationService.cancelAll();
const fireDate = dayjs().set('hours', newReminder.getHours()).set('minutes', newReminder.getMinutes()).set('seconds', 0).toDate();
/*NotificationService.scheduleNotification({
date: fireDate,
title: notifReminderTitle,
message: notifReminderMessage,
repeatType: "day",
});*/

if (!(await NotificationService.hasToken())) return;

await API.put({
path: '/reminder',
body: {
Expand Down Expand Up @@ -128,7 +123,6 @@ const Reminder = ({navigation, route, notifReminderTitle = "Comment ça va aujou
};

const deleteReminder = async () => {
NotificationService.cancelAll();
setReminder('');
setReminderSetupVisible(false);
await AsyncStorage.removeItem(ReminderStorageKey);
Expand All @@ -139,8 +133,10 @@ const Reminder = ({navigation, route, notifReminderTitle = "Comment ça va aujou
};

const desactivateReminder = async () => {
logEvents.logReminderCancel();
await deleteReminder();
if (!(await NotificationService.hasToken())) return;

await API.put({
path: '/reminder',
body: {
Expand Down Expand Up @@ -177,7 +173,7 @@ const Reminder = ({navigation, route, notifReminderTitle = "Comment ça va aujou
</TouchableOpacity>
</>
) : (
<Text style={styles.subtitle}>Un rappel permet de remplir plus souvent lapplication et obtenir des analyses plus pertinentes</Text>
<Text style={styles.subtitle}>Un rappel permet de remplir plus souvent l'application et obtenir des analyses plus pertinentes</Text>
)}
</View>
</ScrollView>
Expand Down
60 changes: 31 additions & 29 deletions app/src/services/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import AsyncStorage from '@react-native-async-storage/async-storage';
import {STORAGE_KEY_PUSH_NOTIFICATION_TOKEN, STORAGE_KEY_PUSH_NOTIFICATION_TOKEN_ERROR} from '../utils/constants';
import logEvents from './logEvents';
import API from './api';
import {registerForPushNotificationsAsync} from './notifications-expo';

class NotificationService {
listeners = {};
Expand Down Expand Up @@ -85,31 +86,19 @@ class NotificationService {
}
}

checkPermission = async () => {
const {status: existingStatus} = await Notifications.getPermissionsAsync();
let permission = {granted: false, canAsk: false};

switch (existingStatus) {
case 'undetermined':
permission = {granted: false, canAsk: true};
break;
case 'denied':
permission = {granted: false, canAsk: false};
break;
case 'granted':
permission = {granted: true};
break;
}
return permission;
};
async checkPermission() {
const {status} = await Notifications.getPermissionsAsync();
return status === 'granted';
}

checkAndAskForPermission = async () => {
async checkAndAskForPermission() {
const {status: existingStatus} = await Notifications.getPermissionsAsync();
if (existingStatus === 'granted') return true;

const {status} = await Notifications.requestPermissionsAsync();
return status === 'granted';
};
if (existingStatus !== 'granted') {
const {status} = await Notifications.requestPermissionsAsync();
return status === 'granted';
}
return true;
}

checkAndGetPermissionIfAlreadyGiven = async from => {
const {status: existingStatus} = await Notifications.getPermissionsAsync();
Expand Down Expand Up @@ -210,13 +199,26 @@ class NotificationService {
delete this.listeners[listenerKey];
};

getToken = async () => {
return await AsyncStorage.getItem(STORAGE_KEY_PUSH_NOTIFICATION_TOKEN, null);
};
async getToken() {
try {
let deviceId = await AsyncStorage.getItem('deviceId');
if (!deviceId) {
deviceId = uuid.v4();
await AsyncStorage.setItem('deviceId', deviceId);
}

hasToken = async () => {
return (await AsyncStorage.getItem(STORAGE_KEY_PUSH_NOTIFICATION_TOKEN, null)) !== null;
};
const token = await registerForPushNotificationsAsync({userId: deviceId});
return token;
} catch (error) {
console.error('Error getting push token:', error);
return null;
}
}

async hasToken() {
const token = await this.getToken();
return !!token;
}

getTokenError = async () => {
return await AsyncStorage.getItem(STORAGE_KEY_PUSH_NOTIFICATION_TOKEN_ERROR, null);
Expand Down
Loading

0 comments on commit ea3e3d3

Please sign in to comment.