Skip to content

Commit

Permalink
[#64][#144] Create notification for sync form submission
Browse files Browse the repository at this point in the history
  • Loading branch information
wayangalihpratama committed Aug 24, 2023
1 parent 98e19f4 commit e0781f1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
24 changes: 22 additions & 2 deletions app/src/lib/__test__/notification.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('notificationHandler', () => {
});

describe('sendPushNotification', () => {
it('should schedule a push notification', async () => {
it('should send a sync form version push notification if type not defined', async () => {
const { scheduleNotificationAsync } = Notifications;
scheduleNotificationAsync.mockResolvedValue();

Expand All @@ -81,7 +81,27 @@ describe('notificationHandler', () => {
content: {
title: 'New Form version available',
body: 'Here is the notification body',
data: null,
data: {
notificationType: 'sync-form-version'
},
},
trigger: null,
});
});

it('should send a push notification by type defined', async () => {
const { scheduleNotificationAsync } = Notifications;
scheduleNotificationAsync.mockResolvedValue();

await notification.sendPushNotification('sync-form-submission');

expect(scheduleNotificationAsync).toHaveBeenCalledWith({
content: {
title: 'Sync submission completed',
body: 'Here is the notification body',
data: {
notificationType: 'sync-form-submission'
},
},
trigger: null,
});
Expand Down
2 changes: 2 additions & 0 deletions app/src/lib/background-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ const syncFormSubmission = async (photos = []) => {
});
return Promise.all(syncProcess).then(async (res) => {
return res;
}).then(() => {
console.info('[syncFormSubmision] Finish: ', new Date());
});
} catch (err) {
console.error('[syncFormSubmission] Error: ', err);
Expand Down
37 changes: 28 additions & 9 deletions app/src/lib/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,34 @@ const registerForPushNotificationsAsync = async () => {
return token;
};

const sendPushNotification = async () => {
await Notifications.scheduleNotificationAsync({
content: {
title: 'New Form version available',
body: 'Here is the notification body',
data: null,
},
trigger: null,
});
const sendPushNotification = async (type = 'sync-form-version') => {
const data = {
notificationType: type
}
let notificationBody = null
switch (type) {
case 'sync-form-submission':
notificationBody = {
content: {
title: 'Sync submission completed',
body: 'Here is the notification body',
data: data,
},
trigger: null,
}
break;
default:
notificationBody = {
content: {
title: 'New Form version available',
body: 'Here is the notification body',
data: data,
},
trigger: null,
}
break;
}
return await Notifications.scheduleNotificationAsync(notificationBody);
};

const notificationHandler = () => {
Expand Down

0 comments on commit e0781f1

Please sign in to comment.