initializeNotificationService static method

Future<String?> initializeNotificationService(
  1. {void onPushBackgroundNotificationOpened(
    1. RemoteMessage remoteMessage
    )?,
  2. void onLocalForegroundNotificationOpened(
    1. NotificationResponse notificationResponse
    )?,
  3. void onLocalBackgroundNotificationOpened(
    1. NotificationResponse notificationResponse
    )?}
)

Implementation

static Future<String?> initializeNotificationService({
  void Function(RemoteMessage remoteMessage)?
      onPushBackgroundNotificationOpened,
  void Function(NotificationResponse notificationResponse)?
      onLocalForegroundNotificationOpened,
  void Function(NotificationResponse notificationResponse)?
      onLocalBackgroundNotificationOpened,
}) async {
  await requestPermission();

  const InitializationSettings initializationSettings =
      InitializationSettings(
    android: AndroidInitializationSettings(
      '@drawable/ic_notification',
    ),
    iOS: DarwinInitializationSettings(),
  );

  await flutterLocalNotificationsPlugin.initialize(
    initializationSettings,
    onDidReceiveNotificationResponse: onLocalForegroundNotificationOpened,
    onDidReceiveBackgroundNotificationResponse:
        onLocalForegroundNotificationOpened,
  );

  firebaseMessaging.setForegroundNotificationPresentationOptions(
    alert: true,
    badge: true,
    sound: true,
  );

  token = await firebaseMessaging.getToken();

  FirebaseMessaging.onMessage.listen(_onForegroundMessageHandler);
  FirebaseMessaging.onBackgroundMessage(_onBackgroundMessageHandler);
  FirebaseMessaging.onMessageOpenedApp
      .listen(onPushBackgroundNotificationOpened);

  return token;
}