FCM kinda works [does not receive notifications]

This commit is contained in:
2024-06-01 03:06:02 +02:00
parent 4c02afb957
commit 0b7fb533da
15 changed files with 219 additions and 68 deletions

View File

@@ -1,10 +1,16 @@
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';
import 'package:simplecloudnotifier/api/api_client.dart';
import 'package:simplecloudnotifier/models/key_token_auth.dart';
import 'package:simplecloudnotifier/models/user.dart';
import 'package:simplecloudnotifier/pages/account/login.dart';
import 'package:simplecloudnotifier/state/application_log.dart';
import 'package:simplecloudnotifier/state/globals.dart';
import 'package:simplecloudnotifier/state/user_account.dart';
import 'package:simplecloudnotifier/utils/toaster.dart';
class AccountRootPage extends StatefulWidget {
const AccountRootPage({super.key});
@@ -22,6 +28,8 @@ class _AccountRootPageState extends State<AccountRootPage> {
late UserAccount userAcc;
bool loading = false;
@override
void initState() {
super.initState();
@@ -102,25 +110,54 @@ class _AccountRootPageState extends State<AccountRootPage> {
}
Widget buildNoAuth(BuildContext context) {
return Center(
return Padding(
padding: const EdgeInsets.fromLTRB(24, 32, 24, 16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ElevatedButton(
style: ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
onPressed: () {
//TODO
},
child: const Text('Use existing account'),
),
if (!loading)
Center(
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.secondary,
borderRadius: BorderRadius.circular(100),
),
child: Center(child: FaIcon(FontAwesomeIcons.userSecret, size: 96, color: Theme.of(context).colorScheme.onSecondary)),
),
),
if (loading)
Center(
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.secondary,
borderRadius: BorderRadius.circular(100),
),
child: Center(child: CircularProgressIndicator(color: Theme.of(context).colorScheme.onSecondary)),
),
),
const SizedBox(height: 32),
ElevatedButton(
style: ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
FilledButton(
style: FilledButton.styleFrom(textStyle: const TextStyle(fontSize: 24), padding: const EdgeInsets.fromLTRB(8, 12, 8, 12)),
onPressed: () {
//TODO
if (loading) return;
createNewAccount();
},
child: const Text('Create new account'),
),
const SizedBox(height: 16),
FilledButton.tonal(
style: FilledButton.styleFrom(textStyle: const TextStyle(fontSize: 24), padding: const EdgeInsets.fromLTRB(8, 12, 8, 12)),
onPressed: () {
if (loading) return;
Navigator.push(context, MaterialPageRoute<AccountLoginPage>(builder: (context) => AccountLoginPage()));
},
child: const Text('Use existing account'),
),
],
),
);
@@ -391,4 +428,40 @@ class _AccountRootPageState extends State<AccountRootPage> {
),
);
}
void createNewAccount() async {
setState(() => loading = true);
final acc = Provider.of<UserAccount>(context, listen: false);
try {
final notificationSettings = await FirebaseMessaging.instance.requestPermission(provisional: true);
if (notificationSettings.authorizationStatus == AuthorizationStatus.denied) {
Toaster.error("Missing Permission", 'Please allow notifications to create an account');
return;
}
final fcmToken = await FirebaseMessaging.instance.getToken();
if (fcmToken == null) {
Toaster.warn("Missing Token", 'No FCM Token found, please allow notifications, ensure you have a network connection and restart the app');
return;
}
await Globals().setPrefFCMToken(fcmToken);
final user = await APIClient.createUserWithClient(null, fcmToken, Globals().platform, Globals().version, Globals().hostname, Globals().clientType);
acc.setUser(user.user);
acc.setToken(KeyTokenAuth(userId: user.user.userID, tokenAdmin: user.adminKey, tokenSend: user.sendKey));
acc.setClient(user.clients[0]);
await acc.save();
} catch (exc, trace) {
ApplicationLog.error('Failed to create user account: ' + exc.toString(), trace: trace);
Toaster.error("Error", 'Failed to create user account');
} finally {
setState(() => loading = false);
}
}
}

View File

@@ -54,8 +54,8 @@ class _AccountLoginPageState extends State<AccountLoginPage> {
),
),
const SizedBox(height: 16),
ElevatedButton(
style: ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
FilledButton(
style: FilledButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
onPressed: _login,
child: const Text('Login'),
),
@@ -66,7 +66,7 @@ class _AccountLoginPageState extends State<AccountLoginPage> {
void _login() async {
final msgr = ScaffoldMessenger.of(context);
final prov = Provider.of<UserAccount>(context, listen: false);
final acc = Provider.of<UserAccount>(context, listen: false);
try {
final uid = _ctrlUserID.text;
@@ -79,8 +79,8 @@ class _AccountLoginPageState extends State<AccountLoginPage> {
content: Text('Data ok'), //TODO use toast?
),
);
prov.setToken(KeyTokenAuth(userId: uid, token: tok));
await prov.save();
acc.setToken(KeyTokenAuth(userId: uid, tokenAdmin: tok, tokenSend: '')); //TOTO send token
await acc.save();
widget.onLogin?.call();
} else {
msgr.showSnackBar(