2020-09-10 02:13:22 +06:30
|
|
|
import 'dart:io';
|
|
|
|
|
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'package:fcs/data/services/messaging_service.dart';
|
2020-09-06 02:36:57 +06:30
|
|
|
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'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class MessagingFCM {
|
|
|
|
|
final log = Logger('MessagingFCM');
|
|
|
|
|
|
|
|
|
|
FirebaseMessaging _firebaseMessaging;
|
|
|
|
|
|
2020-09-20 05:34:49 +06:30
|
|
|
MessagingFCM(OnNotify onMessage,
|
|
|
|
|
{OnNotify onLaunch, OnNotify onResume, OnSetupComplete onSetupComplete}) {
|
2020-09-06 02:36:57 +06:30
|
|
|
_firebaseMessaging = FirebaseMessaging();
|
|
|
|
|
_firebaseMessaging.configure(
|
|
|
|
|
onMessage: (Map<String, dynamic> message) async {
|
|
|
|
|
log.info("onMessage: $message");
|
|
|
|
|
if (onMessage != null) _onNotify(message, onMessage);
|
|
|
|
|
},
|
2020-09-10 02:13:22 +06:30
|
|
|
onBackgroundMessage: Platform.isIOS ? null : backgroundMessageHandler,
|
2020-09-06 02:36:57 +06:30
|
|
|
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);
|
|
|
|
|
},
|
|
|
|
|
);
|
2020-09-10 02:13:22 +06:30
|
|
|
_firebaseMessaging
|
|
|
|
|
.requestNotificationPermissions(const IosNotificationSettings());
|
2020-09-06 02:36:57 +06:30
|
|
|
_firebaseMessaging.onIosSettingsRegistered
|
|
|
|
|
.listen((IosNotificationSettings settings) {
|
|
|
|
|
log.info("Settings registered: $settings");
|
|
|
|
|
});
|
|
|
|
|
_firebaseMessaging.getToken().then((String token) {
|
2020-09-20 05:34:49 +06:30
|
|
|
if (onSetupComplete != null) onSetupComplete(token);
|
2020-09-06 02:36:57 +06:30
|
|
|
log.info("Messaging Token:$token");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> subscribeToTopic(String topic) {
|
|
|
|
|
return _firebaseMessaging.subscribeToTopic(topic);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_onNotify(Map<String, dynamic> message, OnNotify onNotify) {
|
|
|
|
|
var data = message['data'] ?? message;
|
|
|
|
|
onNotify(Map<String, dynamic>.from(message));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> unsubscribeToTopic(String topic) {
|
|
|
|
|
return _firebaseMessaging.unsubscribeFromTopic(topic);
|
|
|
|
|
}
|
|
|
|
|
}
|