WIP #48 Notifications with mobile_app component
This commit is contained in:
parent
5b99ade088
commit
491c2b0dc0
@ -14,6 +14,8 @@ class HomeAssistant {
|
||||
String _userName;
|
||||
HSVColor savedColor;
|
||||
|
||||
String fcmToken;
|
||||
|
||||
Map _rawLovelaceData;
|
||||
|
||||
List<Panel> 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}",
|
||||
|
@ -169,6 +169,7 @@ class _MainPageState extends State<MainPage> 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<MainPage> with WidgetsBindingObserver, Ticker
|
||||
_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) {
|
||||
_serviceCallSubscription =
|
||||
eventBus.on<ServiceCallEvent>().listen((event) {
|
||||
@ -294,23 +307,11 @@ class _MainPageState extends State<MainPage> 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()}");
|
||||
_firebaseMessaging.getToken().then((String token) {
|
||||
HomeAssistant().fcmToken = token;
|
||||
completer.complete();
|
||||
});
|
||||
}).catchError((e) {
|
||||
Logger.e("Error registering notification listener: ${e.toString()}");
|
||||
completer.complete();
|
||||
});*/
|
||||
completer.complete();
|
||||
//completer.complete();
|
||||
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) {
|
||||
_showInfoBottomBar(
|
||||
message: "Calling $domain.$service",
|
||||
|
@ -91,11 +91,33 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
|
||||
}
|
||||
|
||||
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
|
||||
|
@ -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;
|
||||
|
||||
|
Reference in New Issue
Block a user