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(); AuthManager._internal();
Future getTempToken({String httpWebHost, String oauthUrl}) { Future getTempToken({String oauthUrl}) {
Completer completer = Completer(); Completer completer = Completer();
final flutterWebviewPlugin = new FlutterWebviewPlugin(); final flutterWebviewPlugin = new FlutterWebviewPlugin();
flutterWebviewPlugin.onUrlChanged.listen((String url) { flutterWebviewPlugin.onUrlChanged.listen((String url) {
@ -18,7 +18,6 @@ class AuthManager {
String authCode = url.split("=")[1]; String authCode = url.split("=")[1];
Logger.d("We have auth code. Getting temporary access token..."); Logger.d("We have auth code. Getting temporary access token...");
Connection().sendHTTPPost( Connection().sendHTTPPost(
host: httpWebHost,
endPoint: "/auth/token", endPoint: "/auth/token",
contentType: "application/x-www-form-urlencoded", contentType: "application/x-www-form-urlencoded",
includeAuthHeader: false, 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')}"; 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) { if (_token == null) {
await AuthManager().getTempToken( await AuthManager().getTempToken(
httpWebHost: httpWebHost,
oauthUrl: oauthUrl oauthUrl: oauthUrl
).then((token) { ).then((token) {
Logger.d("Token from AuthManager recived"); 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(); Completer completer = Completer();
String url = "$host$endPoint"; String url = "$httpWebHost$endPoint";
Logger.d("[Sending] ==> $url"); Logger.d("[Sending] ==> $url");
Map<String, String> headers = {}; Map<String, String> headers = {};
if (contentType != null) { if (contentType != null) {
headers["Content-Type"] = contentType; headers["Content-Type"] = contentType;
} }
if (includeAuthHeader) { if (includeAuthHeader) {
headers["authorization"] = "Bearer $authToken"; headers["authorization"] = "Bearer $_token";
} }
http.post( http.post(
url, url,

View File

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