2020-05-05 10:30:24 +02:00
|
|
|
|
import 'dart:convert';
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
|
|
|
import 'package:fluffychat/components/matrix.dart';
|
2020-05-07 07:52:40 +02:00
|
|
|
|
import 'package:fluffychat/l10n/l10n.dart';
|
2020-05-05 10:30:24 +02:00
|
|
|
|
import 'package:fluffychat/utils/app_route.dart';
|
|
|
|
|
import 'package:fluffychat/views/chat.dart';
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
2020-05-13 10:45:50 +02:00
|
|
|
|
import 'package:bot_toast/bot_toast.dart';
|
2020-05-05 10:30:24 +02:00
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
2020-05-05 12:51:38 +02:00
|
|
|
|
import 'famedlysdk_store.dart';
|
2020-07-21 10:56:26 +02:00
|
|
|
|
import '../components/matrix.dart';
|
2020-05-05 10:30:24 +02:00
|
|
|
|
|
|
|
|
|
abstract class FirebaseController {
|
2020-05-13 15:58:59 +02:00
|
|
|
|
static final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
|
|
|
|
|
static final FlutterLocalNotificationsPlugin
|
|
|
|
|
_flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
|
2020-05-05 10:30:24 +02:00
|
|
|
|
static BuildContext context;
|
2020-05-07 11:13:54 +02:00
|
|
|
|
static const String CHANNEL_ID = 'fluffychat_push';
|
|
|
|
|
static const String CHANNEL_NAME = 'FluffyChat push channel';
|
|
|
|
|
static const String CHANNEL_DESCRIPTION = 'Push notifications for FluffyChat';
|
2020-09-17 08:34:36 +02:00
|
|
|
|
static const String APP_ID = 'chat.fluffy.fluffychat.experimental';
|
2020-05-13 11:00:12 +02:00
|
|
|
|
static const String GATEWAY_URL = 'https://janian.de:7023/';
|
|
|
|
|
static const String PUSHER_FORMAT = 'event_id_only';
|
2020-05-05 10:30:24 +02:00
|
|
|
|
|
2020-07-21 10:56:26 +02:00
|
|
|
|
static Future<void> setupFirebase(
|
|
|
|
|
MatrixState matrix, String clientName) async {
|
|
|
|
|
final client = matrix.client;
|
2020-05-05 10:30:24 +02:00
|
|
|
|
if (Platform.isIOS) iOS_Permission();
|
|
|
|
|
|
|
|
|
|
String token;
|
|
|
|
|
try {
|
|
|
|
|
token = await _firebaseMessaging.getToken();
|
|
|
|
|
} catch (_) {
|
|
|
|
|
token = null;
|
|
|
|
|
}
|
|
|
|
|
if (token?.isEmpty ?? true) {
|
2020-07-21 10:56:26 +02:00
|
|
|
|
final storeItem =
|
|
|
|
|
await matrix.store.getItem('chat.fluffy.show_no_google');
|
|
|
|
|
final configOptionMissing = storeItem == null || storeItem.isEmpty;
|
|
|
|
|
if (configOptionMissing || (!configOptionMissing && storeItem == '1')) {
|
|
|
|
|
BotToast.showText(
|
|
|
|
|
text: L10n.of(context).noGoogleServicesWarning,
|
|
|
|
|
duration: Duration(seconds: 15),
|
|
|
|
|
);
|
|
|
|
|
if (configOptionMissing) {
|
|
|
|
|
await matrix.store.setItem('chat.fluffy.show_no_google', '0');
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-05 10:30:24 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-16 12:54:43 +02:00
|
|
|
|
final pushers = await client.requestPushers();
|
2020-05-13 11:00:12 +02:00
|
|
|
|
final currentPushers = pushers.where((pusher) => pusher.pushkey == token);
|
|
|
|
|
if (currentPushers.length == 1 &&
|
|
|
|
|
currentPushers.first.kind == 'http' &&
|
|
|
|
|
currentPushers.first.appId == APP_ID &&
|
|
|
|
|
currentPushers.first.appDisplayName == clientName &&
|
|
|
|
|
currentPushers.first.deviceDisplayName == client.deviceName &&
|
|
|
|
|
currentPushers.first.lang == 'en' &&
|
2020-06-10 10:07:01 +02:00
|
|
|
|
currentPushers.first.data.url.toString() == GATEWAY_URL &&
|
2020-05-13 11:00:12 +02:00
|
|
|
|
currentPushers.first.data.format == PUSHER_FORMAT) {
|
2020-05-13 15:58:59 +02:00
|
|
|
|
debugPrint('[Push] Pusher already set');
|
2020-05-13 11:00:12 +02:00
|
|
|
|
} else {
|
|
|
|
|
if (currentPushers.isNotEmpty) {
|
|
|
|
|
for (final currentPusher in currentPushers) {
|
2020-06-10 10:07:01 +02:00
|
|
|
|
currentPusher.pushkey = token;
|
|
|
|
|
currentPusher.kind = 'null';
|
2020-08-16 12:54:43 +02:00
|
|
|
|
await client.setPusher(
|
2020-06-10 10:07:01 +02:00
|
|
|
|
currentPusher,
|
2020-05-13 11:00:12 +02:00
|
|
|
|
append: true,
|
|
|
|
|
);
|
2020-05-13 15:58:59 +02:00
|
|
|
|
debugPrint('[Push] Remove legacy pusher for this device');
|
2020-05-13 11:00:12 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-16 12:54:43 +02:00
|
|
|
|
await client.setPusher(
|
2020-06-10 10:07:01 +02:00
|
|
|
|
Pusher(
|
|
|
|
|
token,
|
|
|
|
|
APP_ID,
|
|
|
|
|
clientName,
|
|
|
|
|
client.deviceName,
|
|
|
|
|
'en',
|
|
|
|
|
PusherData(
|
|
|
|
|
url: Uri.parse(GATEWAY_URL),
|
|
|
|
|
format: PUSHER_FORMAT,
|
|
|
|
|
),
|
|
|
|
|
kind: 'http',
|
|
|
|
|
),
|
2020-05-13 11:00:12 +02:00
|
|
|
|
append: false,
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-05-05 10:30:24 +02:00
|
|
|
|
|
|
|
|
|
Function goToRoom = (dynamic message) async {
|
|
|
|
|
try {
|
|
|
|
|
String roomId;
|
|
|
|
|
if (message is String) {
|
|
|
|
|
roomId = message;
|
|
|
|
|
} else if (message is Map) {
|
2020-05-13 15:58:59 +02:00
|
|
|
|
roomId = (message['data'] ?? message)['room_id'];
|
2020-05-05 10:30:24 +02:00
|
|
|
|
}
|
2020-05-13 15:58:59 +02:00
|
|
|
|
if (roomId?.isEmpty ?? true) throw ('Bad roomId');
|
2020-05-05 10:30:24 +02:00
|
|
|
|
await Navigator.of(context).pushAndRemoveUntil(
|
|
|
|
|
AppRoute.defaultRoute(
|
|
|
|
|
context,
|
|
|
|
|
ChatView(roomId),
|
|
|
|
|
),
|
|
|
|
|
(r) => r.isFirst);
|
|
|
|
|
} catch (_) {
|
2020-05-13 15:58:59 +02:00
|
|
|
|
BotToast.showText(text: 'Failed to open chat...');
|
2020-05-05 10:30:24 +02:00
|
|
|
|
debugPrint(_);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
|
|
|
|
|
var initializationSettingsAndroid =
|
|
|
|
|
AndroidInitializationSettings('notifications_icon');
|
|
|
|
|
var initializationSettingsIOS =
|
|
|
|
|
IOSInitializationSettings(onDidReceiveLocalNotification: (i, a, b, c) {
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
var initializationSettings = InitializationSettings(
|
|
|
|
|
initializationSettingsAndroid, initializationSettingsIOS);
|
|
|
|
|
await _flutterLocalNotificationsPlugin.initialize(initializationSettings,
|
|
|
|
|
onSelectNotification: goToRoom);
|
|
|
|
|
|
|
|
|
|
_firebaseMessaging.configure(
|
2020-05-05 12:51:38 +02:00
|
|
|
|
onMessage: _onMessage,
|
2020-05-13 11:03:16 +02:00
|
|
|
|
onBackgroundMessage: _onMessage,
|
2020-05-05 10:30:24 +02:00
|
|
|
|
onResume: goToRoom,
|
2020-05-05 12:51:38 +02:00
|
|
|
|
onLaunch: goToRoom,
|
2020-05-05 10:30:24 +02:00
|
|
|
|
);
|
2020-05-13 15:58:59 +02:00
|
|
|
|
debugPrint('[Push] Firebase initialized');
|
2020-05-05 10:30:24 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-05 12:51:38 +02:00
|
|
|
|
static Future<dynamic> _onMessage(Map<String, dynamic> message) async {
|
|
|
|
|
try {
|
|
|
|
|
final data = message['data'] ?? message;
|
2020-05-13 15:58:59 +02:00
|
|
|
|
final String roomId = data['room_id'];
|
|
|
|
|
final String eventId = data['event_id'];
|
|
|
|
|
final int unread = json.decode(data['counts'])['unread'];
|
2020-05-05 12:51:38 +02:00
|
|
|
|
if ((roomId?.isEmpty ?? true) ||
|
|
|
|
|
(eventId?.isEmpty ?? true) ||
|
|
|
|
|
unread == 0) {
|
|
|
|
|
await _flutterLocalNotificationsPlugin.cancelAll();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (context != null && Matrix.of(context).activeRoomId == roomId) {
|
2020-09-17 08:34:36 +02:00
|
|
|
|
debugPrint('[Push] New clearing push');
|
2020-05-05 12:51:38 +02:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-09-17 08:34:36 +02:00
|
|
|
|
debugPrint('[Push] New message received');
|
2020-05-07 11:13:54 +02:00
|
|
|
|
final i18n =
|
|
|
|
|
context == null ? L10n(Platform.localeName) : L10n.of(context);
|
2020-05-05 12:51:38 +02:00
|
|
|
|
|
|
|
|
|
// Get the client
|
|
|
|
|
Client client;
|
2020-09-17 08:34:36 +02:00
|
|
|
|
var tempClient = false;
|
|
|
|
|
try {
|
2020-05-05 12:51:38 +02:00
|
|
|
|
client = Matrix.of(context).client;
|
2020-09-17 08:34:36 +02:00
|
|
|
|
} catch (_) {
|
|
|
|
|
client = null;
|
|
|
|
|
}
|
|
|
|
|
if (client == null) {
|
|
|
|
|
tempClient = true;
|
2020-05-13 15:58:59 +02:00
|
|
|
|
final platform = kIsWeb ? 'Web' : Platform.operatingSystem;
|
|
|
|
|
final clientName = 'FluffyChat $platform';
|
2020-08-12 11:30:31 +02:00
|
|
|
|
client = Client(clientName);
|
2020-05-20 09:10:13 +02:00
|
|
|
|
client.database = await getDatabase(client);
|
2020-05-13 15:58:59 +02:00
|
|
|
|
client.connect();
|
2020-09-17 08:34:36 +02:00
|
|
|
|
debugPrint('[Push] Use a temp client');
|
2020-05-05 12:51:38 +02:00
|
|
|
|
await client.onLoginStateChanged.stream
|
2020-05-05 15:34:44 +02:00
|
|
|
|
.firstWhere((l) => l == LoginState.logged)
|
|
|
|
|
.timeout(
|
|
|
|
|
Duration(seconds: 5),
|
|
|
|
|
);
|
2020-05-05 12:51:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the room
|
2020-05-13 15:58:59 +02:00
|
|
|
|
var room = client.getRoomById(roomId);
|
2020-05-05 12:51:38 +02:00
|
|
|
|
if (room == null) {
|
2020-09-17 08:34:36 +02:00
|
|
|
|
debugPrint('[Push] Wait for the room');
|
2020-05-05 15:34:44 +02:00
|
|
|
|
await client.onRoomUpdate.stream
|
|
|
|
|
.where((u) => u.id == roomId)
|
|
|
|
|
.first
|
|
|
|
|
.timeout(Duration(seconds: 5));
|
2020-09-17 08:34:36 +02:00
|
|
|
|
debugPrint('[Push] Room found');
|
2020-05-05 12:51:38 +02:00
|
|
|
|
room = client.getRoomById(roomId);
|
|
|
|
|
if (room == null) return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the event
|
2020-05-13 15:58:59 +02:00
|
|
|
|
var event = await client.database.getEventById(client.id, eventId, room);
|
2020-05-05 12:51:38 +02:00
|
|
|
|
if (event == null) {
|
2020-09-17 08:34:36 +02:00
|
|
|
|
debugPrint('[Push] Wait for the event');
|
2020-05-13 15:58:59 +02:00
|
|
|
|
final eventUpdate = await client.onEvent.stream
|
|
|
|
|
.where((u) => u.content['event_id'] == eventId)
|
2020-05-05 15:34:44 +02:00
|
|
|
|
.first
|
|
|
|
|
.timeout(Duration(seconds: 5));
|
2020-09-17 08:34:36 +02:00
|
|
|
|
debugPrint('[Push] Event found');
|
2020-05-05 12:51:38 +02:00
|
|
|
|
event = Event.fromJson(eventUpdate.content, room);
|
|
|
|
|
if (room == null) return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Count all unread events
|
2020-05-13 15:58:59 +02:00
|
|
|
|
var unreadEvents = 0;
|
2020-05-05 12:51:38 +02:00
|
|
|
|
client.rooms
|
|
|
|
|
.forEach((Room room) => unreadEvents += room.notificationCount);
|
|
|
|
|
|
|
|
|
|
// Calculate title
|
2020-05-13 15:58:59 +02:00
|
|
|
|
final title = unread > 1
|
2020-05-05 14:25:45 +02:00
|
|
|
|
? i18n.unreadMessagesInChats(
|
|
|
|
|
unreadEvents.toString(), unread.toString())
|
|
|
|
|
: i18n.unreadMessages(unreadEvents.toString());
|
2020-05-05 12:51:38 +02:00
|
|
|
|
|
|
|
|
|
// Calculate the body
|
2020-05-13 15:58:59 +02:00
|
|
|
|
final body = event.getLocalizedBody(
|
2020-05-05 14:55:19 +02:00
|
|
|
|
i18n,
|
|
|
|
|
withSenderNamePrefix: true,
|
|
|
|
|
hideReply: true,
|
|
|
|
|
);
|
2020-05-05 12:51:38 +02:00
|
|
|
|
|
|
|
|
|
// The person object for the android message style notification
|
|
|
|
|
final person = Person(
|
2020-05-05 14:55:19 +02:00
|
|
|
|
name: room.getLocalizedDisplayname(i18n),
|
2020-05-05 12:51:38 +02:00
|
|
|
|
icon: room.avatar == null
|
|
|
|
|
? null
|
2020-05-05 14:55:19 +02:00
|
|
|
|
: BitmapFilePathAndroidIcon(
|
|
|
|
|
await downloadAndSaveAvatar(
|
|
|
|
|
room.avatar,
|
|
|
|
|
client,
|
|
|
|
|
width: 126,
|
|
|
|
|
height: 126,
|
|
|
|
|
),
|
2020-05-05 12:51:38 +02:00
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Show notification
|
|
|
|
|
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
|
2020-05-07 11:13:54 +02:00
|
|
|
|
CHANNEL_ID, CHANNEL_NAME, CHANNEL_DESCRIPTION,
|
2020-05-05 12:51:38 +02:00
|
|
|
|
styleInformation: MessagingStyleInformation(
|
|
|
|
|
person,
|
|
|
|
|
conversationTitle: title,
|
|
|
|
|
messages: [
|
|
|
|
|
Message(
|
|
|
|
|
body,
|
2020-06-10 10:07:01 +02:00
|
|
|
|
event.originServerTs,
|
2020-05-05 12:51:38 +02:00
|
|
|
|
person,
|
|
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
importance: Importance.Max,
|
|
|
|
|
priority: Priority.High,
|
2020-05-05 14:25:45 +02:00
|
|
|
|
ticker: i18n.newMessageInFluffyChat);
|
2020-05-05 12:51:38 +02:00
|
|
|
|
var iOSPlatformChannelSpecifics = IOSNotificationDetails();
|
|
|
|
|
var platformChannelSpecifics = NotificationDetails(
|
|
|
|
|
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
|
2020-05-05 14:55:19 +02:00
|
|
|
|
await _flutterLocalNotificationsPlugin.show(
|
|
|
|
|
0, room.getLocalizedDisplayname(i18n), body, platformChannelSpecifics,
|
2020-05-05 12:51:38 +02:00
|
|
|
|
payload: roomId);
|
2020-09-17 08:34:36 +02:00
|
|
|
|
|
|
|
|
|
if (tempClient) {
|
|
|
|
|
await client.dispose();
|
|
|
|
|
client = null;
|
|
|
|
|
debugPrint('[Push] Temp client disposed');
|
|
|
|
|
}
|
2020-05-05 12:51:38 +02:00
|
|
|
|
} catch (exception) {
|
2020-05-13 15:58:59 +02:00
|
|
|
|
debugPrint('[Push] Error while processing notification: ' +
|
2020-05-05 12:51:38 +02:00
|
|
|
|
exception.toString());
|
2020-05-05 15:34:44 +02:00
|
|
|
|
await _showDefaultNotification(message);
|
2020-05-05 12:51:38 +02:00
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-05 15:34:44 +02:00
|
|
|
|
static Future<dynamic> _showDefaultNotification(
|
|
|
|
|
Map<String, dynamic> message) async {
|
|
|
|
|
try {
|
2020-05-13 15:58:59 +02:00
|
|
|
|
var flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
|
2020-05-05 15:34:44 +02:00
|
|
|
|
// Init notifications framework
|
|
|
|
|
var initializationSettingsAndroid =
|
|
|
|
|
AndroidInitializationSettings('notifications_icon');
|
|
|
|
|
var initializationSettingsIOS = IOSInitializationSettings();
|
|
|
|
|
var initializationSettings = InitializationSettings(
|
|
|
|
|
initializationSettingsAndroid, initializationSettingsIOS);
|
|
|
|
|
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
|
2020-05-07 11:13:54 +02:00
|
|
|
|
final l10n = L10n(Platform.localeName);
|
2020-05-05 15:34:44 +02:00
|
|
|
|
|
|
|
|
|
// Notification data and matrix data
|
|
|
|
|
Map<dynamic, dynamic> data = message['data'] ?? message;
|
2020-05-13 15:58:59 +02:00
|
|
|
|
String eventID = data['event_id'];
|
|
|
|
|
String roomID = data['room_id'];
|
|
|
|
|
final int unread = data.containsKey('counts')
|
|
|
|
|
? json.decode(data['counts'])['unread']
|
2020-05-05 15:34:44 +02:00
|
|
|
|
: 1;
|
|
|
|
|
await flutterLocalNotificationsPlugin.cancelAll();
|
|
|
|
|
if (unread == 0 || roomID == null || eventID == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Display notification
|
|
|
|
|
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
|
2020-05-07 11:13:54 +02:00
|
|
|
|
CHANNEL_ID, CHANNEL_NAME, CHANNEL_DESCRIPTION,
|
|
|
|
|
importance: Importance.Max, priority: Priority.High);
|
2020-05-05 15:34:44 +02:00
|
|
|
|
var iOSPlatformChannelSpecifics = IOSNotificationDetails();
|
|
|
|
|
var platformChannelSpecifics = NotificationDetails(
|
|
|
|
|
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
|
2020-05-13 15:58:59 +02:00
|
|
|
|
final title = l10n.unreadChats(unread.toString());
|
2020-05-05 15:34:44 +02:00
|
|
|
|
await flutterLocalNotificationsPlugin.show(
|
2020-05-07 11:13:54 +02:00
|
|
|
|
1, title, l10n.openAppToReadMessages, platformChannelSpecifics,
|
2020-05-05 15:34:44 +02:00
|
|
|
|
payload: roomID);
|
|
|
|
|
} catch (exception) {
|
2020-05-13 15:58:59 +02:00
|
|
|
|
debugPrint('[Push] Error while processing background notification: ' +
|
2020-05-05 15:34:44 +02:00
|
|
|
|
exception.toString());
|
|
|
|
|
}
|
|
|
|
|
return Future<void>.value();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-05 10:30:24 +02:00
|
|
|
|
static Future<String> downloadAndSaveAvatar(Uri content, Client client,
|
|
|
|
|
{int width, int height}) async {
|
2020-05-13 15:58:59 +02:00
|
|
|
|
final thumbnail = width == null && height == null ? false : true;
|
|
|
|
|
final tempDirectory = (await getTemporaryDirectory()).path;
|
|
|
|
|
final prefix = thumbnail ? 'thumbnail' : '';
|
|
|
|
|
var file =
|
2020-05-05 10:30:24 +02:00
|
|
|
|
File('$tempDirectory/${prefix}_${content.toString().split("/").last}');
|
|
|
|
|
|
|
|
|
|
if (!file.existsSync()) {
|
|
|
|
|
final url = thumbnail
|
|
|
|
|
? content.getThumbnail(client, width: width, height: height)
|
|
|
|
|
: content.getDownloadLink(client);
|
|
|
|
|
var request = await HttpClient().getUrl(Uri.parse(url));
|
|
|
|
|
var response = await request.close();
|
|
|
|
|
var bytes = await consolidateHttpClientResponseBytes(response);
|
|
|
|
|
await file.writeAsBytes(bytes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return file.path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void iOS_Permission() {
|
|
|
|
|
_firebaseMessaging.requestNotificationPermissions(
|
|
|
|
|
IosNotificationSettings(sound: true, badge: true, alert: true));
|
|
|
|
|
_firebaseMessaging.onIosSettingsRegistered
|
|
|
|
|
.listen((IosNotificationSettings settings) {
|
2020-05-13 15:58:59 +02:00
|
|
|
|
debugPrint('Settings registered: $settings');
|
2020-05-05 10:30:24 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|