From 491c2b0dc0c1e05e5d6a7b06fc9c9070a8dc1386 Mon Sep 17 00:00:00 2001 From: estevez-dev Date: Sun, 16 Jun 2019 00:08:13 +0300 Subject: [PATCH] WIP #48 Notifications with mobile_app component --- lib/home_assistant.class.dart | 18 ++++--- lib/main.dart | 68 +++++++++++++++++++------ lib/ui_widgets/config_panel_widget.dart | 26 +++++++++- lib/utils.class.dart | 11 ++++ 4 files changed, 99 insertions(+), 24 deletions(-) diff --git a/lib/home_assistant.class.dart b/lib/home_assistant.class.dart index d3cf7b0..eb0bde3 100644 --- a/lib/home_assistant.class.dart +++ b/lib/home_assistant.class.dart @@ -14,6 +14,8 @@ class HomeAssistant { String _userName; HSVColor savedColor; + String fcmToken; + Map _rawLovelaceData; List panels = []; @@ -83,9 +85,8 @@ class HomeAssistant { }); } - Future checkAppRegistration({bool forceRegister: false, bool forceUpdate: false}) { - Completer completer = Completer(); - var registrationData = { + Map _getAppRegistrationData() { + return { "app_version": "$appVersion", "device_name": "$userName's ${Device().model}", "manufacturer": Device().manufacturer, @@ -93,11 +94,17 @@ class HomeAssistant { "os_name": Device().osName, "os_version": Device().osVersion, "app_data": { - "push_notification_key": "d" + "push_token": "$fcmToken", + "push_url": "https://us-central1-ha-client-c73c4.cloudfunctions.net/sendPushNotification" } }; + } + + Future checkAppRegistration({bool forceRegister: false, bool forceUpdate: false}) { + Completer completer = Completer(); if (Connection().webhookId == null || forceRegister) { Logger.d("Mobile app was not registered yet or need to be reseted. Registering..."); + var registrationData = _getAppRegistrationData(); registrationData.addAll({ "app_id": "ha_client", "app_name": "$appName", @@ -110,7 +117,6 @@ class HomeAssistant { ).then((response) { Logger.d("Processing registration responce..."); var responseObject = json.decode(response); - Logger.d(responseObject.toString()); SharedPreferences.getInstance().then((prefs) { prefs.setString("app-webhook-id", responseObject["webhook_id"]); prefs.setString("registered-app-version", "$appVersion"); @@ -125,7 +131,7 @@ class HomeAssistant { Logger.d("Registered app version is old. Registration need to be updated"); var updateData = { "type": "update_registration", - "data": registrationData + "data": _getAppRegistrationData() }; Connection().sendHTTPPost( endPoint: "/api/webhook/${Connection().webhookId}", diff --git a/lib/main.dart b/lib/main.dart index 8329675..1dd468a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -169,6 +169,7 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker StreamSubscription _showEntityPageSubscription; StreamSubscription _showErrorSubscription; StreamSubscription _startAuthSubscription; + StreamSubscription _showDialogSubscription; StreamSubscription _reloadUISubscription; int _previousViewCount; bool _showLoginButton = false; @@ -265,6 +266,18 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker _quickLoad(); }); } + if (_showDialogSubscription == null) { + _showDialogSubscription = eventBus.on().listen((event){ + _showDialog( + title: event.title, + body: event.body, + onPositive: event.onPositive, + onNegative: event.onNegative, + positiveText: event.positiveText, + negativeText: event.negativeText + ); + }); + } if (_serviceCallSubscription == null) { _serviceCallSubscription = eventBus.on().listen((event) { @@ -294,23 +307,11 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker }); } - /*_firebaseMessaging.getToken().then((String token) { - Logger.d("Device name: ${json.encode(Connection().unicDeviceName)}"); - Connection().sendHTTPPost( - endPoint: '/api/notify.ha-client', - data: '{"token": "$token", "device": ${json.encode(Connection().unicDeviceName)}}' - ).then((_) { - Logger.d("Notificatin listener registered."); - completer.complete(); - }).catchError((e) { - Logger.e("Error registering notification listener: ${e.toString()}"); - completer.complete(); - }); - }).catchError((e) { - Logger.e("Error registering notification listener: ${e.toString()}"); + _firebaseMessaging.getToken().then((String token) { + HomeAssistant().fcmToken = token; completer.complete(); - });*/ - completer.complete(); + }); + //completer.complete(); return completer.future; } @@ -343,6 +344,41 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker } } + void _showDialog({String title, String body, var onPositive, var onNegative, String positiveText, String negativeText}) { + // flutter defined function + showDialog( + context: context, + builder: (BuildContext context) { + // return object of type Dialog + return AlertDialog( + title: new Text("$title"), + content: new Text("$body"), + actions: [ + // usually buttons at the bottom of the dialog + new FlatButton( + child: new Text("$positiveText"), + onPressed: () { + Navigator.of(context).pop(); + if (onPositive != null) { + onPositive(); + } + }, + ), + new FlatButton( + child: new Text("$negativeText"), + onPressed: () { + Navigator.of(context).pop(); + if (onNegative != null) { + onNegative(); + } + }, + ), + ], + ); + }, + ); + } + void _callService(String domain, String service, String entityId, Map additionalParams) { _showInfoBottomBar( message: "Calling $domain.$service", diff --git a/lib/ui_widgets/config_panel_widget.dart b/lib/ui_widgets/config_panel_widget.dart index 004f9b1..8acb31a 100644 --- a/lib/ui_widgets/config_panel_widget.dart +++ b/lib/ui_widgets/config_panel_widget.dart @@ -91,11 +91,33 @@ class _ConfigPanelWidgetState extends State { } resetRegistration() { - HomeAssistant().checkAppRegistration(forceRegister: true).then((_) => Navigator.of(context).pop()); + HomeAssistant().checkAppRegistration(forceRegister: true).then((_) { + Navigator.of(context).pop(); + eventBus.fire(ShowDialogEvent( + title: "App registered", + body: "To start using notifications you need to restart your Home Assistant", + positiveText: "Restart now", + negativeText: "Later", + onPositive: () { + Connection().callService(domain: "homeassistant", service: "restart", entityId: null); + }, + )); + }); } updateRegistration() { - HomeAssistant().checkAppRegistration(forceUpdate: true).then((_) => Navigator.of(context).pop()); + HomeAssistant().checkAppRegistration(forceUpdate: true).then((_) { + Navigator.of(context).pop(); + eventBus.fire(ShowDialogEvent( + title: "App registration updated", + body: "To start using notifications you need to restart your Home Assistant", + positiveText: "Restart now", + negativeText: "Later", + onPositive: () { + Connection().callService(domain: "homeassistant", service: "restart", entityId: null); + }, + )); + }); } @override diff --git a/lib/utils.class.dart b/lib/utils.class.dart index ab62ad7..a7e18f6 100644 --- a/lib/utils.class.dart +++ b/lib/utils.class.dart @@ -173,6 +173,17 @@ class ServiceCallEvent { ServiceCallEvent(this.domain, this.service, this.entityId, this.additionalParams); } +class ShowDialogEvent { + final String title; + final String body; + final String positiveText; + final String negativeText; + final onPositive; + final onNegative; + + ShowDialogEvent({this.title, this.body, this.positiveText: "Ok", this.negativeText: "Cancel", this.onPositive, this.onNegative}); +} + class ShowEntityPageEvent { Entity entity;