Compare commits

...

3 Commits

Author SHA1 Message Date
24bf7cd434 Fix notification sounds under iOS
Some checks failed
Build Docker and Deploy / Build Docker Container (push) Successful in 1m38s
Build Docker and Deploy / Run Unit-Tests (push) Failing after 9m23s
Build Docker and Deploy / Deploy to Server (push) Has been skipped
2025-12-18 15:02:51 +01:00
54c4f873fc [Flutter] Use deviceName instead of hostName for clients 2025-12-18 14:57:46 +01:00
b2de793758 [Flutter] Fix sending notifications without content 2025-12-18 14:36:45 +01:00
8 changed files with 70 additions and 23 deletions

View File

@@ -6,6 +6,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:in_app_purchase/in_app_purchase.dart'; import 'package:in_app_purchase/in_app_purchase.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:simplecloudnotifier/api/api_client.dart';
import 'package:simplecloudnotifier/main_messaging.dart'; import 'package:simplecloudnotifier/main_messaging.dart';
import 'package:simplecloudnotifier/main_utils.dart'; import 'package:simplecloudnotifier/main_utils.dart';
import 'package:simplecloudnotifier/components/layout/nav_layout.dart'; import 'package:simplecloudnotifier/components/layout/nav_layout.dart';
@@ -68,13 +69,15 @@ void main() async {
print('[INIT] Request Notification permissions...'); print('[INIT] Request Notification permissions...');
await FirebaseMessaging.instance.requestPermission(provisional: true); await FirebaseMessaging.instance.requestPermission(provisional: true);
FirebaseMessaging.instance.onTokenRefresh.listen((fcmToken) { FirebaseMessaging.instance.onTokenRefresh
.listen((fcmToken) {
try { try {
setFirebaseToken(fcmToken); setFirebaseToken(fcmToken);
} catch (exc, trace) { } catch (exc, trace) {
ApplicationLog.error('Failed to set firebase token: ' + exc.toString(), trace: trace); ApplicationLog.error('Failed to set firebase token: ' + exc.toString(), trace: trace);
} }
}).onError((dynamic err) { })
.onError((dynamic err) {
ApplicationLog.error('Failed to listen to token refresh events: ' + (err?.toString() ?? '')); ApplicationLog.error('Failed to listen to token refresh events: ' + (err?.toString() ?? ''));
}); });
@@ -96,6 +99,25 @@ void main() async {
await appAuth.tryMigrateFromV1(); await appAuth.tryMigrateFromV1();
if (appAuth.isAuth()) {
print('[INIT] Load Client and potentially update...');
try {
var client = await appAuth.loadClient(onlyCached: true);
if (client != null) {
if (client.agentModel != Globals().deviceModel || client.name != Globals().nameForClient() || client.agentVersion != Globals().version) {
print('[INIT] Update Client info...');
final newClient = await APIClient.updateClient(appAuth, client.clientID, agentModel: Globals().deviceModel, name: Globals().nameForClient(), agentVersion: Globals().version);
appAuth.setClientAndClientID(newClient);
await appAuth.save();
}
}
} catch (exc, trace) {
ApplicationLog.error('Failed to get client (on init): ' + exc.toString(), trace: trace);
}
}
print('[INIT] Load Notifications...'); print('[INIT] Load Notifications...');
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

View File

@@ -40,11 +40,11 @@ void setFirebaseToken(String fcmToken) async {
if (client == null) { if (client == null) {
// should not really happen - perhaps someone externally deleted the client? // should not really happen - perhaps someone externally deleted the client?
final newClient = await APIClient.addClient(acc, fcmToken, Globals().deviceModel, Globals().version, Globals().hostname, Globals().clientType); final newClient = await APIClient.addClient(acc, fcmToken, Globals().deviceModel, Globals().version, Globals().nameForClient(), Globals().clientType);
acc.setClientAndClientID(newClient); acc.setClientAndClientID(newClient);
await acc.save(); await acc.save();
} else { } else {
final newClient = await APIClient.updateClient(acc, client.clientID, fcmToken: fcmToken, agentModel: Globals().deviceModel, name: Globals().hostname, agentVersion: Globals().version); final newClient = await APIClient.updateClient(acc, client.clientID, fcmToken: fcmToken, agentModel: Globals().deviceModel, name: Globals().nameForClient(), agentVersion: Globals().version);
acc.setClientAndClientID(newClient); acc.setClientAndClientID(newClient);
await acc.save(); await acc.save();
} }

View File

@@ -527,7 +527,7 @@ class _AccountRootPageState extends State<AccountRootPage> {
await Globals().setPrefFCMToken(fcmToken); await Globals().setPrefFCMToken(fcmToken);
final user = await APIClient.createUserWithClient(null, fcmToken, Globals().platform, Globals().version, Globals().hostname, Globals().clientType); final user = await APIClient.createUserWithClient(null, fcmToken, Globals().platform, Globals().version, Globals().nameForClient(), Globals().clientType);
acc.set(user.user, user.clients[0], user.adminKey, user.sendKey); acc.set(user.user, user.clients[0], user.adminKey, user.sendKey);

View File

@@ -156,7 +156,7 @@ class _AccountLoginPageState extends State<AccountLoginPage> {
final user = await APIClient.getUser(DirectTokenSource(uid, atokv), uid); final user = await APIClient.getUser(DirectTokenSource(uid, atokv), uid);
final client = await APIClient.addClient(DirectTokenSource(uid, atokv), fcmToken, Globals().deviceModel, Globals().version, Globals().hostname, Globals().clientType); final client = await APIClient.addClient(DirectTokenSource(uid, atokv), fcmToken, Globals().deviceModel, Globals().version, Globals().nameForClient(), Globals().clientType);
acc.set(user, client, atokv, stokv); acc.set(user, client, atokv, stokv);
await acc.save(); await acc.save();

View File

@@ -284,7 +284,9 @@ class _SendRootPageState extends State<SendRootPage> {
} }
try { try {
await APIClient.sendMessage(acc.userID!, acc.tokenSend!, _msgContent.text); var content = (_msgContent.text != '') ? _msgContent.text : null;
await APIClient.sendMessage(acc.userID!, acc.tokenSend!, _msgTitle.text, content: content);
Toaster.success("Success", 'Message sent'); Toaster.success("Success", 'Message sent');
setState(() { setState(() {
_msgTitle.clear(); _msgTitle.clear();
@@ -306,7 +308,11 @@ class _SendRootPageState extends State<SendRootPage> {
} }
try { try {
await APIClient.sendMessage(acc.userID!, acc.tokenSend!, _msgContent.text, channel: _channelName.text, senderName: _senderName.text, priority: _priority); var content = (_msgContent.text != '') ? _msgContent.text : null;
var channel = (_channelName.text != '') ? _channelName.text : null;
var sender = (_senderName.text != '') ? _senderName.text : null;
await APIClient.sendMessage(acc.userID!, acc.tokenSend!, _msgTitle.text, content: content, channel: channel, senderName: sender, priority: _priority);
Toaster.success("Success", 'Message sent'); Toaster.success("Success", 'Message sent');
setState(() { setState(() {
_msgTitle.clear(); _msgTitle.clear();

View File

@@ -99,7 +99,7 @@ class AppAuth extends ChangeNotifier implements TokenSource {
final user = await APIClient.getUser(DirectTokenSource(oldUserID, oldUserKey), oldUserID); final user = await APIClient.getUser(DirectTokenSource(oldUserID, oldUserKey), oldUserID);
final client = await APIClient.addClient(DirectTokenSource(oldUserID, oldUserKey), fcmToken, Globals().deviceModel, Globals().version, Globals().hostname, Globals().clientType); final client = await APIClient.addClient(DirectTokenSource(oldUserID, oldUserKey), fcmToken, Globals().deviceModel, Globals().version, Globals().nameForClient(), Globals().clientType);
set(user, client, oldUserKey, newTokenSend.token); set(user, client, oldUserKey, newTokenSend.token);
@@ -232,7 +232,7 @@ class AppAuth extends ChangeNotifier implements TokenSource {
return _user?.$1; return _user?.$1;
} }
Future<Client?> loadClient({bool force = false, Duration? forceIfOlder = null}) async { Future<Client?> loadClient({bool force = false, Duration? forceIfOlder = null, bool onlyCached = false}) async {
if (forceIfOlder != null && _client != null && _client!.$2.difference(DateTime.now()) > forceIfOlder) { if (forceIfOlder != null && _client != null && _client!.$2.difference(DateTime.now()) > forceIfOlder) {
force = true; force = true;
} }
@@ -245,6 +245,10 @@ class AppAuth extends ChangeNotifier implements TokenSource {
throw Exception('Not authenticated'); throw Exception('Not authenticated');
} }
if (onlyCached) {
return null;
}
try { try {
final client = await APIClient.getClient(this, _clientID!); final client = await APIClient.getClient(this, _clientID!);

View File

@@ -92,4 +92,12 @@ class Globals {
Future<bool> setPrefFCMToken(String value) { Future<bool> setPrefFCMToken(String value) {
return sharedPrefs.setString("fcm.token", value); return sharedPrefs.setString("fcm.token", value);
} }
String nameForClient() {
if (this.deviceName.isNotEmpty) {
return this.deviceName;
} else {
return this.hostname;
}
}
} }

View File

@@ -1,22 +1,23 @@
package push package push
import ( import (
scn "blackforestbytes.com/simplecloudnotifier"
"blackforestbytes.com/simplecloudnotifier/models"
"bytes" "bytes"
"context" "context"
_ "embed" _ "embed"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"io" "io"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"time" "time"
scn "blackforestbytes.com/simplecloudnotifier"
"blackforestbytes.com/simplecloudnotifier/models"
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
) )
// https://firebase.google.com/docs/cloud-messaging/send-message#rest // https://firebase.google.com/docs/cloud-messaging/send-message#rest
@@ -66,7 +67,13 @@ func (fb FirebaseConnector) SendNotification(ctx context.Context, user models.Us
"title": msg.Title, "title": msg.Title,
"body": msg.ShortContent(), "body": msg.ShortContent(),
}, },
"apns": gin.H{}, "apns": gin.H{
"payload": gin.H{
"aps": gin.H{
"sound": "default",
},
},
},
} }
} else if client.Type == models.ClientTypeAndroid { } else if client.Type == models.ClientTypeAndroid {
jsonBody = gin.H{ jsonBody = gin.H{