Skip to content

Commit

Permalink
timer and background notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
as1605 committed Oct 29, 2023
1 parent 5c0b28f commit 24d0370
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 10 deletions.
9 changes: 6 additions & 3 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ if (flutterVersionName == null) {

android {
namespace "com.example.ocs_tracker"
compileSdkVersion flutter.compileSdkVersion
compileSdkVersion 34 //flutter.compileSdkVersion
ndkVersion flutter.ndkVersion

compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Expand All @@ -49,6 +50,7 @@ android {
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
multiDexEnabled true
}

buildTypes {
Expand All @@ -63,5 +65,6 @@ android {
flutter {
source '../..'
}

dependencies {}
dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.2'
}
22 changes: 22 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
android:label="ocs_tracker"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<service
android:name="com.dexterous.flutterlocalnotifications.ForegroundService"
android:exported="false"
android:stopWithTask="false"
/>
<receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ActionBroadcastReceiver" />
<receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
<receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:exported="true"
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:7.3.0'
classpath 'com.android.tools.build:gradle:7.3.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';
import 'package:flutter/material.dart';
import 'package:ocs_tracker/screens/launch.dart';
import 'package:ocs_tracker/utils/api.dart';
import 'package:ocs_tracker/utils/background.dart';

class MyHttpOverrides extends HttpOverrides {
// https://stackoverflow.com/a/61312927
Expand All @@ -19,6 +20,7 @@ void main() async {
HttpOverrides.global = MyHttpOverrides();

final api = ApiProvider();
initializeService();
// await api.credentialProvider.logout();
runApp(MyApp(api: api));
}
Expand Down
7 changes: 5 additions & 2 deletions lib/utils/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class CredentialProvider {
}

final jwt = response.body.toString();
print({jwt});
// print({jwt});
await setJWT(jwt.substring(1, jwt.length - 1));

return true;
Expand Down Expand Up @@ -138,11 +138,14 @@ class ApiProvider {
if (response.statusCode != 200) {
throw Exception("Notification ERROR");
}
print(response.body);

final List<dynamic> newNotifications = jsonDecode(response.body);
final List<dynamic> oldNotifications =
jsonDecode(await read("OCS_COMPANIES") ?? "[]");

print(newNotifications.length);
print(oldNotifications.length);

await save("OCS_COMPANIES", jsonEncode(newNotifications));

return newNotifications
Expand Down
115 changes: 115 additions & 0 deletions lib/utils/background.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import 'dart:async';
import 'dart:io';
import 'dart:ui';

import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:ocs_tracker/main.dart';
import 'package:ocs_tracker/utils/api.dart';

const notificationChannelId = 'my_foreground';

// this will be used for notification id, So you can update your custom notification with this id.
var notificationId = 888;

Future<void> initializeService() async {
final service = FlutterBackgroundService();

const AndroidNotificationChannel channel = AndroidNotificationChannel(
notificationChannelId, // id
'OCS Tracker', // title
description: 'This channel is used for new companies.', // description
importance: Importance.high, // importance must be at low or higher level
);

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

final AndroidFlutterLocalNotificationsPlugin? androidImplementation =
flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>();

await androidImplementation?.createNotificationChannel(channel);
await androidImplementation?.requestNotificationsPermission();

await service.configure(
iosConfiguration: IosConfiguration(),
androidConfiguration: AndroidConfiguration(
// this will be executed when app is in foreground or background in separated isolate
onStart: onStart,

// auto start service
autoStart: true,
isForegroundMode: true,

notificationChannelId: notificationChannelId,
// this must match with notification channel you created above.
initialNotificationTitle: 'OCS Tracker',
initialNotificationContent:
'Initializing... This app will check for new companies every 10 minutes',
foregroundServiceNotificationId: notificationId,
),
);
print("AYYYYy");
}

Future<(String, int)> getText() async {
HttpOverrides.global = MyHttpOverrides();
final api = ApiProvider();

final isUser = await api.credentialProvider.getUser() ?? "";
if (isUser == "") {
return ("Please login with kerberos and OCS password", -1);
}

final isLogged = await api.credentialProvider.checkLogged();
if (!isLogged) {
await api.credentialProvider.login();
final nowLogged = await api.credentialProvider.checkLogged();
if (!nowLogged) {
return ("Invalid credentials", -1);
}
}

final notifs = await api.getNotifications();

return (
"${notifs.length} new companies! ${notifs.map((e) => "${e["company_name"]} (${e["designation"]})").join(" | ")}",
notifs.length
);
}

Future<void> checkNotif(FlutterLocalNotificationsPlugin plugin) async {
final (text, num) = await getText();
if (num > 0) {
notificationId++;
}
plugin.show(
notificationId,
'OCS Tracker',
text,
const NotificationDetails(
android: AndroidNotificationDetails(notificationChannelId, 'OCS Tracker',
icon: 'ic_bg_service_small',
onlyAlertOnce: true,
priority: Priority.max,
importance: Importance.max),
),
);
}

Future<void> onStart(ServiceInstance service) async {
// Only available for flutter 3.0.0 and later
DartPluginRegistrant.ensureInitialized();

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

checkNotif(flutterLocalNotificationsPlugin);

Timer(const Duration(minutes: 1),
() => checkNotif(flutterLocalNotificationsPlugin));

Timer.periodic(const Duration(minutes: 10),
(_) => checkNotif(flutterLocalNotificationsPlugin));
}
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import FlutterMacOS
import Foundation

import flutter_local_notifications
import flutter_secure_storage_macos
import path_provider_foundation
import shared_preferences_foundation
import url_launcher_macos

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FlutterLocalNotificationsPlugin.register(with: registry.registrar(forPlugin: "FlutterLocalNotificationsPlugin"))
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
Expand Down
96 changes: 96 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
args:
dependency: transitive
description:
name: args
sha256: eef6c46b622e0494a36c5a12d10d77fb4e855501a91c1b9ef9339326e58f0596
url: "https://pub.dev"
source: hosted
version: "2.4.2"
async:
dependency: transitive
description:
Expand Down Expand Up @@ -57,6 +65,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.6"
dbus:
dependency: transitive
description:
name: dbus
sha256: "6f07cba3f7b3448d42d015bfd3d53fe12e5b36da2423f23838efc1d5fb31a263"
url: "https://pub.dev"
source: hosted
version: "0.7.8"
fake_async:
dependency: transitive
description:
Expand Down Expand Up @@ -86,6 +102,38 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_background_service:
dependency: "direct main"
description:
name: flutter_background_service
sha256: "7b18ac89f88e521a44e9da8fcaa768a59b7c2cfad9f41bf3fcc6cf673032e33e"
url: "https://pub.dev"
source: hosted
version: "5.0.2"
flutter_background_service_android:
dependency: transitive
description:
name: flutter_background_service_android
sha256: "4998b3d191a04f36f720eff69d3905f384b91e1f92b3dd74aca4ffb5670f38dc"
url: "https://pub.dev"
source: hosted
version: "6.1.0"
flutter_background_service_ios:
dependency: transitive
description:
name: flutter_background_service_ios
sha256: ab73657535876e16abc89e40f924df3e92ad3dee83f64d187081417e824709ed
url: "https://pub.dev"
source: hosted
version: "5.0.0"
flutter_background_service_platform_interface:
dependency: transitive
description:
name: flutter_background_service_platform_interface
sha256: cd5720ff5b051d551a4734fae16683aace779bd0425e8d3f15d84a0cdcc2d8d9
url: "https://pub.dev"
source: hosted
version: "5.0.0"
flutter_lints:
dependency: "direct dev"
description:
Expand All @@ -94,6 +142,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.3"
flutter_local_notifications:
dependency: "direct main"
description:
name: flutter_local_notifications
sha256: "6d11ea777496061e583623aaf31923f93a9409ef8fcaeeefdd6cd78bf4fe5bb3"
url: "https://pub.dev"
source: hosted
version: "16.1.0"
flutter_local_notifications_linux:
dependency: transitive
description:
name: flutter_local_notifications_linux
sha256: "33f741ef47b5f63cc7f78fe75eeeac7e19f171ff3c3df054d84c1e38bedb6a03"
url: "https://pub.dev"
source: hosted
version: "4.0.0+1"
flutter_local_notifications_platform_interface:
dependency: transitive
description:
name: flutter_local_notifications_platform_interface
sha256: "7cf643d6d5022f3baed0be777b0662cce5919c0a7b86e700299f22dc4ae660ef"
url: "https://pub.dev"
source: hosted
version: "7.0.0+1"
flutter_secure_storage:
dependency: "direct main"
description:
Expand Down Expand Up @@ -280,6 +352,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.2.1"
petitparser:
dependency: transitive
description:
name: petitparser
sha256: cb3798bef7fc021ac45b308f4b51208a152792445cce0448c9a4ba5879dd8750
url: "https://pub.dev"
source: hosted
version: "5.4.0"
platform:
dependency: transitive
description:
Expand Down Expand Up @@ -405,6 +485,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.6.0"
timezone:
dependency: transitive
description:
name: timezone
sha256: "1cfd8ddc2d1cfd836bc93e67b9be88c3adaeca6f40a00ca999104c30693cdca0"
url: "https://pub.dev"
source: hosted
version: "0.9.2"
typed_data:
dependency: transitive
description:
Expand Down Expand Up @@ -509,6 +597,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.3"
xml:
dependency: transitive
description:
name: xml
sha256: "5bc72e1e45e941d825fd7468b9b4cc3b9327942649aeb6fc5cdbf135f0a86e84"
url: "https://pub.dev"
source: hosted
version: "6.3.0"
sdks:
dart: ">=3.1.5 <4.0.0"
flutter: ">=3.13.0"
Loading

0 comments on commit 24d0370

Please sign in to comment.