Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Feat/implement a local notification service #235

Merged
merged 9 commits into from
Oct 16, 2023
6 changes: 6 additions & 0 deletions example/vue2/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) TIKI Inc.
~ MIT license. See LICENSE file in root directory.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application
Expand Down Expand Up @@ -38,4 +43,5 @@
<!-- Permissions -->

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
</manifest>
11 changes: 10 additions & 1 deletion example/vue2/package-lock.json

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

3 changes: 2 additions & 1 deletion example/vue2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"@capacitor/android": "^5.4.2",
"@capacitor/core": "latest",
"@capacitor/ios": "^5.4.2",
"@capacitor/local-notifications": "^5.0.6",
"@capacitor/preferences": "^5.0.6",
"@mytiki/capture-receipt-capacitor": "^0.7.1",
"@mytiki/capture-receipt-capacitor": "^0.7.0",
"@mytiki/receipt-capacitor-vue2": "file:../../",
"@mytiki/tiki-sdk-capacitor": "^0.3.4",
"uuid": "^9.0.0",
Expand Down
7 changes: 6 additions & 1 deletion example/vue3/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) TIKI Inc.
~ MIT license. See LICENSE file in root directory.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-feature
Expand Down Expand Up @@ -41,5 +46,5 @@
<!-- Permissions -->

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
</manifest>
1 change: 1 addition & 0 deletions example/vue3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@mytiki/tiki-sdk-capacitor": "^0.3.4",
"@mytiki/receipt-capacitor": "file:../../",
"@capacitor/preferences": "^5.0.6",
"@capacitor/local-notifications": "^5.0.6",
MiroBenicio marked this conversation as resolved.
Show resolved Hide resolved
"uuid": "^9.0.0",
"vue": "^3.3.4"
},
Expand Down
1 change: 1 addition & 0 deletions src/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
export * from "./capture";
export * from "./store";
export * from "./publish";
export * from "./notify";
export * from "./tiki-service";
61 changes: 61 additions & 0 deletions src/service/notify/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) TIKI Inc.
* MIT license. See LICENSE file in root directory.
*/

import { LocalNotifications } from "@capacitor/local-notifications";

export class Notify {
/**
* Checks permission from the user to show notifications
* and requests them if not already granted.
*
* Permission required to use the sendNotification method.
*/
isAuthorized = async (): Promise<boolean> => {
const status = await LocalNotifications.checkPermissions();
switch (status.display) {
case "denied":
return false;
case "prompt":
case "prompt-with-rationale": {
const rsp = await LocalNotifications.requestPermissions();
return rsp.display === "granted";
}
case "granted":
return true;
}
};

/**
* Send a local notification to the user
* @param title - the title of the notification
* @param body - the body of the notification
* @param schedule - when to send the notification, defaults to `new Date()`
* @param id - the notification id, use to update existing notifications. Defaults to `Math.random()`.
*/
async sendNotification(
title: string,
body: string,
schedule: Date = new Date(),
id: number = Math.random(),
): Promise<void> {
const hasPermission = await this.isAuthorized();
if (hasPermission) {
await LocalNotifications.schedule({
notifications: [
{
title: title,
body: body,
id: id,
schedule: { at: schedule },
sound: undefined,
attachments: undefined,
actionTypeId: "",
extra: null,
},
],
});
}
}
}
12 changes: 7 additions & 5 deletions vue2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
],
"dependencies": {
"@capacitor/preferences": "^5.0.6",
"@capacitor/local-notifications": "^5.0.6",
"@mytiki/capture-receipt-capacitor": "^0.7.1",
"@mytiki/tiki-sdk-capacitor": "^0.3.4",
"vue": "2.7.14",
Expand Down Expand Up @@ -71,12 +72,13 @@
"unplugin-vue-markdown": "^0.24.3"
},
"peerDependencies": {
"@capacitor/android": "^5.4.1",
"@capacitor/core": "^5.4.1",
"@capacitor/ios": "^5.4.1",
"@capacitor/preferences": "^5.0.6",
"@capacitor/android": "^5.4.0",
"@capacitor/core": "^5.4.0",
"@capacitor/ios": "^5.4.0",
"@capacitor/preferences": "^5.0.0",
"@capacitor/local-notifications": "^5.0.0",
"@mytiki/capture-receipt-capacitor": "^0.7.1",
"@mytiki/tiki-sdk-capacitor": "^0.3.4",
"vue": "^3.3.0"
"vue": "^3.0.0"
}
}
9 changes: 5 additions & 4 deletions vue3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
],
"dependencies": {
"@capacitor/preferences": "^5.0.6",
"@capacitor/local-notifications": "^5.0.6",
"@mytiki/capture-receipt-capacitor": "^0.7.1",
"@mytiki/tiki-sdk-capacitor": "^0.3.4",
"vue": "^3.3.4",
Expand Down Expand Up @@ -71,10 +72,10 @@
"unplugin-vue-markdown": "^0.24.3"
},
"peerDependencies": {
"@capacitor/android": "^5.4.1",
"@capacitor/core": "^5.4.1",
"@capacitor/ios": "^5.4.1",
"@capacitor/preferences": "^5.0.6",
"@capacitor/android": "^5.4.0",
"@capacitor/core": "^5.4.0",
"@capacitor/ios": "^5.4.0",
"@capacitor/preferences": "^5.0.0",
"@mytiki/capture-receipt-capacitor": "^0.7.1",
"@mytiki/tiki-sdk-capacitor": "^0.3.4",
"vue": "^3.0.0"
Expand Down