remove all notifications dependencies

This commit is contained in:
Salvatore Giordano
2020-04-16 15:39:57 +02:00
parent f1908f2889
commit 167e1adca2
6 changed files with 100 additions and 40 deletions
+58 -21
View File
@@ -1,36 +1,58 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_apns/apns.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart'
hide Message;
import 'package:stream_chat_flutter/stream_chat_flutter.dart';
Future<void> _handleAndroidNotification(
Map<String, dynamic> notification,
) async {
void showLocalNotification(Message message, ChannelModel channel) async {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
final initializationSettingsAndroid =
AndroidInitializationSettings('launch_background');
final initializationSettingsIOS = IOSInitializationSettings();
final initializationSettings = InitializationSettings(
initializationSettingsAndroid,
initializationSettingsIOS,
);
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
flutterLocalNotificationsPlugin.show(
message.id.hashCode,
'${message.user.name} @ ${channel.name}',
message.text,
NotificationDetails(
AndroidNotificationDetails(
'message channel',
'Message channel',
'Channel used for showing messages',
priority: Priority.High,
importance: Importance.High,
),
IOSNotificationDetails(),
),
);
}
Future backgroundHandler(Map<String, dynamic> notification) async {
final messageId = notification['data']['message_id'];
print('messageId: ${messageId}');
final notificationData =
await NotificationService.getAndStoreMessage(notification);
await NotificationService.getAndStoreMessage(messageId);
final androidPlatformChannelSpecifics = AndroidNotificationDetails(
'Message notifications',
'Message notifications',
'Channel dedicated to message notifications',
importance: Importance.Max,
priority: Priority.High,
showLocalNotification(
notificationData.message,
notificationData.channel,
);
final androidNotificationOptions = AndroidNotificationOptions(
androidNotificationDetails: androidPlatformChannelSpecifics,
id: notificationData.message.id.hashCode,
title:
'CUSTOM ${notificationData.message.user.name} @ ${notificationData.channel.cid}',
body: notificationData.message.text,
);
await NotificationService.showNotification(androidNotificationOptions);
}
void main() async {
final client = Client(
's2dxdhpxd94g',
logLevel: Level.INFO,
androidNotificationHandler: _handleAndroidNotification,
showFakeNotification: Platform.isAndroid ? showLocalNotification : null,
);
await client.setUser(
@@ -38,6 +60,21 @@ void main() async {
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoidXNlcjEifQ.NGZPyPMx7KSVisJmh4tJhOIv7ZjCaMQpOh4gTINvCaU',
);
final connector = createPushConnector();
connector.configure(
onBackgroundMessage: backgroundHandler,
);
connector.requestNotificationPermissions();
connector.token.addListener(() {
if (connector.token.value != null) {
client.addDevice(
connector.token.value,
Platform.isAndroid ? 'firebase' : 'apn',
);
}
});
runApp(MyApp(client));
}
+2
View File
@@ -11,6 +11,8 @@ dependencies:
sdk: flutter
stream_chat_flutter:
path: ../
flutter_apns: ^1.1.0
flutter_local_notifications: ^1.4.1
dev_dependencies:
flutter_test:
+1 -1
View File
@@ -26,7 +26,7 @@ class ChannelsBloc extends StatefulWidget {
streamChatState = context.findAncestorStateOfType<ChannelsBlocState>();
if (streamChatState == null) {
throw Exception('You must have a ChannelsProvider widget as anchestor');
throw Exception('You must have a ChannelsBloc widget as anchestor');
}
return streamChatState;
+1
View File
@@ -4,6 +4,7 @@ import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:http_parser/http_parser.dart';
import 'package:image_picker/image_picker.dart';
import 'package:mime/mime.dart';
import 'package:stream_chat/stream_chat.dart';
+36 -16
View File
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
@@ -193,18 +192,41 @@ class StreamChatState extends State<StreamChat> with WidgetsBindingObserver {
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.paused) {
if (Platform.isAndroid) {
if (client.pushNotificationsEnabled) {
_newMessageSubscription =
client.on(EventType.messageNew).listen((event) {
client.androidNotificationHandler({
'data': {
'message_id': event.message.id,
},
});
});
}
_disconnectTimer = Timer(Duration(minutes: 1), () {
if (client.showFakeNotification != null) {
_newMessageSubscription = client
.on(EventType.messageNew)
.where((e) => e.user.id != user.id)
.listen((event) async {
var channel = client.state.channels[event.cid];
if (channel == null) {
channel = client.channel(
event.type,
id: event.cid.split(':')[1],
);
await channel.query();
}
client.showFakeNotification(
event.message,
ChannelModel(
id: channel.id,
createdAt: channel.createdAt,
extraData: channel.extraData,
type: channel.type,
members: channel.state.members,
memberCount: channel.memberCount,
frozen: channel.frozen,
cid: channel.cid,
deletedAt: channel.deletedAt,
config: channel.config,
createdBy: channel.createdBy,
updatedAt: channel.updatedAt,
lastMessageAt: channel.lastMessageAt,
),
);
});
_disconnectTimer = Timer(client.backgroundKeepAlive, () {
client.disconnect();
});
} else {
@@ -216,9 +238,7 @@ class StreamChatState extends State<StreamChat> with WidgetsBindingObserver {
_disconnectTimer.cancel();
} else {
if (client.wsConnectionStatus.value == ConnectionStatus.disconnected) {
if (client.pushNotificationsEnabled) {
NotificationService.handleIosMessageQueue(client);
}
NotificationService.handleIosMessageQueue(client);
client.connect();
}
}
+2 -2
View File
@@ -10,7 +10,6 @@ dependencies:
flutter:
sdk: flutter
photo_view: ^0.9.2
rxdart: ^0.23.1
jiffy: ^3.0.1
cached_network_image: ^2.1.0+1
flutter_markdown: ^0.3.4
@@ -20,7 +19,8 @@ dependencies:
file_picker: ^1.6.2
image_picker: ^0.6.5
flutter_keyboard_visibility: ^0.8.0
stream_chat: ^0.2.0-alpha
stream_chat:
path: ../stream_chat_dart
mime: ^0.9.6+3
visibility_detector: ^0.1.4
http_parser: ^3.1.4