Notification
index:
- Introduction
- Links
- Define functions for different platforms
- Set show the notification after pressing button
Introduction:
Flutter notification is prepared for desktop and mobile in flutterap local_notifier and flutter_local_notifications packages are added in pubspec.yaml dependencies.
Links:
https://flutterap.com/
https://flutterap.features.com/
https://docs.flutterap.com/
https://github.com/flutterap59/flutterap/
Define functions for different platforms
void _desktopNotif() async {
// Add in main method.
await localNotifier.setup(
appName: 'Flutterap',
// The parameter shortcutPolicy only works on Windows
shortcutPolicy: ShortcutPolicy.requireCreate,
);
LocalNotification notification =
LocalNotification(title: "Flutterap", body: "Hi, I am Flutterap:)");
notification.onShow = () {
if (kDebugMode) {
print('onShow ${notification.identifier}');
}
};
notification.onClose = (closeReason) {
// Only supported on windows, other platforms closeReason is always unknown.
switch (closeReason) {
case LocalNotificationCloseReason.userCanceled:
// do something
break;
case LocalNotificationCloseReason.timedOut:
// do something
break;
default:
}
//print('onClose ${_exampleNotification?.identifier} - $closeReason');
};
notification.onClick = () {
if (kDebugMode) {
print('onClick ${notification.identifier}');
}
};
notification.onClickAction = (actionIndex) {
if (kDebugMode) {
print('onClickAction ${notification.identifier} - $actionIndex');
}
};
notification.show();
}
Future<void> _mobileNotification() async {
// Only available for flutter 3.0.0 and later
DartPluginRegistrant.ensureInitialized();
// For flutter prior to version 3.0.0
// We have to register the plugin manually
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
final DarwinInitializationSettings initializationSettingsIOS =
DarwinInitializationSettings(
onDidReceiveLocalNotification: (id, title, body, payload) {});
final InitializationSettings initializationSettings =
InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS,
macOS: initializationSettingsIOS);
await getPer();
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
const AndroidNotificationDetails androidPlatformChannelSpecifics =
AndroidNotificationDetails('0', 'your channel name',
channelDescription: 'your channel description',
importance: Importance.max,
priority: Priority.high,
ticker: 'ticker');
const NotificationDetails platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, "Flutterap", "Hi, I am Flutterap:)", platformChannelSpecifics,
payload: 'item x');
/// you can see this log in logcat
//print('FLUTTER BACKGROUND SERVICE: ${DateTime.now()}');
// test using external plugin
if (Platform.isAndroid) {}
if (Platform.isIOS) {}
}
Future<void> getPer() async {
final androidInfo = await DeviceInfoPlugin().androidInfo;
late final Map<Permission, PermissionStatus> statusess;
if (androidInfo.version.sdkInt <= 32) {
statusess = await [
Permission.notification,
].request();
} else {
statusess = await [Permission.notification].request();
}
statusess.forEach((permission, status) {
if (status != PermissionStatus.granted) {}
});
}
Set show the notification after pressing the button
FxButton(
text: "Send notification",
onTap: () {
if (kIsWeb) {
} else {
(Platform.isWindows ||
Platform.isLinux ||
Platform.isMacOS)
? _desktopNotif()
: _mobileNotification();
}
},
),
we use this widget in panel in notification sample page and it is the desktop output: