Instructions:
Enable Push notifications on each Platform, you can follow the instructions at the link:
-
Install package on it's latest version
-
Ensure the following dependencies constraints
- permission_handler: ^10.0.2
-
iOS
- Enable Push Notifications Capability under XCode
- Enable Background Modes
- Remote Notifications
- Background fetch
- Upload APN to Firebase Console
-
Android
-
Web
- Configure VAPID key on
Config - Create background service worker in root of project
- firebase-messaging-sw.js
- Configure VAPID key on
-
Create Configuration
-
Example
lib/core/config/push_notifications_config.dartfinal FirePushNotificationsConfig pushNotificationsConfig = FirePushNotificationsConfig( registerDeviceTokenApiEndpoint: (params) { return Uri.parse(EnvironmentConfig.api_url + ApiEndpoints.devices()); }, removeDeviceTokenApiEndpoint: (params) { return Uri.parse(EnvironmentConfig.api_url + ApiEndpoints.devices()); }, removeDeviceRequestMapper: (request, info, params) { if (params?.containsKey("authToken") ?? false) { request.headers.addAll({ "Authorization": params!["authToken"], }); return request; } return request; }, );
-
-
Create a wrapper container to setup some config and have better control
-
Example
lib/core/presentation/containers/app_push_notifications_container.dartimport 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_gb_app_notifications/application/bloc/bloc.dart'; import 'package:flutter_gb_authentication_basic/flutter_gb_authentication_basic.dart'; import 'package:flutter_gb_fire_push_notifications/flutter_gb_fire_push_notifications.dart'; class AppPushNotificationsContainer extends StatelessWidget { final Widget child; const AppPushNotificationsContainer({ Key? key, required this.child, }) : super(key: key); @override Widget build(BuildContext context) { String? sessionToken; return FirePushNotificationsProvider( config: FirePushNotificationsBlocConfig( logEvents: true, paramBuilder: (event) { return event.maybeMap( removeToken: (value) { return { "authToken": sessionToken, }; }, orElse: () { return {}; }, ); }, ), child: AuthenticationBasicBlocListener( onLogout: (context, token) { sessionToken = token; context.read<FirePushNotificationsBloc>().removeToken(); }, child: Builder( builder: (context) { return PushNotificationListener( onEvent: (context, event) { event.mapOrNull( notificationReceived: (value) { context.read<AppNotificationBloc>().getNotifications(); context.read<AppNotificationBloc>().getUnreadCount(); }, ); }, child: child); }, ), ), ); } } -
Use that container to wrap your widget tree at your main widget tree, we recommend wrapping a widget that is behind an authentication guard
-
Libraries
- application/application
- application/bloc/bloc
- application/bloc/push_notifications/push_notifications_bloc
- config/bloc_config
- config/config
- config/global_configuration
- dependency_injection
- domain/domain
- domain/entities/device_info
- domain/entities/entities
- domain/entities/push_notification
- domain/entities/push_token_register_info
- domain/services/push_notifications_service
- domain/services/services
- flutter_gb_fire_push_notifications
- infrastructure/facades/device_info_facade_impl
- infrastructure/facades/facades
- infrastructure/facades/firebase_notifications_facade_impl
- infrastructure/infrastructure
- infrastructure/models/device_info_model
- infrastructure/models/global_config_model
- infrastructure/models/models
- infrastructure/models/push_notification_model
- infrastructure/models/push_token_register_info_model
- infrastructure/models/serializers
- infrastructure/services/push_notifications_service_impl
- infrastructure/services/services
- presentation/containers/containers
- presentation/containers/notifications_provider
- presentation/containers/push_notification_listener
- presentation/presentation
- utils/failures/failures
- utils/notification_logger
- utils/utils