Skip to content

Commit

Permalink
feat: FCM HTTP v1 changes (#787)
Browse files Browse the repository at this point in the history
  • Loading branch information
muhsin-k committed Jun 18, 2024
1 parent b555830 commit 42e8181
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 29 deletions.
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ android {
applicationId "com.chatwoot.app"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 5180031
versionName "1.10.40"
versionCode 5180032
versionName "1.10.41"
}
lintOptions {
checkReleaseBuilds false
Expand Down
8 changes: 4 additions & 4 deletions ios/Chatwoot.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Chatwoot/Chatwoot.entitlements;
CURRENT_PROJECT_VERSION = 340;
CURRENT_PROJECT_VERSION = 341;
DEVELOPMENT_TEAM = L7YLMN4634;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Chatwoot/Info.plist;
Expand All @@ -570,7 +570,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.10.40;
MARKETING_VERSION = 1.10.41;
OTHER_LDFLAGS = (
"$(inherited)",
"-ObjC",
Expand All @@ -595,15 +595,15 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Chatwoot/Chatwoot.entitlements;
CURRENT_PROJECT_VERSION = 340;
CURRENT_PROJECT_VERSION = 341;
DEVELOPMENT_TEAM = L7YLMN4634;
INFOPLIST_FILE = Chatwoot/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = Chatwoot;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.10.40;
MARKETING_VERSION = 1.10.41;
OTHER_LDFLAGS = (
"$(inherited)",
"-ObjC",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chatwoot/mobile-app",
"version": "1.10.40",
"version": "1.10.41",
"private": true,
"scripts": {
"android": "react-native run-android",
Expand Down
20 changes: 16 additions & 4 deletions src/helpers/PushHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ export const updateBadgeCount = ({ count = 0 }) => {
};

export const findConversationLinkFromPush = ({ notification, installationUrl }) => {
const pushData = JSON.parse(notification);

const { notification_type } = pushData;
const { notification_type } = notification;

if (NOTIFICATION_TYPES.includes(notification_type)) {
const { primary_actor, primary_actor_id, primary_actor_type } = pushData;
const { primary_actor, primary_actor_id, primary_actor_type } = notification;
let conversationId = null;
if (primary_actor_type === 'Conversation') {
conversationId = primary_actor.id;
Expand All @@ -35,3 +33,17 @@ export const findConversationLinkFromPush = ({ notification, installationUrl })
}
return;
};

export const findNotificationFromFCM = ({ message }) => {
let notification = null;
// FCM HTTP v1
if (message?.data?.payload) {
const parsedPayload = JSON.parse(message.data.payload);
notification = parsedPayload.data.notification;
}
// FCM legacy. It will be deprecated soon
else {
notification = JSON.parse(message.data.notification);
}
return notification;
};
82 changes: 67 additions & 15 deletions src/helpers/specs/pushHelper.spec.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
import { findConversationLinkFromPush } from '../PushHelper';
import { findConversationLinkFromPush, findNotificationFromFCM } from '../PushHelper';

describe('findConversationLinkFromPush', () => {
it('should return conversation link if notification_type is conversation_creation', () => {
const notification =
'{"id":8687,"notification_type":"conversation_creation","primary_actor_id":14902,"primary_actor_type":"Conversation","primary_actor":{"id":14428}}';
const notification = {
id: 8687,
notification_type: 'conversation_creation',
primary_actor_id: 14902,
primary_actor_type: 'Conversation',
primary_actor: { id: 14428 },
};
const installationUrl = 'https://app.chatwoot.com';
const result = findConversationLinkFromPush({ notification, installationUrl });
expect(result).toBe(
'https://app.chatwoot.com/app/accounts/1/conversations/14428/14902/Conversation',
);
});

it('should return conversation link if notification_type is conversation_assignment', () => {
const notification =
'{"id":8696,"notification_type":"conversation_assignment","primary_actor_id":3104,"primary_actor_type":"Conversation","primary_actor":{"id":2684}}';
const notification = {
id: 8696,
notification_type: 'conversation_assignment',
primary_actor_id: 3104,
primary_actor_type: 'Conversation',
primary_actor: { id: 2684 },
};
const installationUrl = 'https://app.chatwoot.com';
const result = findConversationLinkFromPush({ notification, installationUrl });
expect(result).toBe(
Expand All @@ -21,8 +32,13 @@ describe('findConversationLinkFromPush', () => {
});

it('should return conversation link if notification_type is assigned_conversation_new_message', () => {
const notification =
'{"id":8694,"notification_type":"assigned_conversation_new_message","primary_actor_id":58731,"primary_actor_type":"Message","primary_actor":{"conversation_id":14429,"id":58731}}';
const notification = {
id: 8694,
notification_type: 'assigned_conversation_new_message',
primary_actor_id: 58731,
primary_actor_type: 'Message',
primary_actor: { conversation_id: 14429, id: 58731 },
};
const installationUrl = 'https://app.chatwoot.com';
const result = findConversationLinkFromPush({ notification, installationUrl });
expect(result).toBe(
Expand All @@ -31,8 +47,13 @@ describe('findConversationLinkFromPush', () => {
});

it('should return conversation link if notification_type is conversation_mention', () => {
const notification =
'{"id":8690,"notification_type":"conversation_mention","primary_actor_id":58725,"primary_actor_type":"Message","primary_actor":{"conversation_id":14428,"id":58725}}';
const notification = {
id: 8690,
notification_type: 'conversation_mention',
primary_actor_id: 58725,
primary_actor_type: 'Message',
primary_actor: { conversation_id: 14428, id: 58725 },
};
const installationUrl = 'https://app.chatwoot.com';
const result = findConversationLinkFromPush({ notification, installationUrl });
expect(result).toBe(
Expand All @@ -41,21 +62,52 @@ describe('findConversationLinkFromPush', () => {
});

it('should return conversation link if notification_type is participating_conversation_new_message', () => {
const notification =
'{"id":8678,"notification_type":"participating_conversation_new_message","primary_actor_id":58712,"primary_actor_type":"Message","primary_actor":{"conversation_id":14427,"id":58712}}';

const notification = {
id: 8678,
notification_type: 'participating_conversation_new_message',
primary_actor_id: 58712,
primary_actor_type: 'Message',
primary_actor: { conversation_id: 14427, id: 58712 },
};
const installationUrl = 'https://app.chatwoot.com';
const result = findConversationLinkFromPush({ notification, installationUrl });
expect(result).toBe(
'https://app.chatwoot.com/app/accounts/1/conversations/14427/58712/Message',
);
});
it('should return nothing if notification_type is not valid', () => {
const notification =
'{"id":8678,"notification_type":"participating_conversation_message","primary_actor_id":58712,"primary_actor_type":"Message","primary_actor":{"conversation_id":14427,"id":58712}}';

it('should return nothing if notification_type is not valid', () => {
const notification = {
id: 8678,
notification_type: 'participating_conversation_message',
primary_actor_id: 58712,
primary_actor_type: 'Message',
primary_actor: { conversation_id: 14427, id: 58712 },
};
const installationUrl = 'https://app.chatwoot.com';
const result = findConversationLinkFromPush({ notification, installationUrl });
expect(result).toBe(undefined);
});
});

describe('findNotificationFromFCM', () => {
it('should return notification from FCM HTTP v1 message', () => {
const message = {
data: {
payload: '{"data": {"notification": {"id": 123, "title": "Test Notification"}}}',
},
};
const result = findNotificationFromFCM({ message });
expect(result).toEqual({ id: 123, title: 'Test Notification' });
});

it('should return notification from FCM legacy message', () => {
const message = {
data: {
notification: '{"id": 456, "title": "Legacy Notification"}',
},
};
const result = findNotificationFromFCM({ message });
expect(result).toEqual({ id: 456, title: 'Legacy Notification' });
});
});
6 changes: 3 additions & 3 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import ConversationAction from './screens/ConversationAction/ConversationAction'
import TabStack from './components/TabBar';
import i18n from 'i18n';
import { navigationRef } from 'helpers/NavigationHelper';
import { findConversationLinkFromPush } from './helpers/PushHelper';
import { findConversationLinkFromPush, findNotificationFromFCM } from './helpers/PushHelper';
import { extractConversationIdFromUrl } from './helpers/conversationHelpers';
import { selectLoggedIn } from 'reducer/authSlice';
import { selectInstallationUrl, selectLocale } from 'reducer/settingsSlice';
Expand Down Expand Up @@ -109,7 +109,7 @@ const App = () => {
// Handle notification caused app to open from quit state:
const message = await messaging().getInitialNotification();
if (message) {
const { notification } = message.data;
const notification = findNotificationFromFCM({ message });
const conversationLink = findConversationLinkFromPush({ notification, installationUrl });
if (conversationLink) {
return conversationLink;
Expand All @@ -126,7 +126,7 @@ const App = () => {
// Handle notification caused app to open from background state
const unsubscribeNotification = messaging().onNotificationOpenedApp(message => {
if (message) {
const { notification } = message.data;
const notification = findNotificationFromFCM({ message });

const conversationLink = findConversationLinkFromPush({ notification, installationUrl });
if (conversationLink) {
Expand Down

0 comments on commit 42e8181

Please sign in to comment.