64 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			64 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			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';
 | 
						|
 | 
						|
class NotifyUpdater {
 | 
						|
  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}");
 | 
						|
    });
 | 
						|
  }
 | 
						|
} |