-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from Crequency/dev=cranyozen
[Pull Request] Simple notification service
- Loading branch information
Showing
18 changed files
with
346 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:awesome_notifications/awesome_notifications.dart'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:get/get.dart'; | ||
import 'package:kitx_mobile/services/public/service_status.dart'; | ||
import 'package:kitx_mobile/utils/extensions/service_status_to_string.dart'; | ||
import 'package:kitx_mobile/instances.dart'; | ||
// import 'package:flutter/material.dart'; | ||
|
||
/// [NotificationService] class | ||
class NotificationService { | ||
/// Status notification id | ||
var statusNotificationId = 10; | ||
|
||
/// Status channel key | ||
var statusChannelKey = "status_channel"; | ||
|
||
/// Initialize the notification service | ||
Future<void> initAsync() async { | ||
// TODO: Adapt to iOS | ||
AwesomeNotifications().initialize( | ||
'resource://drawable/app_icon', | ||
[ | ||
// KitX status channel | ||
NotificationChannel( | ||
channelKey: statusChannelKey, | ||
channelName: 'KitX Status Notifications', | ||
channelDescription: 'KitX status', | ||
locked: true, // Prevents the user from deleting the channel | ||
playSound: false, // Do NOT play sound when the notification is displayed | ||
enableVibration: false, // Do NOT vibrate when the notification is displayed | ||
onlyAlertOnce: true, // Only alert once | ||
// defaultColor: Color(0xFF9D50DD), | ||
// ledColor: Colors.white, | ||
), | ||
], | ||
); | ||
AwesomeNotifications().setListeners(onActionReceivedMethod: onActionReceivedMethod); | ||
} | ||
|
||
/// On action received method | ||
@pragma('vm:entry-point') | ||
static Future<void> onActionReceivedMethod(ReceivedAction receivedAction) async { | ||
var key = receivedAction.buttonKeyPressed; | ||
if (key == 'action_view_button') { | ||
if (instances.devicesService.serviceStatus == ServiceStatus.running) { | ||
// Stop service | ||
instances.shutdownDevicesServer(); | ||
} else { | ||
// Start service | ||
instances.restartDevicesServer(); | ||
} | ||
} else if (key == 'action_view_exit') { | ||
// Exit app | ||
instances.shutdownDevicesServer(); | ||
|
||
// Delete all notifications | ||
AwesomeNotifications().cancelAll(); | ||
|
||
await SystemNavigator.pop(); // Probably not working on iOS (by Copilot) | ||
exit(0); | ||
} | ||
} | ||
|
||
/// Update status notification | ||
Future<void> updateStatusNotification({ | ||
required int deviceCount, | ||
required ServiceStatus serviceStatus, | ||
}) async { | ||
AwesomeNotifications().createNotification( | ||
content: NotificationContent( | ||
id: statusNotificationId, | ||
channelKey: statusChannelKey, | ||
title: 'NotificationService_StatusNotificationTitle'.trParams({"status": serviceStatus.toText()}), | ||
body: 'NotificationService_StatusNotificationBody'.trParams({'device_count': deviceCount.toString()}), | ||
locked: true, | ||
autoDismissible: false, | ||
category: NotificationCategory.Status, | ||
), | ||
actionButtons: [ | ||
NotificationActionButton( | ||
key: 'action_view_button', | ||
label: (serviceStatus == ServiceStatus.running) ? 'Public_Stop'.tr : 'Public_Launch'.tr, | ||
actionType: ActionType.KeepOnTop, | ||
autoDismissible: false, | ||
), | ||
NotificationActionButton( | ||
key: 'action_view_exit', | ||
label: 'Public_Quit'.tr, | ||
actionType: ActionType.SilentAction, | ||
autoDismissible: false, | ||
), | ||
]); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
kitx_mobile/lib/utils/extensions/service_status_to_string.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import 'package:get/get.dart'; | ||
import 'package:kitx_mobile/services/public/service_status.dart'; | ||
|
||
/// [ServiceStatus] Extensions | ||
extension ServiceStatusExtensions on ServiceStatus { | ||
/// Convert [ServiceStatus] to [String] | ||
String toText() { | ||
switch (this) { | ||
case ServiceStatus.error: | ||
return 'Public_Error'.tr; | ||
case ServiceStatus.starting: | ||
return 'Public_Launching'.tr; | ||
case ServiceStatus.stopping: | ||
return 'Public_Stopping'.tr; | ||
case ServiceStatus.running: | ||
return 'Public_Running'.tr; | ||
case ServiceStatus.pending: | ||
return 'Public_Pending'.tr; | ||
default: | ||
return 'Unknown'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.