Skip to content

Commit

Permalink
fix: FCM refresh token update (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
muhsin-k committed Sep 8, 2021
1 parent d07653a commit acd6a18
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 98 deletions.
4 changes: 2 additions & 2 deletions android/app/build.gradle
Expand Up @@ -136,8 +136,8 @@ android {
applicationId "com.chatwoot.app"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 5014
versionName "1.3.11"
versionCode 5015
versionName "1.3.15"
multiDexEnabled true
}
splits {
Expand Down
8 changes: 4 additions & 4 deletions ios/Chatwoot.xcodeproj/project.pbxproj
Expand Up @@ -753,14 +753,14 @@
CODE_SIGN_IDENTITY = "Apple Development";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 137;
CURRENT_PROJECT_VERSION = 140;
DEVELOPMENT_TEAM = 6C953F3RX2;
ENABLE_BITCODE = NO;
EXCLUDED_ARCHS = "";
INFOPLIST_FILE = Chatwoot/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.3;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 1.3.11;
MARKETING_VERSION = 1.3.15;
OTHER_LDFLAGS = (
"$(inherited)",
"-ObjC",
Expand All @@ -786,13 +786,13 @@
CODE_SIGN_ENTITLEMENTS = Chatwoot/Chatwoot.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 137;
CURRENT_PROJECT_VERSION = 140;
DEVELOPMENT_TEAM = 6C953F3RX2;
EXCLUDED_ARCHS = "";
INFOPLIST_FILE = Chatwoot/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.3;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 1.3.11;
MARKETING_VERSION = 1.3.15;
OTHER_LDFLAGS = (
"$(inherited)",
"-ObjC",
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@chatwoot/mobile-app",
"version": "1.3.11",
"version": "1.3.15",
"private": true,
"scripts": {
"clean": "rm -rf $TMPDIR/react-* && watchman watch-del-all && npm cache clean --force",
Expand Down
160 changes: 80 additions & 80 deletions src/actions/notification.js
Expand Up @@ -30,78 +30,79 @@ import {
GET_NOTIFICATION_ERROR,
} from '../constants/actions';

export const getAllNotifications = ({ pageNo = 1 }) => async (dispatch) => {
try {
if (pageNo === 1) {
dispatch({ type: GET_NOTIFICATION });
}

const response = await APIHelper.get(`notifications?page=${pageNo}`);
const {
data: { payload, meta },
} = response.data;
export const getAllNotifications =
({ pageNo = 1 }) =>
async dispatch => {
try {
if (pageNo === 1) {
dispatch({ type: GET_NOTIFICATION });
}

const updatedPayload = payload.sort((a, b) => {
return b.created_at - a.created_at;
});
const { unread_count } = meta;
updateBadgeCount({ count: unread_count });
const response = await APIHelper.get(`notifications?page=${pageNo}`);
const {
data: { payload, meta },
} = response.data;

dispatch({
type: GET_NOTIFICATION_SUCCESS,
payload: {
notifications: updatedPayload,
meta,
},
});
const updatedPayload = payload.sort((a, b) => {
return b.created_at - a.created_at;
});
const { unread_count } = meta;
updateBadgeCount({ count: unread_count });

if (payload.length < 25) {
dispatch({
type: ALL_NOTIFICATIONS_LOADED,
type: GET_NOTIFICATION_SUCCESS,
payload: {
notifications: updatedPayload,
meta,
},
});

if (payload.length < 25) {
dispatch({
type: ALL_NOTIFICATIONS_LOADED,
});
}
} catch (error) {
dispatch({ type: GET_NOTIFICATION_ERROR, payload: error });
}
} catch (error) {
dispatch({ type: GET_NOTIFICATION_ERROR, payload: error });
}
};
};

export const markNotificationAsRead = ({ primaryActorId, primaryActorType }) => async (
dispatch,
getState,
) => {
const {
data: { payload, meta },
} = getState().notification;
try {
const apiUrl = 'notifications/read_all';
await APIHelper.post(apiUrl, {
primary_actor_type: primaryActorType,
primary_actor_id: primaryActorId,
});
export const markNotificationAsRead =
({ primaryActorId, primaryActorType }) =>
async (dispatch, getState) => {
const {
data: { payload, meta },
} = getState().notification;
try {
const apiUrl = 'notifications/read_all';
await APIHelper.post(apiUrl, {
primary_actor_type: primaryActorType,
primary_actor_id: primaryActorId,
});

const updatedNotifications = payload.map((item, index) => {
if (item.primary_actor_id === primaryActorId) {
item.read_at = 'read_at';
item.mass = 'mass';
}
return item;
});
const updatedNotifications = payload.map((item, index) => {
if (item.primary_actor_id === primaryActorId) {
item.read_at = 'read_at';
item.mass = 'mass';
}
return item;
});

const { unread_count } = meta;
const { unread_count } = meta;

const updatedUnReadCount = unread_count ? unread_count - 1 : unread_count;
const updatedUnReadCount = unread_count ? unread_count - 1 : unread_count;

updateBadgeCount({ count: updatedUnReadCount });
updateBadgeCount({ count: updatedUnReadCount });

dispatch({
type: UPDATE_ALL_NOTIFICATIONS,
payload: {
notifications: updatedNotifications,
meta: { unread_count: updatedUnReadCount },
},
});
} catch {}
};
dispatch({
type: UPDATE_ALL_NOTIFICATIONS,
payload: {
notifications: updatedNotifications,
meta: { unread_count: updatedUnReadCount },
},
});
} catch {}
};

export const markAllNotificationAsRead = () => async (dispatch, getState) => {
const {
Expand All @@ -126,18 +127,13 @@ export const markAllNotificationAsRead = () => async (dispatch, getState) => {
} catch (error) {}
};

export const saveDeviceDetails = ({ token }) => async (dispatch) => {
export const saveDeviceDetails = () => async dispatch => {
try {
const checkPermission = await messaging().hasPermission();
if (!checkPermission || checkPermission === -1) {
const permissionEnabled = await messaging().hasPermission();
if (!permissionEnabled || permissionEnabled === -1) {
await messaging().requestPermission();
}

if (Platform.os === 'ios') {
await messaging().registerForRemoteNotifications();
}
// Check refresh token or create new fcm token
const fcmToken = token || (await messaging().getToken());
const fcmToken = await messaging().getToken();
const deviceId = getUniqueId();
const devicePlatform = getSystemName();
const manufacturer = await getManufacturer();
Expand All @@ -161,23 +157,27 @@ export const saveDeviceDetails = ({ token }) => async (dispatch) => {
};
const headers = await getHeaders();
const baseURL = await getBaseUrl();

await axios.post(`${baseURL}${API_URL}notification_subscriptions`, pushData, {
headers: headers,
});

dispatch({ type: SET_PUSH_TOKEN, payload: fcmToken });
} catch (err) {}
};

export const addNotification = ({ notification }) => async (dispatch, getState) => {
const {
data: { payload },
} = getState().notification;
export const addNotification =
({ notification }) =>
async (dispatch, getState) => {
const {
data: { payload },
} = getState().notification;

// Check notification is already exists or not
const [notificationExists] = payload.filter((c) => c.id === notification.id);
// Check notification is already exists or not
const [notificationExists] = payload.filter(c => c.id === notification.id);

if (notificationExists) {
return;
}
dispatch({ type: ADD_NOTIFICATION, payload: notification });
};
if (notificationExists) {
return;
}
dispatch({ type: ADD_NOTIFICATION, payload: notification });
};
3 changes: 1 addition & 2 deletions src/helpers/PushHelper.js
Expand Up @@ -53,8 +53,7 @@ export const clearAllDeliveredNotifications = () => {
};

export const updateBadgeCount = ({ count }) => {
if (Platform.OS === 'android') {
} else {
if (Platform.OS === 'ios' && count) {
PushNotificationIOS.setApplicationIconBadgeNumber(count);
}
};
16 changes: 7 additions & 9 deletions src/screens/ConversationList/ConversationList.js
Expand Up @@ -39,23 +39,21 @@ const ConversationList = ({ eva: { style }, navigation }) => {
const conversations = useSelector(state => state.conversation.data);
const conversationStatus = useSelector(state => state.conversation.conversationStatus);
const inboxSelected = useSelector(state => state.inbox.inboxSelected);
const pushToken = useSelector(state => state.notification.pushToken);

useEffect(() => {
clearAllDeliveredNotifications();
initActionCable();
dispatch(getInstalledVersion());
dispatch(getInboxes());
dispatch(getAgents());
// loadConversations();
dispatch(getAllNotifications({ pageNo: 1 }));
// TODO: Check this logic later
dispatch(saveDeviceDetails({ token: null }));
if (!pushToken) {
dispatch(saveDeviceDetails({ token: pushToken }));
}
dispatch(saveDeviceDetails());
storeUser();
}, [dispatch, initActionCable, loadConversations, storeUser, pushToken]);
}, [dispatch, initActionCable, storeUser]);

useEffect(() => {
loadConversations();
dispatch(getAllNotifications({ pageNo: 1 }));
}, [dispatch, initActionCable, loadConversations]);

const loadConversations = useCallback(() => {
dispatch(
Expand Down

0 comments on commit acd6a18

Please sign in to comment.