Skip to content

Commit

Permalink
Merge pull request #36 from Shreemanarjun/master
Browse files Browse the repository at this point in the history
✨ Added callback before notification display handler,onDidReceiveBack…
  • Loading branch information
mu-dawood authored Oct 9, 2024
2 parents afdb170 + f1fadd6 commit 424c3e8
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 13 deletions.
8 changes: 8 additions & 0 deletions fcm_config/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## [3.6.0]

* Added call back for notification details for android,darwin(IOS,MACOS),Linux
* Added onDidReceiveBackgroundNotificationResponse for callback,which willbe fired when the user
selects a notification or notification action that should show the
application/user interface


## [3.5.3]

* Update dependencies
Expand Down
7 changes: 7 additions & 0 deletions fcm_config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
* Notification is an object
# Setup

```yaml
fcm_config:
git:
url: https://github.com/Shreemanarjun/fcm_config.git
path: fcm_config
```
## Android
> - Follow steps here https://firebase.flutter.dev/docs/installation/android
Expand Down
2 changes: 1 addition & 1 deletion fcm_config/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, this.locale}) : super(key: key);

@override
_MyHomePageState createState() => _MyHomePageState();
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
Expand Down
31 changes: 31 additions & 0 deletions fcm_config/lib/src/fcm_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ class FCMConfig extends FCMConfigInterface {
/// Not work on the web
BackgroundMessageHandler? onBackgroundMessage,

///
/// The [onDidReceiveNotificationResponse] callback is fired when the user
/// selects a notification or notification action that should show the
/// application/user interface.
/// application was running. To handle when a notification launched an
/// application, use [getNotificationAppLaunchDetails]. For notification
/// actions that don't show the application/user interface, the
/// [onDidReceiveBackgroundNotificationResponse] callback is invoked on
/// a background isolate. Functions passed to the
/// [onDidReceiveBackgroundNotificationResponse]
/// callback need to be annotated with the `@pragma('vm:entry-point')`
/// annotation to ensure they are not stripped out by the Dart compiler.
void Function(NotificationResponse)?
onDidReceiveBackgroundNotificationResponse,

/// Drawable icon works only in foreground
String defaultAndroidForegroundIcon = '@mipmap/ic_launcher',

Expand Down Expand Up @@ -104,6 +119,17 @@ class FCMConfig extends FCMConfigInterface {

/// default action name for linux
String linuxActionName = 'fcm_config',

//Callbacks for Notification
///Android
final AndroidNotificationDetailsCallback?
androidNotificationDetailsCallback,

///IOS,MACOS
final DarwinNotificationDetailsCallback? darwinNotificationDetailsCallback,

///Linux
final LinuxNotificationDetailsCallback linuxNotificationDetailsCallback,
}) async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(name: name, options: options);
Expand Down Expand Up @@ -138,6 +164,11 @@ class FCMConfig extends FCMConfigInterface {
onRemoteMessage: onMessage,
tapSink: _onTapController.sink,
linuxActionName: linuxActionName,
onDidReceiveBackgroundNotificationResponse:
onDidReceiveBackgroundNotificationResponse,
androidNotificationDetailsCallback: androidNotificationDetailsCallback,
darwinNotificationDetailsCallback: darwinNotificationDetailsCallback,
linuxNotificationDetailsCallback: linuxNotificationDetailsCallback,
);

await _localeNotification!.init();
Expand Down
41 changes: 38 additions & 3 deletions fcm_config/lib/src/fcm_config_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,31 @@ import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart'
show
DarwinNotificationDetails,
AndroidNotificationChannel,
AndroidNotificationDetails,
AndroidNotificationChannel;
DarwinNotificationDetails,
LinuxNotificationDetails;

import 'details.dart';

typedef AndroidNotificationDetailsCallback
= Future<AndroidNotificationDetails?>? Function(
AndroidNotificationDetails androidNotificationDetails,
RemoteMessage remoteMessage,
)?;

typedef DarwinNotificationDetailsCallback = Future<DarwinNotificationDetails?>?
Function(
DarwinNotificationDetails darwinNotificationDetails,
RemoteMessage remoteMessage,
)?;

typedef LinuxNotificationDetailsCallback = Future<LinuxNotificationDetails?>?
Function(
LinuxNotificationDetails darwinNotificationDetails,
RemoteMessage remoteMessage,
)?;

abstract class FCMConfigInterface {
Future<RemoteMessage?> getInitialMessage();
Stream<RemoteMessage> get onMessage;
Expand Down Expand Up @@ -92,7 +111,23 @@ abstract class LocaleNotificationInterface {
DarwinNotificationDetails? macOS,
});

Future displayNotificationFrom(RemoteMessage message);
Future displayNotificationFrom(
RemoteMessage message,

/// callback for mutate [AndroidNotificationDetails]
/// which will get android notification details using [_getAndroidDetails] from [remoteMessage]
/// callback for mutate [AndroidNotificationDetails]
/// which will get android notification details using [_getAndroidDetails] from [remoteMessage]
AndroidNotificationDetailsCallback? onAndroidNotification,

/// callback for mutate [DarwinNotificationDetails]
/// which will get [Darwin] notification details using [_getDarwinDetails] from [remoteMessage]
DarwinNotificationDetailsCallback? onIosNotification,

/// callback for mutate [LinuxNotificationDetails]
/// which will get [Linux] notification details using [_getLinuxDetails] from [remoteMessage]
LinuxNotificationDetailsCallback? onLinuxNotification,
);

