initialize method
Future<void>
initialize(
- FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin
)
Implementation
Future<void> initialize(
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async {
var androidInitialize =
const AndroidInitializationSettings('notification_icon');
var iOSInitialize = const DarwinInitializationSettings();
var initializationsSettings =
InitializationSettings(android: androidInitialize, iOS: iOSInitialize);
if (Platform.isAndroid) {
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()!
.requestNotificationsPermission();
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()!
.requestNotificationsPermission();
if (Platform.isAndroid) {
// Permission.
bool result = await Permission.ignoreBatteryOptimizations.isGranted;
if (!result) {
await Permission.ignoreBatteryOptimizations.request();
// await openAppSettings();
}
}
} else {
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()!
.requestPermissions();
}
flutterLocalNotificationsPlugin.initialize(initializationsSettings,
onDidReceiveNotificationResponse: (NotificationResponse load) async {
try {
// NotificationBody payload;
if (load.payload!.isNotEmpty) {
// payload = NotificationBody.fromJson(jsonDecode(load.payload!));
}
} catch (_) {}
return;
});
await FirebaseMessaging.instance.subscribeToTopic("growlytics");
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
NotificationHelper.showNotification(
message, flutterLocalNotificationsPlugin, true);
await _trackNotificationAction(
data: convertNotification(message.data), action: Constants.received);
await _trackNotificationAction(
data: convertNotification(message.data), action: Constants.click);
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
try {
NotificationBody notificationBody = convertNotification(message.data);
//await _trackNotificationAction(data: notificationBody, action: Constants.received);
await _trackNotificationAction(
data: notificationBody, action: Constants.click);
} catch (_) {
if (kDebugMode) {
print(_);
}
}
});
}