1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 21:09:21 +00:00
comunicmobile/lib/helpers/push_notifications_helper.dart

110 lines
3.9 KiB
Dart
Raw Normal View History

import 'package:comunic/helpers/firebase_messaging_helper.dart';
2021-04-14 16:03:27 +00:00
import 'package:comunic/helpers/independent_push_notifications_helper.dart';
2021-04-12 17:26:05 +00:00
import 'package:comunic/helpers/preferences_helper.dart';
2021-04-13 15:21:00 +00:00
import 'package:comunic/helpers/server_config_helper.dart';
2021-04-12 17:26:05 +00:00
import 'package:comunic/models/api_request.dart';
2021-04-13 15:21:00 +00:00
import 'package:comunic/utils/flutter_utils.dart';
import 'package:flutter/material.dart';
2021-04-12 17:26:05 +00:00
/// Push notifications helper
///
/// @author Pierre Hubert
enum PushNotificationsStatus { UNDEFINED, DISABLED, FIREBASE, INDEPENDENT }
const _PushNotificationsAPIMap = {
"undefined": PushNotificationsStatus.UNDEFINED,
"disabled": PushNotificationsStatus.DISABLED,
"firebase": PushNotificationsStatus.FIREBASE,
"independent": PushNotificationsStatus.INDEPENDENT
};
class PushNotificationsHelper {
/// Get cached status of push notifications
static Future<PushNotificationsStatus?> getLocalStatus() async {
2021-04-12 17:26:05 +00:00
final pref = await PreferencesHelper.getInstance();
if (!pref.containsKey(PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS))
return PushNotificationsStatus.UNDEFINED;
return _PushNotificationsAPIMap[
pref.getString(PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS)!];
2021-04-12 17:26:05 +00:00
}
/// Refresh local status with information from server
///
/// Throws in case of failure
static Future<void> refreshLocalStatus() async {
final response = await APIRequest.withLogin("push_notifications/status")
.execWithThrowGetObject();
2021-04-12 17:34:06 +00:00
var status = _PushNotificationsAPIMap[response["status"]];
if (status == null) status = PushNotificationsStatus.UNDEFINED;
2021-04-12 17:26:05 +00:00
2021-04-12 17:34:06 +00:00
if (status == PushNotificationsStatus.INDEPENDENT) {
2021-04-14 16:03:27 +00:00
// Invoke plugin to apply new WebSocket URL
await IndependentPushNotificationsHelper.configure(
response["independent_push_url"]);
2021-04-12 17:26:05 +00:00
}
2021-04-12 17:34:06 +00:00
2022-03-10 19:36:55 +00:00
await (await PreferencesHelper.getInstance()).setString(
2021-04-12 17:34:06 +00:00
PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS, response["status"]);
2021-04-12 17:26:05 +00:00
}
/// Clear local push notifications status
static Future<void> clearLocalStatus() async {
2022-03-10 19:36:55 +00:00
await (await PreferencesHelper.getInstance())
2021-04-12 17:26:05 +00:00
.removeKey(PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS);
2021-04-14 16:03:27 +00:00
// Stop local refresh notification refresh
IndependentPushNotificationsHelper.disable();
2021-04-12 17:26:05 +00:00
}
/// Configure push notifications
static Future<void> configure(
BuildContext context, PushNotificationsStatus? newStatus) async {
String? firebaseToken = "";
switch (newStatus) {
case PushNotificationsStatus.DISABLED:
break;
case PushNotificationsStatus.FIREBASE:
await FirebaseMessagingHelper.preConfigure();
firebaseToken = await FirebaseMessagingHelper.getToken();
break;
case PushNotificationsStatus.INDEPENDENT:
if (await IndependentPushNotificationsHelper.needPreConfiguration())
await IndependentPushNotificationsHelper.preConfigure(context);
break;
default:
throw new Exception("Unknown status!");
}
await PushNotificationsHelper.clearLocalStatus();
await PushNotificationsHelper.setNewStatus(newStatus,
firebaseToken: firebaseToken);
await PushNotificationsHelper.refreshLocalStatus();
}
2021-04-12 17:26:05 +00:00
/// Set new push notification status on the server
static Future<void> setNewStatus(
PushNotificationsStatus? newStatus, {
String? firebaseToken = "",
2021-04-12 17:26:05 +00:00
}) async =>
await APIRequest.withLogin("push_notifications/configure")
.addString(
"status",
_PushNotificationsAPIMap.entries
.firstWhere((e) => e.value == newStatus)
.key)
.addString("firebase_token", firebaseToken)
.execWithThrow();
2021-04-13 15:21:00 +00:00
/// Is true if possible if push notifications are configurable
static bool get arePushNotificationsAvailable =>
srvConfig!.notificationsPolicy.hasFirebase ||
(isAndroid && srvConfig!.notificationsPolicy.hasIndependent);
2021-04-12 17:26:05 +00:00
}