This commit is contained in:
2021-09-11 08:27:27 +06:30
parent 7e9ea1b867
commit a4d3777e8a
55 changed files with 929 additions and 382 deletions

View File

@@ -1,57 +1,53 @@
import 'dart:io';
import 'package:fcs/data/services/messaging_service.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:logging/logging.dart';
final msgLog = Logger('backgroundMessageHandler');
Future<dynamic> backgroundMessageHandler(Map<String, dynamic> message) async {
if (message.containsKey('data')) {
// Handle data message
final dynamic data = message['data'];
msgLog.info("background onMessage: $message");
}
if (message.containsKey('notification')) {
// Handle notification message
final dynamic notification = message['notification'];
}
Future<void> backgroundMessageHandler(RemoteMessage message) async {
await Firebase.initializeApp();
msgLog.info("background onMessage: $message");
}
class MessagingFCM {
final log = Logger('MessagingFCM');
FirebaseMessaging _firebaseMessaging;
late FirebaseMessaging _firebaseMessaging;
MessagingFCM(OnNotify onMessage,
{OnNotify onLaunch, OnNotify onResume, OnSetupComplete onSetupComplete}) {
_firebaseMessaging = FirebaseMessaging();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
log.info("onMessage: $message");
if (onMessage != null) _onNotify(message, onMessage);
},
onBackgroundMessage: Platform.isIOS ? null : backgroundMessageHandler,
onLaunch: (Map<String, dynamic> message) async {
log.info("onLaunch: $message");
if (onLaunch != null) _onNotify(message, onLaunch);
},
onResume: (Map<String, dynamic> message) async {
log.info("onResume: $message");
if (onResume != null) _onNotify(message, onResume);
},
{OnNotify? onLaunch,
OnNotify? onResume,
OnSetupComplete? onSetupComplete}) {
_firebaseMessaging = FirebaseMessaging.instance;
init(onMessage: onMessage, onSetupComplete: onSetupComplete);
}
init({OnNotify? onMessage, OnSetupComplete? onSetupComplete}) async {
NotificationSettings settings = await _firebaseMessaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
_firebaseMessaging
.requestNotificationPermissions(const IosNotificationSettings());
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings) {
log.info("Settings registered: $settings");
});
_firebaseMessaging.getToken().then((String token) {
if (onSetupComplete != null) onSetupComplete(token);
log.info("Messaging Token:$token");
print('User granted permission: ${settings.authorizationStatus}');
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
log.info("onMessage: $message");
if (onMessage != null) _onNotify(message.data, onMessage);
if (message.notification != null) {
print('Message also contained a notification: ${message.notification}');
}
});
String? token = await _firebaseMessaging.getToken();
if (onSetupComplete != null && token != null) onSetupComplete(token);
log.info("Messaging Token:$token");
}
Future<void> subscribeToTopic(String topic) {