Future close();
}
81 changes: 75 additions & 6 deletions fcm_config/lib/src/io_notifications_Manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,32 @@ class NotificationManager implements LocaleNotificationInterface {
/// ios notification sound
final bool iosPresentSound;
final String linuxActionName;

///
/// The [onDidReceiveNotificationResponse] callback is fired when the user
/// selects a notification or notification action that should show the
/// application/user interface.
/// application was running. To handle when a notification launched an
/// application, use [getNotificationAppLaunchDetails]. For notification
/// actions that don't show the application/user interface, the
/// [onDidReceiveBackgroundNotificationResponse] callback is invoked on
/// a background isolate. Functions passed to the
/// [onDidReceiveBackgroundNotificationResponse]
/// callback need to be annotated with the `@pragma('vm:entry-point')`
/// annotation to ensure they are not stripped out by the Dart compiler.
void Function(NotificationResponse)?
onDidReceiveBackgroundNotificationResponse;

//Callbacks for Notification
///Android
final AndroidNotificationDetailsCallback? androidNotificationDetailsCallback;

///IOS,MACOS
final DarwinNotificationDetailsCallback? darwinNotificationDetailsCallback;

///Linux
final LinuxNotificationDetailsCallback? linuxNotificationDetailsCallback;

NotificationManager({
required this.androidNotificationChannel,
required this.appAndroidIcon,
Expand All @@ -49,6 +75,10 @@ class NotificationManager implements LocaleNotificationInterface {
required this.iosPresentSound,
required this.iosPresentAlert,
required this.linuxActionName,
this.onDidReceiveBackgroundNotificationResponse,
this.androidNotificationDetailsCallback,
this.darwinNotificationDetailsCallback,
this.linuxNotificationDetailsCallback,
});

@override
Expand Down Expand Up @@ -83,14 +113,21 @@ class NotificationManager implements LocaleNotificationInterface {
await _localeNotification.initialize(
initializationSettings,
onDidReceiveNotificationResponse: _onPayLoad,
onDidReceiveBackgroundNotificationResponse:
onDidReceiveBackgroundNotificationResponse,
);
await _remoteSubscription?.cancel();
//Listen to messages

_remoteSubscription = onRemoteMessage.listen((notification) {
if (notification.notification != null &&
displayInForeground(notification)) {
displayNotificationFrom(notification);
displayNotificationFrom(
notification,
androidNotificationDetailsCallback,
darwinNotificationDetailsCallback,
linuxNotificationDetailsCallback,
);
}
});
}
Expand Down Expand Up @@ -262,6 +299,13 @@ class NotificationManager implements LocaleNotificationInterface {
channelShowBadge:
android?.channelShowBadge ?? androidNotificationChannel.showBadge,
visibility: android?.visibility ?? _getVisibility(notification),
actions: android?.actions,
audioAttributesUsage:
android?.audioAttributesUsage ?? AudioAttributesUsage.notification,
chronometerCountDown: android?.chronometerCountDown ?? false,
colorized: android?.colorized ?? false,
number: android?.number,
when: android?.when,
);
}

Expand Down Expand Up @@ -324,7 +368,12 @@ class NotificationManager implements LocaleNotificationInterface {
}

@override
Future displayNotificationFrom(RemoteMessage message) async {
Future displayNotificationFrom(
RemoteMessage message,
AndroidNotificationDetailsCallback? onAndroidNotification,
DarwinNotificationDetailsCallback? onIosNotification,
LinuxNotificationDetailsCallback? onLinuxNotification,
) async {
if (message.notification == null) return;

String? largeIconPath;
Expand All @@ -343,11 +392,31 @@ class NotificationManager implements LocaleNotificationInterface {
hideExpandedLargeIcon: true,
);
}
late AndroidNotificationDetails android;
if (onAndroidNotification != null) {
android = (await onAndroidNotification(
await _getAndroidDetails(message: message), message)) ??
await _getAndroidDetails(message: message);
} else {
android = await _getAndroidDetails(message: message);
}
late DarwinNotificationDetails darwin;
if (onIosNotification != null) {
darwin = await onIosNotification(
await _getDarwinDetails(message: message), message) ??
await _getDarwinDetails(message: message);
} else {
darwin = await _getDarwinDetails(message: message);
}
late LinuxNotificationDetails linux;
if (onLinuxNotification != null) {
linux = await onLinuxNotification(
await _getLinuxDetails(message: message), message) ??
await _getLinuxDetails(message: message);
} else {
linux = await _getLinuxDetails(message: message);
}

var android = await _getAndroidDetails(message: message);
var darwin = await _getDarwinDetails(message: message);

var linux = await _getLinuxDetails(message: message);
var details = NotificationDetails(
android: android,
iOS: darwin,
Expand Down
6 changes: 5 additions & 1 deletion fcm_config/lib/src/web_notification_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ class NotificationManager implements LocaleNotificationInterface {
}

@override
Future displayNotificationFrom(RemoteMessage message) {
Future displayNotificationFrom(
RemoteMessage message,
AndroidNotificationDetailsCallback? onAndroidNotification,
DarwinNotificationDetailsCallback? onIosNotification,
LinuxNotificationDetailsCallback? onLinuxNotification) {
return displayNotification(
id: int.tryParse(message.messageId ?? '') ?? message.hashCode,
title: message.notification?.title,
Expand Down
4 changes: 2 additions & 2 deletions fcm_config/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: fcm_config
description: This flutter package is made to merge between firebase_messaging package and flutter_local_notifications
version: 3.5.3
version: 3.6.1

homepage: https://github.com/mo-ah-dawood/fcm_config/tree/master/fcm_config

environment:
sdk: ">=2.17.0 <4.0.0"
sdk: ">=3.0.0 <4.0.0"
flutter: ">=1.17.0"

dependencies:
Expand Down

0 comments on commit 424c3e8

Please sign in to comment.