WIP #48 Notifications with mobile_app component
This commit is contained in:
parent
5b99ade088
commit
491c2b0dc0
@ -14,6 +14,8 @@ class HomeAssistant {
|
|||||||
String _userName;
|
String _userName;
|
||||||
HSVColor savedColor;
|
HSVColor savedColor;
|
||||||
|
|
||||||
|
String fcmToken;
|
||||||
|
|
||||||
Map _rawLovelaceData;
|
Map _rawLovelaceData;
|
||||||
|
|
||||||
List<Panel> panels = [];
|
List<Panel> panels = [];
|
||||||
@ -83,9 +85,8 @@ class HomeAssistant {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Future checkAppRegistration({bool forceRegister: false, bool forceUpdate: false}) {
|
Map _getAppRegistrationData() {
|
||||||
Completer completer = Completer();
|
return {
|
||||||
var registrationData = {
|
|
||||||
"app_version": "$appVersion",
|
"app_version": "$appVersion",
|
||||||
"device_name": "$userName's ${Device().model}",
|
"device_name": "$userName's ${Device().model}",
|
||||||
"manufacturer": Device().manufacturer,
|
"manufacturer": Device().manufacturer,
|
||||||
@ -93,11 +94,17 @@ class HomeAssistant {
|
|||||||
"os_name": Device().osName,
|
"os_name": Device().osName,
|
||||||
"os_version": Device().osVersion,
|
"os_version": Device().osVersion,
|
||||||
"app_data": {
|
"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) {
|
if (Connection().webhookId == null || forceRegister) {
|
||||||
Logger.d("Mobile app was not registered yet or need to be reseted. Registering...");
|
Logger.d("Mobile app was not registered yet or need to be reseted. Registering...");
|
||||||
|
var registrationData = _getAppRegistrationData();
|
||||||
registrationData.addAll({
|
registrationData.addAll({
|
||||||
"app_id": "ha_client",
|
"app_id": "ha_client",
|
||||||
"app_name": "$appName",
|
"app_name": "$appName",
|
||||||
@ -110,7 +117,6 @@ class HomeAssistant {
|
|||||||
).then((response) {
|
).then((response) {
|
||||||
Logger.d("Processing registration responce...");
|
Logger.d("Processing registration responce...");
|
||||||
var responseObject = json.decode(response);
|
var responseObject = json.decode(response);
|
||||||
Logger.d(responseObject.toString());
|
|
||||||
SharedPreferences.getInstance().then((prefs) {
|
SharedPreferences.getInstance().then((prefs) {
|
||||||
prefs.setString("app-webhook-id", responseObject["webhook_id"]);
|
prefs.setString("app-webhook-id", responseObject["webhook_id"]);
|
||||||
prefs.setString("registered-app-version", "$appVersion");
|
prefs.setString("registered-app-version", "$appVersion");
|
||||||
@ -125,7 +131,7 @@ class HomeAssistant {
|
|||||||
Logger.d("Registered app version is old. Registration need to be updated");
|
Logger.d("Registered app version is old. Registration need to be updated");
|
||||||
var updateData = {
|
var updateData = {
|
||||||
"type": "update_registration",
|
"type": "update_registration",
|
||||||
"data": registrationData
|
"data": _getAppRegistrationData()
|
||||||
};
|
};
|
||||||
Connection().sendHTTPPost(
|
Connection().sendHTTPPost(
|
||||||
endPoint: "/api/webhook/${Connection().webhookId}",
|
endPoint: "/api/webhook/${Connection().webhookId}",
|
||||||
|
@ -169,6 +169,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
StreamSubscription _showEntityPageSubscription;
|
StreamSubscription _showEntityPageSubscription;
|
||||||
StreamSubscription _showErrorSubscription;
|
StreamSubscription _showErrorSubscription;
|
||||||
StreamSubscription _startAuthSubscription;
|
StreamSubscription _startAuthSubscription;
|
||||||
|
StreamSubscription _showDialogSubscription;
|
||||||
StreamSubscription _reloadUISubscription;
|
StreamSubscription _reloadUISubscription;
|
||||||
int _previousViewCount;
|
int _previousViewCount;
|
||||||
bool _showLoginButton = false;
|
bool _showLoginButton = false;
|
||||||
@ -265,6 +266,18 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
_quickLoad();
|
_quickLoad();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (_showDialogSubscription == null) {
|
||||||
|
_showDialogSubscription = eventBus.on<ShowDialogEvent>().listen((event){
|
||||||
|
_showDialog(
|
||||||
|
title: event.title,
|
||||||
|
body: event.body,
|
||||||
|
onPositive: event.onPositive,
|
||||||
|
onNegative: event.onNegative,
|
||||||
|
positiveText: event.positiveText,
|
||||||
|
negativeText: event.negativeText
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
if (_serviceCallSubscription == null) {
|
if (_serviceCallSubscription == null) {
|
||||||
_serviceCallSubscription =
|
_serviceCallSubscription =
|
||||||
eventBus.on<ServiceCallEvent>().listen((event) {
|
eventBus.on<ServiceCallEvent>().listen((event) {
|
||||||
@ -294,23 +307,11 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/*_firebaseMessaging.getToken().then((String token) {
|
_firebaseMessaging.getToken().then((String token) {
|
||||||
Logger.d("Device name: ${json.encode(Connection().unicDeviceName)}");
|
HomeAssistant().fcmToken = token;
|
||||||
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();
|
completer.complete();
|
||||||
});
|
});
|
||||||
}).catchError((e) {
|
//completer.complete();
|
||||||
Logger.e("Error registering notification listener: ${e.toString()}");
|
|
||||||
completer.complete();
|
|
||||||
});*/
|
|
||||||
completer.complete();
|
|
||||||
return completer.future;
|
return completer.future;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -343,6 +344,41 @@ class _MainPageState extends State<MainPage> 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: <Widget>[
|
||||||
|
// 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) {
|
void _callService(String domain, String service, String entityId, Map additionalParams) {
|
||||||
_showInfoBottomBar(
|
_showInfoBottomBar(
|
||||||
message: "Calling $domain.$service",
|
message: "Calling $domain.$service",
|
||||||
|
@ -91,11 +91,33 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resetRegistration() {
|
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() {
|
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
|
@override
|
||||||
|
@ -173,6 +173,17 @@ class ServiceCallEvent {
|
|||||||
ServiceCallEvent(this.domain, this.service, this.entityId, this.additionalParams);
|
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 {
|
class ShowEntityPageEvent {
|
||||||
Entity entity;
|
Entity entity;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user