85 lines
3.2 KiB
Dart
85 lines
3.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
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';
|
|
import '../storage/base/settingsProvider.dart';
|
|
import '../widget/confirmDialog.dart';
|
|
|
|
class NotifyUpdater {
|
|
static ConfirmDialog enableAfterDisclaimer(SettingsProvider settings) {
|
|
return ConfirmDialog(
|
|
title: "Warnung",
|
|
icon: Icons.warning_amber,
|
|
content: ""
|
|
"Die Push-Benachrichtigungen werden durch mhsl.eu versendet.\n\n"
|
|
"Durch das aktivieren dieser Funktion wird dein Nutzername, dein Password und eine Geräte-ID von mhsl dauerhaft gespeichert und verarbeitet.\n\n"
|
|
"Für mehr Informationen drücke lange auf die Einstellungsoption!",
|
|
confirmButton: "Aktivieren",
|
|
onConfirm: () {
|
|
FirebaseMessaging.instance.requestPermission(
|
|
provisional: false
|
|
);
|
|
settings.val(write: true).notificationSettings.enabled = true;
|
|
NotifyUpdater.registerToServer();
|
|
},
|
|
);
|
|
}
|
|
static void registerToServer() async {
|
|
String? fcmToken = await FirebaseMessaging.instance.getToken();
|
|
|
|
if(fcmToken == null) throw Exception("Failed to register push notification because there is no FBC token!");
|
|
|
|
NotifyRegister(
|
|
NotifyRegisterParams(
|
|
username: AccountData().getUsername(),
|
|
password: AccountData().getPassword(),
|
|
fcmToken: fcmToken,
|
|
),
|
|
).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}");
|
|
});
|
|
}
|
|
} |