WIP #48 Notifications

This commit is contained in:
estevez-dev 2019-03-29 13:09:34 +02:00
parent 4e96b9adbb
commit 102b10ade0
3 changed files with 32 additions and 29 deletions

View File

@ -10,7 +10,7 @@ class AuthManager {
AuthManager._internal();
Future getTempToken({String httpWebHost, String oauthUrl}) {
Future getTempToken({String oauthUrl}) {
Completer completer = Completer();
final flutterWebviewPlugin = new FlutterWebviewPlugin();
flutterWebviewPlugin.onUrlChanged.listen((String url) {
@ -18,7 +18,6 @@ class AuthManager {
String authCode = url.split("=")[1];
Logger.d("We have auth code. Getting temporary access token...");
Connection().sendHTTPPost(
host: httpWebHost,
endPoint: "/auth/token",
contentType: "application/x-www-form-urlencoded",
includeAuthHeader: false,

View File

@ -54,7 +54,6 @@ class Connection {
oauthUrl = "$httpWebHost/auth/authorize?client_id=${Uri.encodeComponent('http://ha-client.homemade.systems/')}&redirect_uri=${Uri.encodeComponent('http://ha-client.homemade.systems/service/auth_callback.html')}";
if (_token == null) {
await AuthManager().getTempToken(
httpWebHost: httpWebHost,
oauthUrl: oauthUrl
).then((token) {
Logger.d("Token from AuthManager recived");
@ -311,16 +310,16 @@ class Connection {
}
}
Future sendHTTPPost({String host, String endPoint, String data, String contentType: "application/json", bool includeAuthHeader: true, String authToken}) async {
Future sendHTTPPost({String endPoint, String data, String contentType: "application/json", bool includeAuthHeader: true}) async {
Completer completer = Completer();
String url = "$host$endPoint";
String url = "$httpWebHost$endPoint";
Logger.d("[Sending] ==> $url");
Map<String, String> headers = {};
if (contentType != null) {
headers["Content-Type"] = contentType;
}
if (includeAuthHeader) {
headers["authorization"] = "Bearer $authToken";
headers["authorization"] = "Bearer $_token";
}
http.post(
url,

View File

@ -168,15 +168,25 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
StreamSubscription _startAuthSubscription;
StreamSubscription _reloadUISubscription;
int _previousViewCount;
//final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
@override
void initState() {
super.initState();
//widget.homeAssistant = HomeAssistant();
//_settingsLoaded = false;
WidgetsBinding.instance.addObserver(this);
_firebaseMessaging.configure(
onLaunch: (data) {
Logger.d("Notification [onLaunch]: $data");
},
onMessage: (data) {
Logger.d("Notification [onMessage]: $data");
},
onResume: (data) {
Logger.d("Notification [onResume]: $data");
}
);
_settingsSubscription = eventBus.on<SettingsChangedEvent>().listen((event) {
Logger.d("Settings change event: reconnect=${event.reconnect}");
if (event.reconnect) {
@ -187,9 +197,9 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
_initialLoad();
}
void _initialLoad() {
void _initialLoad() async {
_showInfoBottomBar(progress: true,);
_subscribe();
await _subscribe();
widget.homeAssistant.init().then((_){
_fetchData();
}, onError: (e) {
@ -230,7 +240,8 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
}
}
_subscribe() {
Future _subscribe() {
Completer completer = Completer();
if (_stateSubscription == null) {
_stateSubscription = eventBus.on<StateChangedEvent>().listen((event) {
if (event.needToRebuildUI) {
@ -273,26 +284,20 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
});
}
/*_firebaseMessaging.getToken().then((String token) {
_firebaseMessaging.getToken().then((String token) {
//Logger.d("FCM token: $token");
widget.homeAssistant.sendHTTPPost(
widget.homeAssistant.connection.sendHTTPPost(
endPoint: '/api/notify.fcm-android',
jsonData: '{"token": "$token"}'
);
data: '{"token": "$token"}'
).then((_) {
Logger.d("Notificatin listener registered.");
completer.complete();
});
}).catchError((e) {
Logger.e("Error registering notification listener: ${e.toString()}");
completer.complete();
});
_firebaseMessaging.configure(
onLaunch: (data) {
Logger.d("Notification [onLaunch]: $data");
},
onMessage: (data) {
Logger.d("Notification [onMessage]: $data");
},
onResume: (data) {
Logger.d("Notification [onResume]: $data");
}
);*/
return completer.future;
}
void _showOAuth() {