151 lines
4.7 KiB
Dart
151 lines
4.7 KiB
Dart
import 'dart:convert';
|
|
import 'dart:developer';
|
|
import 'dart:io';
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:firebase_core/firebase_core.dart';
|
|
import 'package:flutter_app_badger/flutter_app_badger.dart';
|
|
import 'notification_util.dart';
|
|
import 'package:get/get.dart';
|
|
import 'extensions/custom_extensions.dart';
|
|
|
|
class FcmNotification {
|
|
static FcmNotification? _instance;
|
|
|
|
static FcmNotification getInstance() {
|
|
_instance ??= FcmNotification();
|
|
return _instance!;
|
|
}
|
|
|
|
init() async {
|
|
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
|
|
await Firebase.initializeApp();
|
|
_firebaseCloudMessagingListeners();
|
|
}
|
|
|
|
Future<void> _firebaseCloudMessagingListeners() async {
|
|
// ios permission
|
|
if (Platform.isIOS) iOSPermission();
|
|
await FirebaseMessaging.instance
|
|
.setForegroundNotificationPresentationOptions(
|
|
alert: false, // Required to display a heads up notification
|
|
badge: true,
|
|
sound: true,
|
|
);
|
|
//foreground message
|
|
FirebaseMessaging.onMessage.listen((RemoteMessage remoteMessage) async {
|
|
// RemoteNotification notificationData = remoteMessage.notification;
|
|
|
|
if (kDebugMode) log("new message received: ${remoteMessage.data}");
|
|
|
|
incrementNotificationCount();
|
|
|
|
// final data = Notifications.fromJson(remoteMessage.data);
|
|
|
|
// if (Get.currentRoute == AppRoute.individualChatScreen &&
|
|
// data.contentType == notificationContentTypeMessage) return;
|
|
|
|
if (Platform.isAndroid) {
|
|
NotificationUtils.showDefaultNotification(
|
|
title: "${remoteMessage.notification?.title}",
|
|
body: '${remoteMessage.notification?.body}',
|
|
payload: remoteMessage.data,
|
|
);
|
|
}
|
|
});
|
|
// Also handle any interaction when the app is in the background via a
|
|
// Stream listener
|
|
FirebaseMessaging.onMessageOpenedApp.listen(
|
|
(RemoteMessage? remoteMessage) async {
|
|
if (remoteMessage != null) {
|
|
handleAppNotification(remoteMessage.data);
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
Future<void> iOSPermission() async {
|
|
NotificationSettings settings =
|
|
await FirebaseMessaging.instance.requestPermission(
|
|
alert: true,
|
|
announcement: false,
|
|
badge: true,
|
|
carPlay: false,
|
|
criticalAlert: false,
|
|
provisional: false,
|
|
sound: true,
|
|
);
|
|
debugPrint('User granted permission: ${settings.authorizationStatus}');
|
|
}
|
|
|
|
void selectNotification(String? payload) async {
|
|
debugPrint('selected notification payload: $payload');
|
|
|
|
if (payload != null) {
|
|
var messageData = jsonDecode(payload);
|
|
handleAppNotification(messageData);
|
|
}
|
|
}
|
|
}
|
|
|
|
handleAppNotification(Map<String, dynamic> payload) {
|
|
if (kDebugMode) {
|
|
log("Handle App Notification: $payload");
|
|
}
|
|
// onNotificationTap(Notifications.fromJson(payload));
|
|
}
|
|
|
|
// onNotificationTap(Notifications notification) {
|
|
// if (TradePersonNotificationContentTypes.toOpenJobDetailPage
|
|
// .contains(notification.contentType)) {
|
|
// _gotoJobDetailScreen(notification.content);
|
|
// } else if (ClientNotificationContentTypes.toOpenJobDetailPage
|
|
// .contains(notification.contentType)) {
|
|
// if (notification.content == null) {
|
|
// debugPrint("content null");
|
|
// return;
|
|
// }
|
|
//
|
|
// final id = JobDetail.fromJson(notification.content!).id;
|
|
// if (id.isNullOrEmpty()) {
|
|
// debugPrint("job id missing");
|
|
// return;
|
|
// }
|
|
// AppRoute.toJobDetailScreen(args: JobDetailScreenArgs(jobId: id));
|
|
// } else if (notification.contentType == notificationContentTypeMessage) {
|
|
// if (notification.content == null) {
|
|
// debugPrint("content null");
|
|
// return;
|
|
// }
|
|
// final message = ChatModel.fromJson(notification.content);
|
|
// AppRoute.toIndividualChatScreen(
|
|
// arguments: IndividualChatScreenArgs(
|
|
// otherUserId: message.sentBy!.id!,
|
|
// name: message.sentBy!.fullname ?? emptyString,
|
|
// profilePicPath: message.sentBy!.profilePic ?? emptyString,
|
|
// otherUserType: message.sentBy!.userType!,
|
|
// onLastMessageUpdate: (ChatModel message) {},
|
|
// ),
|
|
// );
|
|
// }
|
|
// }
|
|
|
|
incrementNotificationCount() async {
|
|
//Todo
|
|
// try {
|
|
// final appProviderController = AppProviderController.instance;
|
|
// appProviderController.notiCountUp();
|
|
// if (await FlutterAppBadger.isAppBadgeSupported()) {
|
|
// FlutterAppBadger.updateBadgeCount(
|
|
// appProviderController.notificationCount());
|
|
// }
|
|
// } catch (e) {
|
|
// debugPrint(e.toString());
|
|
// }
|
|
}
|
|
|
|
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
|
debugPrint("Handling a background message: ${message.data}, ${message.ttl}");
|
|
incrementNotificationCount();
|
|
}
|