init static method
Future<void>
init(
{ - required dynamic onTapTerminatedNotification({
- String payload,
}),
- required dynamic onTapNotification({
- String payload,
}),
- required dynamic onNotification({
- String payload,
}),
})
Implementation
static Future<void> init(
{required Function({String payload}) onTapTerminatedNotification,
required Function({String payload}) onTapNotification,
required Function({String payload}) onNotification}) async {
FirebaseMessaging.instance.requestPermission();
// Todo when app is terminated
FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage? message) {
if (message != null) {
var data = json.encode(message.data);
onTapTerminatedNotification(payload: data);
}
});
// when app in foreground
FirebaseMessaging.onMessage.listen(
(RemoteMessage message) {
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
if (notification != null || android != null) {
onNotification(payload: json.encode(message.data));
if (_foregroundNotificationEnable) {
LocalNotificationApi.showNotification(
id: notification.hashCode,
title: "${notification?.title}",
body: "${notification?.body}",
payload: json.encode(message.data));
}
foregroundEnable(true);
}
},
);
// when app is on background
FirebaseMessaging.onBackgroundMessage(backgroundHandler);
// when app is active onTap notification
FirebaseMessaging.onMessageOpenedApp.listen(
(RemoteMessage message) {
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
if (notification != null && android != null) {
onTapNotification(payload: json.encode(message.data));
}
if (notification != null && Platform.isIOS) {
var data = json.encode(message.data);
onTapNotification(payload: data);
}
},
);
}