100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Added EventBus in order to get push notification actions
Michael Rademakerb4787663082a
diff --git a/console/generic_app/lib/ConsoleProviders/GeofenceProvider.dart b/console/generic_app/lib/ConsoleProviders/GeofenceProvider.dartindex feeb184db..5ea971555 100644--- a/console/generic_app/lib/ConsoleProviders/GeofenceProvider.dart+++ b/console/generic_app/lib/ConsoleProviders/GeofenceProvider.dart@@ -93,8 +93,6 @@ class GeofenceProvider {static Future<GeofenceProvider> getInstance() async {if (_instance == null) {- // ignore: invalid_use_of_visible_for_testing_member- SharedPreferences.setMockInitialValues({});_instance = GeofenceProvider(await SharedPreferences.getInstance());}return _instance;diff --git a/console/generic_app/lib/events/OpenWebPageEvent.dart b/console/generic_app/lib/events/OpenWebPageEvent.dartnew file mode 100644index 000000000..9776eb51a--- /dev/null+++ b/console/generic_app/lib/events/OpenWebPageEvent.dart@@ -0,0 +1,6 @@+class OpenWebPageEvent{++ String url;++ OpenWebPageEvent(this.url);+}diff --git a/console/generic_app/lib/main.dart b/console/generic_app/lib/main.dartindex fae82409c..f21ce8b85 100644--- a/console/generic_app/lib/main.dart+++ b/console/generic_app/lib/main.dart@@ -11,8 +11,6 @@ void main() {WidgetsFlutterBinding.ensureInitialized();SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]).then((_) async {- //ignore: invalid_use_of_visible_for_testing_member- SharedPreferences.setMockInitialValues({});PushNotificationService pushNotificationService = new PushNotificationService();await pushNotificationService.initialise();runApp(new ORApp());diff --git a/console/generic_app/lib/services/EventBusService.dart b/console/generic_app/lib/services/EventBusService.dartnew file mode 100644index 000000000..ddb3bb773--- /dev/null+++ b/console/generic_app/lib/services/EventBusService.dart@@ -0,0 +1,18 @@+import 'package:event_bus/event_bus.dart';++class EventBusService {+ static EventBusService _instance;++ EventBus eventBus;++ EventBusService._internal() {+ eventBus = EventBus();+ }++ static EventBusService getInstance() {+ if (_instance == null) {+ _instance = EventBusService._internal();+ }+ return _instance;+ }+}diff --git a/console/generic_app/lib/services/PushNotificationService.dart b/console/generic_app/lib/services/PushNotificationService.dartindex 150175898..7b2e4ed01 100644--- a/console/generic_app/lib/services/PushNotificationService.dart+++ b/console/generic_app/lib/services/PushNotificationService.dart@@ -1,72 +1,30 @@+import 'dart:convert';import 'dart:io';+import 'package:awesome_dialog/awesome_dialog.dart';import 'package:firebase_messaging/firebase_messaging.dart';+import 'package:flutter/widgets.dart';import 'package:generic_app/ConsoleProviders/GeofenceProvider.dart';import 'package:generic_app/ConsoleProviders/StorageProvider.dart';+import 'package:generic_app/events/OpenWebPageEvent.dart';import 'package:generic_app/models/AlertAction.dart';import 'package:generic_app/models/AlertButton.dart';+import 'EventBusService.dart';+Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> remoteMessage) async {- await handleNotification(remoteMessage, false, false);+ await PushNotificationService.handleNotification(remoteMessage, false, false);}-Future handleNotification(Map<String, dynamic> remoteMessage, bool onResume, bool onLaunch) async {- Map<String, dynamic> messageData = remoteMessage["data"] ??- remoteMessage;- if (messageData != null) {- bool isSilent = !messageData.containsKey("or-title");-- if (isSilent) {- String action = messageData["action"];-- switch (action) {- case "GEOFENCE_REFRESH":- GeofenceProvider geofenceProvider = await GeofenceProvider- .getInstance();- geofenceProvider.refreshGeofences();- break;- case "STORE":- StorageProvider storageProvider = await StorageProvider- .getInstance();- storageProvider.store(messageData["key"], messageData["value"]);- break;- }- } else {- String title = messageData["or-title"];- String body = messageData["or-body"];-- List<AlertButton> buttons;- AlertAction action;-- // Check for action (to be executed when notification is clicked)- if (messageData.containsKey("action")) {- Map<String, dynamic> actionJson = messageData["action"];-- if (actionJson != null && actionJson.isNotEmpty) {- action = AlertAction.fromJson(actionJson);-- if(onResume || onLaunch) {- if(action.openInBrowser) {-- }- }- }- }+class PushNotificationService {+ static FirebaseMessaging _fcm;- // Check for buttons- if (messageData.containsKey("buttons")) {- List<Map<String, dynamic>> buttonsJson = messageData["buttons"];+ static BuildContext _context;- if (buttonsJson != null && buttonsJson.isNotEmpty) {- buttons = buttonsJson.map((e) => AlertButton.fromJson(e));- }- }- }+ static init({@required BuildContext context}) {+ _fcm = FirebaseMessaging();+ _context = context;}-}--class PushNotificationService {- final FirebaseMessaging _fcm = FirebaseMessaging();Future initialise() async {_fcm.configure(@@ -85,6 +43,87 @@ class PushNotificationService {onBackgroundMessage: Platform.isIOS ? null : myBackgroundMessageHandler,);}++ static Future handleNotification(Map<String, dynamic> remoteMessage, bool onResume, bool onLaunch) async {+ Map<dynamic, dynamic> messageData = remoteMessage["data"] ??+ remoteMessage;+ if (messageData != null) {+ bool isSilent = !messageData.containsKey("or-title");++ if (isSilent) {+ String action = messageData["action"];++ switch (action) {+ case "GEOFENCE_REFRESH":+ GeofenceProvider geofenceProvider = await GeofenceProvider+ .getInstance();+ geofenceProvider.refreshGeofences();+ break;+ case "STORE":+ StorageProvider storageProvider = await StorageProvider+ .getInstance();+ storageProvider.store(messageData["key"], messageData["value"]);+ break;+ }+ } else {+ String title = messageData["or-title"];+ String body = messageData["or-body"];++ List<AlertButton> buttons;+ AlertAction action;++ // Check for action (to be executed when notification is clicked)+ if (messageData.containsKey("action")) {+ Map<dynamic, dynamic> actionJson = messageData["action"];++ if (actionJson != null && actionJson.isNotEmpty) {+ action = AlertAction.fromJson(actionJson);++ if(onResume || onLaunch) {+ if(action.openInBrowser) {++ }+ }+ }+ }++ // Check for buttons+ if (messageData.containsKey("buttons")) {+ String buttonsJson = messageData["buttons"];++ if (buttonsJson != null && buttonsJson.isNotEmpty) {+ buttons = (jsonDecode(buttonsJson) as List).map((e) => AlertButton.fromJson(e));+ }+ }++ AwesomeDialog(+ context: _context,+ dialogType: DialogType.INFO,+ animType: AnimType.BOTTOMSLIDE,+ title: title,+ desc: body,+ btnOkText: buttons != null && buttons.isNotEmpty ? buttons[0].title : null,+ btnCancelText: buttons != null && buttons.isNotEmpty ? buttons[1].title: null,+ btnOkOnPress: () {+ if (buttons != null && buttons.isNotEmpty) {+ AlertButton button = buttons[0];+ if (button.action.url != null && button.action.url.isNotEmpty) {+ if (button.action.openInBrowser) {++ } else {+ EventBusService+ .getInstance()+ .eventBus+ .fire(new OpenWebPageEvent(button.action.url));+ }+ }+ }+ },+ btnCancelOnPress: () {}+ );+ }+ }+ }}diff --git a/console/generic_app/lib/ui/WebViewPage.dart b/console/generic_app/lib/ui/WebViewPage.dartindex 66413455c..f15bb28bb 100644--- a/console/generic_app/lib/ui/WebViewPage.dart+++ b/console/generic_app/lib/ui/WebViewPage.dart@@ -1,14 +1,14 @@import 'dart:async';import 'dart:convert';-import 'dart:io';-import 'package:firebase_messaging/firebase_messaging.dart';import 'package:flutter/material.dart';import 'package:generic_app/ConsoleProviders/GeofenceProvider.dart';import 'package:generic_app/ConsoleProviders/PushProvider.dart';import 'package:generic_app/ConsoleProviders/StorageProvider.dart';import 'package:generic_app/config/CurrentConsoleAppConfig.dart';+import 'package:generic_app/events/OpenWebPageEvent.dart';import 'package:generic_app/models/LinkConfig.dart';+import 'package:generic_app/services/EventBusService.dart';import 'package:webview_flutter/webview_flutter.dart';class WebViewPage extends StatefulWidget {@@ -28,24 +28,17 @@ class _WebViewPageState extends State<WebViewPage> {StorageProvider _storageProvider;WebView _webView;- final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();-@overridevoid initState() {- super.initState();+ EventBusService+ .getInstance()+ .eventBus+ .on<OpenWebPageEvent>()+ .listen((event) {- _firebaseMessaging.configure(- onMessage: (Map<String, dynamic> message) async {- print("onMessage: $message");- },- onLaunch: (Map<String, dynamic> message) async {- print("onLaunch: $message");- },- onResume: (Map<String, dynamic> message) async {- print("onResume: $message");- },- onBackgroundMessage: Platform.isAndroid ? myBackgroundMessageHandler : null- );+ });++ super.initState();}@override@@ -254,17 +247,3 @@ class _WebViewPageState extends State<WebViewPage> {}}}--Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {- if (message.containsKey('data')) {- // Handle data message- final dynamic data = message['data'];- }-- if (message.containsKey('notification')) {- // Handle notification message- final dynamic notification = message['notification'];- }-- // Or do other work.-}diff --git a/console/generic_app/pubspec.lock b/console/generic_app/pubspec.lockindex 0e26f2b97..b54f723cc 100644--- a/console/generic_app/pubspec.lock+++ b/console/generic_app/pubspec.lock@@ -36,6 +36,13 @@ packages:url: "https://pub.dartlang.org"source: hostedversion: "2.4.2"+ awesome_dialog:+ dependency: "direct main"+ description:+ name: awesome_dialog+ url: "https://pub.dartlang.org"+ source: hosted+ version: "1.2.0"boolean_selector:dependency: transitivedescription:@@ -183,6 +190,13 @@ packages:url: "https://pub.dartlang.org"source: hostedversion: "1.3.6"+ event_bus:+ dependency: "direct main"+ description:+ name: event_bus+ url: "https://pub.dartlang.org"+ source: hosted+ version: "1.1.1"fake_async:dependency: transitivedescription:@@ -239,6 +253,20 @@ packages:url: "https://pub.dartlang.org"source: hostedversion: "0.10.11"+ flare_dart:+ dependency: transitive+ description:+ name: flare_dart+ url: "https://pub.dartlang.org"+ source: hosted+ version: "2.3.4"+ flare_flutter:+ dependency: transitive+ description:+ name: flare_flutter+ url: "https://pub.dartlang.org"+ source: hosted+ version: "2.0.6"flutter:dependency: "direct main"description: flutter@@ -532,6 +560,34 @@ packages:url: "https://pub.dartlang.org"source: hostedversion: "2.1.3"+ sa_anicoto:+ dependency: transitive+ description:+ name: sa_anicoto+ url: "https://pub.dartlang.org"+ source: hosted+ version: "1.0.3"+ sa_multi_tween:+ dependency: transitive+ description:+ name: sa_multi_tween+ url: "https://pub.dartlang.org"+ source: hosted+ version: "1.1.2"+ sa_stateless_animation:+ dependency: transitive+ description:+ name: sa_stateless_animation+ url: "https://pub.dartlang.org"+ source: hosted+ version: "1.0.3"+ sa_v1_migration:+ dependency: transitive+ description:+ name: sa_v1_migration+ url: "https://pub.dartlang.org"+ source: hosted+ version: "1.1.2"shared_preferences:dependency: "direct main"description:@@ -588,6 +644,13 @@ packages:url: "https://pub.dartlang.org"source: hostedversion: "1.0.3"+ simple_animations:+ dependency: transitive+ description:+ name: simple_animations+ url: "https://pub.dartlang.org"+ source: hosted+ version: "2.2.2"sky_engine:dependency: transitivedescription: flutter@@ -635,6 +698,20 @@ packages:url: "https://pub.dartlang.org"source: hostedversion: "1.0.5"+ supercharged:+ dependency: transitive+ description:+ name: supercharged+ url: "https://pub.dartlang.org"+ source: hosted+ version: "1.10.0"+ supercharged_dart:+ dependency: transitive+ description:+ name: supercharged_dart+ url: "https://pub.dartlang.org"+ source: hosted+ version: "1.2.0"term_glyph:dependency: transitivedescription:diff --git a/console/generic_app/pubspec.yaml b/console/generic_app/pubspec.yamlindex 492537281..717bf0d8a 100644--- a/console/generic_app/pubspec.yaml+++ b/console/generic_app/pubspec.yaml@@ -37,6 +37,8 @@ dependencies:firebase_messaging: ^7.0.0quick_actions: 0.4.0+8show_up_animation: ^1.0.2+ awesome_dialog: ^1.2.0+ event_bus: ^1.1.1dev_dependencies:flutter_test: