Moved notification handling to serverside

This commit is contained in:
2023-09-08 20:54:01 +02:00
parent 5f0426aab9
commit 6816e77d21
5 changed files with 77 additions and 86 deletions

View File

@ -1,9 +1,15 @@
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:developer';
import 'package:crypto/crypto.dart';
import 'package:fast_rsa/fast_rsa.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import '../api/mhsl/notify/register/notifyRegister.dart';
import '../api/mhsl/notify/register/notifyRegisterParams.dart';
import '../model/accountData.dart';
import '../model/endpointData.dart';
class NotifyUpdater {
static void registerToServer() async {
@ -19,4 +25,40 @@ class NotifyUpdater {
),
).run();
}
static Future<void> registerNcPush() async {
log("Starting");
log("Generate keys");
final rsaKey = await RSA.generate(2048);
final devicePrivateKey = rsaKey.privateKey.toString();
final devicePublicKey = rsaKey.publicKey.toString();
log("Private: \n$devicePrivateKey");
log("Public: \n$devicePublicKey");
final pushToken = await FirebaseMessaging.instance.getToken();
log("PushToken: $pushToken}");
final pushTokenHash = sha512.convert(utf8.encode(pushToken!));
log("PushTokenHash: $pushTokenHash");
final requestMap = {
"format": "json",
"pushTokenHash": pushTokenHash.toString(),
"devicePublicKey": devicePublicKey.replaceAll("\n", "").replaceAll("-----END RSA PUBLIC KEY-----", "").replaceAll("-----BEGIN RSA PUBLIC KEY-----", "").toString(),
"proxyServer": "https://push-notifications.nextcloud.com/devices"
};
log(jsonEncode(requestMap));
http.post(
//${AccountData().buildHttpAuthString()}@
Uri.parse("https://${EndpointData().nextcloud().full()}/ocs/v2.php/apps/notifications/api/v2/push"),
headers: {
"OCS-APIRequest": "true",
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer Fv3g7g9jW91FXNjZLaJmyprClfy8pX1jEM3hJGbXjPEFcx4oGIEVcpwEnuT4mPs39D9xT063"
},
body: jsonEncode(requestMap),
).then((response) {
log("Response: ${response.statusCode}\n${response.body}");
});
}
}