1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2025-06-19 00:05:16 +00:00

Start to integrate push notifications

This commit is contained in:
2021-04-12 19:26:05 +02:00
parent 38c639331f
commit 612fc7b0d9
9 changed files with 324 additions and 2 deletions

View File

@ -1,5 +1,6 @@
import 'package:comunic/helpers/api_helper.dart';
import 'package:comunic/helpers/preferences_helper.dart';
import 'package:comunic/helpers/push_notifications_helper.dart';
import 'package:comunic/helpers/websocket_helper.dart';
import 'package:comunic/models/api_request.dart';
import 'package:comunic/models/authentication_details.dart';
@ -77,6 +78,8 @@ class AccountHelper {
/// Sign out user
Future<void> signOut() async {
await PushNotificationsHelper.clearLocalStatus();
await APIRequest.withLogin("account/logout").exec();
final preferencesHelper = await PreferencesHelper.getInstance();

View File

@ -13,6 +13,7 @@ enum PreferencesKeyList {
ENABLE_DARK_THEME,
FORCE_MOBILE_MODE,
SHOW_PERFORMANCE_OVERLAY,
PUSH_NOTIFICATIONS_STATUS,
}
const _PreferenceKeysName = {
@ -62,6 +63,10 @@ class PreferencesHelper {
}
}
bool containsKey(PreferencesKeyList key) {
return _sharedPreferences.containsKey(_PreferenceKeysName[key]);
}
Future<bool> removeKey(PreferencesKeyList key) async {
return await _sharedPreferences.remove(_PreferenceKeysName[key]);
}

View File

@ -0,0 +1,88 @@
import 'package:comunic/helpers/preferences_helper.dart';
import 'package:comunic/models/api_request.dart';
/// 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 {
final pref = await PreferencesHelper.getInstance();
if (!pref.containsKey(PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS))
return PushNotificationsStatus.UNDEFINED;
return _PushNotificationsAPIMap[
pref.getString(PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS)];
}
/// Refresh local status with information from server
///
/// Throws in case of failure
static Future<void> refreshLocalStatus() async {
final pref = await PreferencesHelper.getInstance();
final response = await APIRequest.withLogin("push_notifications/status")
.execWithThrowGetObject();
switch (response["status"]) {
case "undefined":
await pref.removeKey(PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS);
break;
case "disabled":
await pref.setString(
PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS, "disabled");
break;
case "firebase":
await pref.setString(
PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS, "firebase");
break;
case "independent":
await pref.setString(
PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS, "independent");
// TODO : Invoke plugin to apply new WebSocket URL
break;
default:
print(
"Push notifications status ${response["status"]} is unknown, defaulting to disabled!");
await pref.setString(
PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS, "disabled");
}
}
/// Clear local push notifications status
static Future<void> clearLocalStatus() async {
await (await PreferencesHelper.getInstance())
.removeKey(PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS);
// TODO : stop local refresh notification refresh
}
/// Set new push notification status on the server
static Future<void> setNewStatus(
PushNotificationsStatus newStatus, {
String firebaseToken = "",
}) async =>
await APIRequest.withLogin("push_notifications/configure")
.addString(
"status",
_PushNotificationsAPIMap.entries
.firstWhere((e) => e.value == newStatus)
.key)
.addString("firebase_token", firebaseToken)
.execWithThrow();
}

View File

@ -17,6 +17,7 @@ class ServerConfigurationHelper {
(await APIRequest.withoutLogin("server/config").execWithThrow())
.getObject();
final pushNotificationsPolicy = response["push_notifications"];
final passwordPolicy = response["password_policy"];
final dataConservationPolicy = response["data_conservation_policy"];
final conversationsPolicy = response["conversations_policy"];
@ -27,6 +28,10 @@ class ServerConfigurationHelper {
termsURL: response["terms_url"],
playStoreURL: response["play_store_url"],
androidDirectDownloadURL: response["android_direct_download_url"],
notificationsPolicy: NotificationsPolicy(
hasFirebase: pushNotificationsPolicy["has_firebase"],
hasIndependent: pushNotificationsPolicy["has_independent"],
),
passwordPolicy: PasswordPolicy(
allowMailInPassword: passwordPolicy["allow_email_in_password"],
allowNameInPassword: passwordPolicy["allow_name_in_password"],