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(

View File

@@ -42,7 +42,7 @@ class _ChannelRootPageState extends State<ChannelRootPage> {
}
try {
final items = await APIClient.getChannelList(acc.auth!, ChannelSelector.all);
final items = (await APIClient.getChannelList(acc.auth!, ChannelSelector.all)).map((p) => p.channel).toList();
items.sort((a, b) => -1 * (a.timestampLastSent ?? '').compareTo(b.timestampLastSent ?? ''));

View File

@@ -17,28 +17,28 @@ class _DebugActionsPageState extends State<DebugActionsPage> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ElevatedButton(
style: ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
FilledButton(
style: FilledButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
onPressed: () => Toaster.success("Hello World", "This was a triumph!"),
child: const Text('Show Success Notification'),
),
ElevatedButton(
style: ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
FilledButton(
style: FilledButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
onPressed: () => Toaster.info("Hello World", "This was a triumph!"),
child: const Text('Show Info Notification'),
),
ElevatedButton(
style: ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
FilledButton(
style: FilledButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
onPressed: () => Toaster.warn("Hello World", "This was a triumph!"),
child: const Text('Show Warn Notification'),
),
ElevatedButton(
style: ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
FilledButton(
style: FilledButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
onPressed: () => Toaster.error("Hello World", "This was a triumph!"),
child: const Text('Show Info Notification'),
),
ElevatedButton(
style: ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
FilledButton(
style: FilledButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
onPressed: () => Toaster.simple("Hello World"),
child: const Text('Show Simple Notification'),
),

View File

@@ -48,7 +48,7 @@ class _MessageListPageState extends State<MessageListPage> {
try {
if (_channels == null) {
final channels = await APIClient.getChannelList(acc.auth!, ChannelSelector.allAny);
_channels = <String, ChannelWithSubscription>{for (var v in channels) v.channelID: v};
_channels = <String, Channel>{for (var v in channels) v.channel.channelID: v.channel};
}
final (npt, newItems) = await APIClient.getMessageList(acc.auth!, thisPageToken, pageSize: _pageSize);

View File

@@ -63,8 +63,8 @@ class _SendRootPageState extends State<SendRootPage> {
),
),
const SizedBox(height: 16),
ElevatedButton(
style: ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
FilledButton(
style: FilledButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
onPressed: _send,
child: const Text('Send'),
),
@@ -92,7 +92,7 @@ class _SendRootPageState extends State<SendRootPage> {
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}'); //TODO better error display
}
var url = 'https://simplecloudnotifier.com?preset_user_id=${acc.user!.userID}&preset_user_key=TODO'; // TODO get send-only key
var url = 'https://simplecloudnotifier.de?preset_user_id=${acc.user!.userID}&preset_user_key=${acc.auth!.tokenSend}';
return GestureDetector(
onTap: () {
_openWeb(url);
@@ -121,7 +121,7 @@ class _SendRootPageState extends State<SendRootPage> {
);
}
var url = 'https://simplecloudnotifier.com?preset_user_id=${acc.user!.userID}&preset_user_key=TODO'; // TODO get send-only key
var url = 'https://simplecloudnotifier.de?preset_user_id=${acc.user!.userID}&preset_user_key=${acc.auth!.tokenSend}';
return GestureDetector(
onTap: () {