initializeLocalNotifications function
Future<NotificationService>
initializeLocalNotifications(
- String? icon, {
- List<
NotificationAction> ? actionCategories, - bool requestPermissions = false,
- void onDidReceiveNotificationResponse(
- NotificationResponse
- void onDidReceiveBackgroundNotificationResponse(
- NotificationResponse
Initialises the local notifications plugin for all supported platforms.
Pass an optional icon drawable name (without prefix/extension).
Set requestPermissions to true to prompt the user for permission.
Returns a NotificationService with the resulting initialisation state.
Implementation
Future<NotificationService> initializeLocalNotifications(String? icon,
// if platform is windows we dont initialize notifications
{List<NotificationAction>? actionCategories,
bool requestPermissions = false,
void Function(NotificationResponse)? onDidReceiveNotificationResponse,
void Function(NotificationResponse)?
onDidReceiveBackgroundNotificationResponse}) async {
NotificationService notificationService = NotificationService(
initialized: false,
permissionGranted: false,
didNotificationLaunchApp: false,
launchPayload: null,
);
final NotificationAppLaunchDetails? notificationAppLaunchDetails =
(!kIsWeb && Platform.isLinux) || Platform.isWindows
? null
: await flutterLocalNotificationsPlugin
.getNotificationAppLaunchDetails();
// check for payload if app was launched via notification
if (notificationAppLaunchDetails?.didNotificationLaunchApp ?? false) {
notificationService.didNotificationLaunchApp = true;
notificationService.launchPayload =
notificationAppLaunchDetails!.notificationResponse?.payload;
// initialRoute = SecondPage.routeName;
}
var initializationSettingsAndroid = AndroidInitializationSettings(
icon != null ? '@mipmap/$icon' : '@mipmap/ic_launcher');
// Linux initialization settings
final LinuxInitializationSettings initializationSettingsLinux =
LinuxInitializationSettings(
defaultActionName: 'Open notification',
defaultIcon: AssetsLinuxIcon(
icon != null ? 'icons/$icon.png' : 'icons/app_icon.png'),
);
// macOS initialization settings
final List<DarwinNotificationCategory> darwinNotificationCategories =
<DarwinNotificationCategory>[];
final DarwinInitializationSettings initializationSettingsDarwin =
DarwinInitializationSettings(
requestAlertPermission: false,
requestBadgePermission: false,
requestSoundPermission: false,
// onDidReceiveLocalNotification:
// (int id, String? title, String? body, String? payload) async {
// didReceiveLocalNotificationStream.add(
// NotificationMessage(
// id: id,
// title: title ?? '',
// body: body ?? '',
// payload: payload,
// ),
// );
// },
notificationCategories: darwinNotificationCategories,
);
final InitializationSettings initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsDarwin,
macOS: initializationSettingsDarwin,
linux: initializationSettingsLinux,
);
notificationService.initialized = Platform.isWindows
? false
: await flutterLocalNotificationsPlugin.initialize(settings: initializationSettings,
onDidReceiveNotificationResponse: onDidReceiveNotificationResponse,
onDidReceiveBackgroundNotificationResponse:
onDidReceiveBackgroundNotificationResponse);
if (requestPermissions) {
switch (defaultTargetPlatform) {
case TargetPlatform.android:
notificationService.permissionGranted =
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.requestNotificationsPermission();
break;
case TargetPlatform.iOS:
notificationService.permissionGranted =
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(
alert: true,
badge: true,
sound: true,
);
case TargetPlatform.macOS:
notificationService.permissionGranted =
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(
alert: true,
badge: true,
sound: true,
);
break;
case TargetPlatform.linux:
notificationService.permissionGranted = true;
break;
default:
notificationService.permissionGranted = true;
break;
}
} else {
notificationService.permissionGranted = true;
}
return notificationService;
}