Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having too many local notifications after the initial notification #2523

Open
yigtkaya opened this issue Jan 28, 2025 · 0 comments
Open

Having too many local notifications after the initial notification #2523

yigtkaya opened this issue Jan 28, 2025 · 0 comments

Comments

@yigtkaya
Copy link

/// Azure Notification Hub implementation
final class AzureNotificationHandler implements NotificationHandler {
static AzureNotificationHandler? _instance;
static AzureNotificationHandler get instance => instance ??= AzureNotificationHandler.();

AzureNotificationHandler._();

final LocalNotificationHandler _localNotificationHandler = LocalNotificationHandler.instance;
static const String _allUsersTag = 'allUsers';

@OverRide
Future initialize() async {
try {
await AzureNotificationHub.instance.registerBackgroundMessageHandler(_azureBackgroundMessageHandler);
await AzureNotificationHub.instance.start();

  // Set up message handlers
  // AzureNotificationHub.instance.onMessage.listen(_handleMessage);
  AzureNotificationHub.instance.onMessageOpenedApp.listen(_handleMessageOpenedApp);
} catch (e) {
  debugPrint('Error initializing Azure Notification Hub: $e');
}

}

void _handleMessage(Map<String, dynamic> message) {
debugPrint('Received Azure message: $message');

_localNotificationHandler.showNotification(message);

}

void _handleMessageOpenedApp(Map<String, dynamic> message) {
debugPrint('App opened from Azure notification: $message');
// Handle app open from notification here
}

@OverRide
Future registerDevice() async {
try {
// First remove any existing tags to prevent duplicates
await unregisterDevice();

  // Then add the tag
  await AzureNotificationHub.instance.addTags(<String>[_allUsersTag]);
  debugPrint('Successfully registered device with Azure Notification Hub');
} catch (e) {
  debugPrint('Error registering Azure device: $e');
}

}

@OverRide
Future unregisterDevice() async {
try {
await AzureNotificationHub.instance.removeTags([_allUsersTag]);
debugPrint('Successfully unregistered device from Azure Notification Hub');
} catch (e) {
debugPrint('Error unregistering Azure device: $e');
}
}
}

/// Local notification handler for displaying notifications
final class LocalNotificationHandler {
static LocalNotificationHandler? _instance;
static LocalNotificationHandler get instance => instance ??= LocalNotificationHandler.();

LocalNotificationHandler._();

final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

Future initialize() async {
try {
const initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher');
const initializationSettingsIOS = DarwinInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestSoundPermission: true,
);
const initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS,
);

  await _flutterLocalNotificationsPlugin.initialize(
    initializationSettings,
    onDidReceiveNotificationResponse: _onLocalNotificationTapped,
  );

  await _requestIOSPermissions();
} catch (e) {
  debugPrint('Error initializing local notifications: $e');
}

}

Future _requestIOSPermissions() async {
await _flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation()
?.requestPermissions(
alert: true,
badge: true,
sound: true,
);
}

Future showNotification(Map<String, dynamic> message) async {
try {
const androidDetails = AndroidNotificationDetails(
'default_channel',
'Default Channel',
channelDescription: 'Default notification channel',
importance: Importance.max,
priority: Priority.high,
showWhen: true,
);

  const iosDetails = DarwinNotificationDetails(
    presentAlert: true,
    presentBadge: true,
    presentSound: true,
  );

  const notificationDetails = NotificationDetails(
    android: androidDetails,
    iOS: iosDetails,
  );

  final String title = message['aps']?['alert']?['title'] ?? // iOS format
      message['notification']?['title'] ?? // FCM notification payload
      message['title'] ?? // Direct data payload
      'New Notification';

  final String body = message['aps']?['alert']?['body'] ?? // iOS format
      message['notification']?['body'] ?? // FCM notification payload
      message['body'] ?? // Direct data payload
      '';

  await _flutterLocalNotificationsPlugin.show(
    DateTime.now().millisecond,
    title,
    body,
    notificationDetails,
  );
} catch (e) {
  debugPrint('Error showing local notification: $e');
}

}

void _onLocalNotificationTapped(NotificationResponse details) {
debugPrint('Local notification tapped: ${details.payload}');
// Handle notification tap here
}
}

/// Main notification service that orchestrates all handlers
final class NotificationService {
static NotificationService? _instance;
static NotificationService get instance => instance ??= NotificationService.();

NotificationService._();

final _azureHandler = AzureNotificationHandler.instance;
final _localHandler = LocalNotificationHandler.instance;

Future initialize() async {
// Initialize handlers in sequence to ensure proper order
await _localHandler.initialize();
// await _firebaseHandler.initialize();
await _azureHandler.initialize();

// Register Azure device after initialization
await _azureHandler.registerDevice();

}

Future unregisterDevice() async {
await Future.wait([
_firebaseHandler.unregisterDevice(),
_azureHandler.unregisterDevice(),
]);
}
}

@pragma('vm:entry-point')
Future _azureBackgroundMessageHandler(Map<String, dynamic> message) async {
log('Handling an Azure background message: $message');
// Add your Azure background message handling logic here
}

After I receive my function my local notifications burst out with tons of notifications why is this happening

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant