add notification
This commit is contained in:
@@ -2,13 +2,16 @@ import 'package:background_fetch/background_fetch.dart';
|
||||
import 'package:filcnaplo/api/providers/user_provider.dart';
|
||||
import 'package:filcnaplo/api/providers/database_provider.dart';
|
||||
import 'package:filcnaplo/database/init.dart';
|
||||
import 'package:filcnaplo/helpers/notification_helper.dart';
|
||||
import 'package:filcnaplo/models/settings.dart';
|
||||
import 'package:filcnaplo_kreta_api/client/client.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:filcnaplo/app.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:filcnaplo_mobile_ui/screens/error_screen.dart';
|
||||
import 'package:filcnaplo_mobile_ui/screens/error_report_screen.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
|
||||
void main() async {
|
||||
// Initalize
|
||||
@@ -43,6 +46,53 @@ class Startup {
|
||||
await database.init();
|
||||
settings = await database.query.getSettings(database);
|
||||
user = await database.query.getUsers(settings);
|
||||
|
||||
// Notifications setup
|
||||
initPlatformState();
|
||||
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
|
||||
FlutterLocalNotificationsPlugin();
|
||||
|
||||
// Get permission to show notifications
|
||||
flutterLocalNotificationsPlugin
|
||||
.resolvePlatformSpecificImplementation<
|
||||
AndroidFlutterLocalNotificationsPlugin>()!
|
||||
.requestPermission();
|
||||
await flutterLocalNotificationsPlugin
|
||||
.resolvePlatformSpecificImplementation<
|
||||
IOSFlutterLocalNotificationsPlugin>()
|
||||
?.requestPermissions(
|
||||
alert: false,
|
||||
badge: true,
|
||||
sound: true,
|
||||
);
|
||||
await flutterLocalNotificationsPlugin
|
||||
.resolvePlatformSpecificImplementation<
|
||||
MacOSFlutterLocalNotificationsPlugin>()
|
||||
?.requestPermissions(
|
||||
alert: false,
|
||||
badge: true,
|
||||
sound: true,
|
||||
);
|
||||
|
||||
// Platform specific settings
|
||||
final DarwinInitializationSettings initializationSettingsDarwin =
|
||||
DarwinInitializationSettings(
|
||||
requestSoundPermission: true,
|
||||
requestBadgePermission: true,
|
||||
requestAlertPermission: false,
|
||||
);
|
||||
const AndroidInitializationSettings initializationSettingsAndroid =
|
||||
AndroidInitializationSettings('ic_notification');
|
||||
final InitializationSettings initializationSettings = InitializationSettings(
|
||||
android: initializationSettingsAndroid,
|
||||
iOS: initializationSettingsDarwin,
|
||||
macOS: initializationSettingsDarwin
|
||||
);
|
||||
|
||||
// Initialize notifications
|
||||
await flutterLocalNotificationsPlugin.initialize(
|
||||
initializationSettings,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,9 +121,38 @@ Widget errorBuilder(FlutterErrorDetails details) {
|
||||
return Container();
|
||||
});
|
||||
}
|
||||
// Platform messages are asynchronous, so we initialize in an async method.
|
||||
Future<void> initPlatformState() async {
|
||||
// Configure BackgroundFetch.
|
||||
int status = await BackgroundFetch.configure(BackgroundFetchConfig(
|
||||
minimumFetchInterval: 15,
|
||||
stopOnTerminate: false,
|
||||
enableHeadless: true,
|
||||
requiresBatteryNotLow: false,
|
||||
requiresCharging: false,
|
||||
requiresStorageNotLow: false,
|
||||
requiresDeviceIdle: false,
|
||||
requiredNetworkType: NetworkType.ANY
|
||||
), (String taskId) async { // <-- Event handler
|
||||
// This is the fetch-event callback.
|
||||
print("[BackgroundFetch] Event received $taskId");
|
||||
NotificationsHelper().backgroundJob();
|
||||
|
||||
// IMPORTANT: You must signal completion of your task or the OS can punish your app
|
||||
// for taking too long in the background.
|
||||
BackgroundFetch.finish(taskId);
|
||||
}, (String taskId) async { // <-- Task timeout handler.
|
||||
// This task has exceeded its allowed running-time. You must stop what you're doing and immediately .finish(taskId)
|
||||
print("[BackgroundFetch] TASK TIMEOUT taskId: $taskId");
|
||||
BackgroundFetch.finish(taskId);
|
||||
});
|
||||
print('[BackgroundFetch] configure success: $status');
|
||||
|
||||
}
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void backgroundHeadlessTask(HeadlessTask task) {
|
||||
print('[BackgroundFetch] Headless event received.');
|
||||
NotificationsHelper().backgroundJob();
|
||||
BackgroundFetch.finish(task.taskId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user