From 8d7aaa44a624d728027ce20a198164071ab84f8d Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Fri, 3 Jan 2020 17:23:40 +0100 Subject: [PATCH] Add firebase stuff --- .gitignore | 1 + android/app/build.gradle | 2 + android/app/src/main/AndroidManifest.xml | 4 ++ android/build.gradle | 1 + lib/components/matrix.dart | 54 +++++++++++++++++++++++- pubspec.lock | 7 +++ pubspec.yaml | 1 + 7 files changed, 69 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c99ad71..7f3d374 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ .history .svn/ lib/generated_plugin_registrant.dart +google-services.json # IntelliJ related *.iml diff --git a/android/app/build.gradle b/android/app/build.gradle index 794929a..f8c52de 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -85,3 +85,5 @@ dependencies { androidTestImplementation 'androidx.test:runner:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' } + +apply plugin: "com.google.gms.google-services" \ No newline at end of file diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 95392b8..1d25d5d 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -21,6 +21,10 @@ + + + + diff --git a/android/build.gradle b/android/build.gradle index 3100ad2..70b3637 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -8,6 +8,7 @@ buildscript { dependencies { classpath 'com.android.tools.build:gradle:3.5.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + classpath 'com.google.gms:google-services:4.3.2' } } diff --git a/lib/components/matrix.dart b/lib/components/matrix.dart index 5d5b225..9d50381 100644 --- a/lib/components/matrix.dart +++ b/lib/components/matrix.dart @@ -1,4 +1,7 @@ +import 'dart:async'; import 'dart:convert'; +import 'dart:io'; +import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:famedlysdk/famedlysdk.dart'; import 'package:fluffychat/utils/sqflite_store.dart'; import 'package:flutter/foundation.dart'; @@ -31,6 +34,8 @@ class MatrixState extends State { Client client; BuildContext context; + FirebaseMessaging _firebaseMessaging = FirebaseMessaging(); + /// Used to load the old account if there is no store available. void loadAccount() async { final LocalStorage storage = LocalStorage('LocalStorage'); @@ -124,10 +129,50 @@ class MatrixState extends State { hideLoadingDialog() => Navigator.of(_loadingDialogContext)?.pop(); + StreamSubscription onSetupFirebase; + + void setupFirebase(LoginState login) { + if (login != LoginState.logged) return; + if (Platform.isIOS) iOS_Permission(); + + _firebaseMessaging.getToken().then((token) { + print("Der token ist: $token"); + client.setPushers( + token, + "http", + "chat.fluffy.fluffychat", + "FluffyChat", + client.deviceName, + "en", + "https://janian.de:7023/", + append: false, + format: "event_id_only", + ); + }); + + _firebaseMessaging.configure( + onResume: (Map message) async { + print('on resume $message'); + }, + onLaunch: (Map message) async { + print('on launch $message'); + }, + ); + } + + void iOS_Permission() { + _firebaseMessaging.requestNotificationPermissions( + IosNotificationSettings(sound: true, badge: true, alert: true)); + _firebaseMessaging.onIosSettingsRegistered + .listen((IosNotificationSettings settings) { + print("Settings registered: $settings"); + }); + } + @override void initState() { if (widget.client == null) { - client = Client(widget.clientName, debug: false); + client = Client(widget.clientName, debug: true); if (!kIsWeb) { client.store = Store(client); } else { @@ -136,9 +181,16 @@ class MatrixState extends State { } else { client = widget.client; } + onSetupFirebase ??= client.onLoginStateChanged.stream.listen(setupFirebase); super.initState(); } + @override + void dispose() { + onSetupFirebase?.cancel(); + super.dispose(); + } + @override Widget build(BuildContext context) { return _InheritedMatrix( diff --git a/pubspec.lock b/pubspec.lock index dad4267..a22b3db 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -94,6 +94,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.4.3+2" + firebase_messaging: + dependency: "direct main" + description: + name: firebase_messaging + url: "https://pub.dartlang.org" + source: hosted + version: "6.0.9" flutter: dependency: "direct main" description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index 0565d4e..be8ddc6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -38,6 +38,7 @@ dependencies: url_launcher: ^5.4.1 sqflite: ^1.2.0 cached_network_image: ^2.0.0 + firebase_messaging: ^6.0.9 dev_dependencies: flutter_test: