💬Local Notifications
Aqui vamos mostrar como tratar as notificações que chegam no nosso app, esteja ele fechado, "minimizado" ou já aberto.
class NotificationService {
static final FirebaseMessaging _messaging = FirebaseMessaging.instance;
final NavigationService navigationService = GetIt.I.get<NavigationService>();
final _localNotificationsPlugin = FlutterLocalNotificationsPlugin();
static Future<void> _requestPermission() async {
if (Platform.isAndroid) return;
await _messaging.requestPermission();
await _messaging.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
}
Future setup() async {
await _requestPermission();
await initialize();
await _configureAndroidChannel();
await _openInitialScreenFromMessage();
}
Future<void> initialize() async {
const androidSetting =
AndroidInitializationSettings('@mipmap/launcher_icon');
const iosSetting = DarwinInitializationSettings();
const initSettings =
InitializationSettings(android: androidSetting, iOS: iosSetting);
await _localNotificationsPlugin
.initialize(initSettings)
.then((_) async {})
.catchError((Object error) {
log('Error: $error');
});
}
Future<void> _configureAndroidChannel() async {
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'BreakingCodeChannel',
'High Importance Notifications',
description: 'This channel is used for important notifications.',
importance: Importance.max,
);
await _localNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
}
invokeLocalNotification(RemoteMessage remoteMessage) async {
RemoteNotification? notification = remoteMessage.notification;
AndroidNotification? android = remoteMessage.notification?.android;
if (notification != null && android != null) {
await _localNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
'BreakingCodeChannel',
'High Importance Notifications',
channelDescription:
'This channel is used for important notifications.',
icon: android.smallIcon,
),
),
payload: jsonEncode(remoteMessage.data),
);
}
}
}
Last updated