flutter_local_notifications 1.5.0-beta.5 flutter_local_notifications: ^1.5.0-beta.5 copied to clipboard
A cross platform plugin for displaying and scheduling local notifications for Flutter applications with the ability to customise for each platform.
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';
import 'package:rxdart/subjects.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
// Streams are created so that app can respond to notification-related events since the plugin is initialised in the `main` function
final BehaviorSubject<ReceivedNotification> didReceiveLocalNotificationSubject =
BehaviorSubject<ReceivedNotification>();
final BehaviorSubject<String> selectNotificationSubject =
BehaviorSubject<String>();
final MethodChannel platform =
MethodChannel('dexterx.dev/flutter_local_notifications_example');
NotificationAppLaunchDetails notificationAppLaunchDetails;
class ReceivedNotification {
final int id;
final String title;
final String body;
final String payload;
ReceivedNotification({
@required this.id,
@required this.title,
@required this.body,
@required this.payload,
});
}
/// IMPORTANT: running the following code on its own won't work as there is setup required for each platform head project.
/// Please download the complete example app from the GitHub repository where all the setup has been done
Future<void> main() async {
// needed if you intend to initialize in the `main` function
WidgetsFlutterBinding.ensureInitialized();
await _configureLocalTimeZone();
notificationAppLaunchDetails =
await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
var initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
// Note: permissions aren't requested here just to demonstrate that can be done later
var initializationSettingsIOS = IOSInitializationSettings(
requestAlertPermission: false,
requestBadgePermission: false,
requestSoundPermission: false,
onDidReceiveLocalNotification:
(int id, String title, String body, String payload) async {
didReceiveLocalNotificationSubject.add(ReceivedNotification(
id: id, title: title, body: body, payload: payload));
});
var initializationSettingsMacOS = MacOSInitializationSettings(
requestAlertPermission: false,
requestBadgePermission: false,
requestSoundPermission: false);
var initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS,
macOS: initializationSettingsMacOS);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (String payload) async {
if (payload != null) {
debugPrint('notification payload: ' + payload);
}
selectNotificationSubject.add(payload);
});
runApp(
MaterialApp(
home: HomePage(),
),
);
}
Future<void> _configureLocalTimeZone() async {
tz.initializeTimeZones();
final String timeZoneName = await platform.invokeMethod('getTimeZoneName');
tz.setLocalLocation(tz.getLocation(timeZoneName));
}
class PaddedRaisedButton extends StatelessWidget {
final String buttonText;
final VoidCallback onPressed;
const PaddedRaisedButton({
@required this.buttonText,
@required this.onPressed,
});
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: RaisedButton(child: Text(buttonText), onPressed: onPressed),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
void initState() {
super.initState();
_requestPermissions();
_configureDidReceiveLocalNotificationSubject();
_configureSelectNotificationSubject();
}
void _requestPermissions() {
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(
alert: true,
badge: true,
sound: true,
);
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
MacOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(
alert: true,
badge: true,
sound: true,
);
}
void _configureDidReceiveLocalNotificationSubject() {
didReceiveLocalNotificationSubject.stream
.listen((ReceivedNotification receivedNotification) async {
await showDialog(
context: context,
builder: (BuildContext context) => CupertinoAlertDialog(
title: receivedNotification.title != null
? Text(receivedNotification.title)
: null,
content: receivedNotification.body != null
? Text(receivedNotification.body)
: null,
actions: [
CupertinoDialogAction(
isDefaultAction: true,
child: Text('Ok'),
onPressed: () async {
Navigator.of(context, rootNavigator: true).pop();
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
SecondScreen(receivedNotification.payload),
),
);
},
)
],
),
);
});
}
void _configureSelectNotificationSubject() {
selectNotificationSubject.stream.listen((String payload) async {
await Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen(payload)),
);
});
}
@override
void dispose() {
didReceiveLocalNotificationSubject.close();
selectNotificationSubject.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Plugin example app'),
),
body: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Padding(
padding: EdgeInsets.all(8.0),
child: Center(
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: Text(
'Tap on a notification when it appears to trigger navigation'),
),
Padding(
padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: Text.rich(
TextSpan(
children: [
TextSpan(
text: 'Did notification launch app? ',
style: TextStyle(fontWeight: FontWeight.bold),
),
TextSpan(
text:
'${notificationAppLaunchDetails?.didNotificationLaunchApp ?? false}',
)
],
),
),
),
if (notificationAppLaunchDetails?.didNotificationLaunchApp ??
false)
Padding(
padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: Text.rich(
TextSpan(
children: [
TextSpan(
text: 'Launch notification payload: ',
style: TextStyle(fontWeight: FontWeight.bold),
),
TextSpan(
text: notificationAppLaunchDetails.payload,
)
],
),
),
),
PaddedRaisedButton(
buttonText: 'Show plain notification with payload',
onPressed: () async {
await _showNotification();
},
),
PaddedRaisedButton(
buttonText:
'Show plain notification that has no body with payload',
onPressed: () async {
await _showNotificationWithNoBody();
},
),
PaddedRaisedButton(
buttonText: 'Show notification with custom sound',
onPressed: () async {
await _showNotificationCustomSound();
},
),
PaddedRaisedButton(
buttonText:
'Schedule notification to appear in 5 seconds based on local time zone',
onPressed: () async {
await _zonedScheduleNotification();
},
),
PaddedRaisedButton(
buttonText: 'Repeat notification every minute',
onPressed: () async {
await _repeatNotification();
},
),
PaddedRaisedButton(
buttonText:
'Schedule daily 10:00:00 am notification in your local time zone',
onPressed: () async {
await _scheduleDailyTenAMNotification();
},
),
PaddedRaisedButton(
buttonText:
'Schedule weekly 10:00:00 am notification in your local time zone',
onPressed: () async {
await _scheduleWeeklyTenAMNotification();
},
),
PaddedRaisedButton(
buttonText: 'Show notification with no sound',
onPressed: () async {
await _showNotificationWithNoSound();
},
),
PaddedRaisedButton(
buttonText: 'Check pending notifications',
onPressed: () async {
await _checkPendingNotificationRequests();
},
),
PaddedRaisedButton(
buttonText: 'Cancel notification',
onPressed: () async {
await _cancelNotification();
},
),
PaddedRaisedButton(
buttonText: 'Cancel all notifications',
onPressed: () async {
await _cancelAllNotifications();
},
),
if (Platform.isAndroid) ...[
Text(
'Android-specific examples',
style: TextStyle(fontWeight: FontWeight.bold),
),
PaddedRaisedButton(
buttonText:
'Show plain notification with payload and update channel description',
onPressed: () async {
await _showNotificationWithUpdatedChannelDescription();
},
),
PaddedRaisedButton(
buttonText:
'Show plain notification as public on every lockscreen',
onPressed: () async {
await _showPublicNotification();
},
),
PaddedRaisedButton(
buttonText:
'Show notification with custom vibration pattern, LED and icon',
onPressed: () async {
await _showNotificationCustomVibrationIconLed();
},
),
PaddedRaisedButton(
buttonText: 'Show notification using Android Uri sound',
onPressed: () async {
await _showSoundUriNotification();
},
),
PaddedRaisedButton(
buttonText:
'Show notification that times out after 3 seconds',
onPressed: () async {
await _showTimeoutNotification();
},
),
PaddedRaisedButton(
buttonText: 'Show insistent notification',
onPressed: () async {
await _showInsistentNotification();
},
),
PaddedRaisedButton(
buttonText: 'Show big picture notification',
onPressed: () async {
await _showBigPictureNotification();
},
),
PaddedRaisedButton(
buttonText:
'Show big picture notification, hide large icon on expand',
onPressed: () async {
await _showBigPictureNotificationHideExpandedLargeIcon();
},
),
PaddedRaisedButton(
buttonText: 'Show media notification',
onPressed: () async {
await _showNotificationMediaStyle();
},
),
PaddedRaisedButton(
buttonText: 'Show big text notification',
onPressed: () async {
await _showBigTextNotification();
},
),
PaddedRaisedButton(
buttonText: 'Show inbox notification',
onPressed: () async {
await _showInboxNotification();
},
),
PaddedRaisedButton(
buttonText: 'Show messaging notification',
onPressed: () async {
await _showMessagingNotification();
},
),
PaddedRaisedButton(
buttonText: 'Show grouped notifications',
onPressed: () async {
await _showGroupedNotifications();
},
),
PaddedRaisedButton(
buttonText: 'Show ongoing notification',
onPressed: () async {
await _showOngoingNotification();
},
),
PaddedRaisedButton(
buttonText:
'Show notification with no badge, alert only once',
onPressed: () async {
await _showNotificationWithNoBadge();
},
),
PaddedRaisedButton(
buttonText:
'Show progress notification - updates every second',
onPressed: () async {
await _showProgressNotification();
},
),
PaddedRaisedButton(
buttonText: 'Show indeterminate progress notification',
onPressed: () async {
await _showIndeterminateProgressNotification();
},
),
PaddedRaisedButton(
buttonText: 'Show notification without timestamp',
onPressed: () async {
await _showNotificationWithoutTimestamp();
},
),
PaddedRaisedButton(
buttonText: 'Show notification with custom timestamp',
onPressed: () async {
await _showNotificationWithCustomTimestamp();
},
),
PaddedRaisedButton(
buttonText: 'Create notification channel',
onPressed: () async {
await _createNotificationChannel();
},
),
PaddedRaisedButton(
buttonText: 'Delete notification channel',
onPressed: () async {
await _deleteNotificationChannel();
},
),
],
if (Platform.isIOS || Platform.isMacOS) ...[
Text(
'iOS and macOS-specific examples',
style: TextStyle(fontWeight: FontWeight.bold),
),
PaddedRaisedButton(
buttonText: 'Show notification with subtitle',
onPressed: () async {
await _showNotificationWithSubtitle();
},
),
PaddedRaisedButton(
buttonText: 'Show notification with icon badge',
onPressed: () async {
await _showNotificationWithIconBadge();
},
),
PaddedRaisedButton(
buttonText: 'Show notification with attachment',
onPressed: () async {
await _showNotificationWithAttachment();
},
),
],
],
),
),
),
),
),
);
}
Future<void> _showNotification() async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your channel id', 'your channel name', 'your channel description',
importance: Importance.max, priority: Priority.high, ticker: 'ticker');
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, 'plain title', 'plain body', platformChannelSpecifics,
payload: 'item x');
}
Future<void> _showNotificationWithNoBody() async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your channel id', 'your channel name', 'your channel description',
importance: Importance.max, priority: Priority.high, ticker: 'ticker');
var platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
);
await flutterLocalNotificationsPlugin.show(
0, 'plain title', null, platformChannelSpecifics,
payload: 'item x');
}
Future<void> _cancelNotification() async {
await flutterLocalNotificationsPlugin.cancel(0);
}
Future<void> _showNotificationCustomSound() async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your other channel id',
'your other channel name',
'your other channel description',
sound: RawResourceAndroidNotificationSound('slow_spring_board'),
);
var iOSPlatformChannelSpecifics =
IOSNotificationDetails(sound: 'slow_spring_board.aiff');
var macOSPlatformChannelSpecifics =
MacOSNotificationDetails(sound: 'slow_spring_board.aiff');
var platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iOSPlatformChannelSpecifics,
macOS: macOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'custom sound notification title',
'custom sound notification body',
platformChannelSpecifics);
}
Future<void> _showNotificationCustomVibrationIconLed() async {
var vibrationPattern = Int64List(4);
vibrationPattern[0] = 0;
vibrationPattern[1] = 1000;
vibrationPattern[2] = 5000;
vibrationPattern[3] = 2000;
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'other custom channel id',
'other custom channel name',
'other custom channel description',
icon: 'secondary_icon',
largeIcon: DrawableResourceAndroidBitmap('sample_large_icon'),
vibrationPattern: vibrationPattern,
enableLights: true,
color: const Color.fromARGB(255, 255, 0, 0),
ledColor: const Color.fromARGB(255, 255, 0, 0),
ledOnMs: 1000,
ledOffMs: 500);
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'title of notification with custom vibration pattern, LED and icon',
'body of notification with custom vibration pattern, LED and icon',
platformChannelSpecifics);
}
Future<void> _zonedScheduleNotification() async {
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
'scheduled title',
'scheduled body',
tz.TZDateTime.now(tz.local).add(Duration(seconds: 5)),
NotificationDetails(
android: AndroidNotificationDetails('your channel id',
'your channel name', 'your channel description')),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime);
}
Future<void> _showNotificationWithNoSound() async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'silent channel id',
'silent channel name',
'silent channel description',
playSound: false,
styleInformation: DefaultStyleInformation(true, true));
var iOSPlatformChannelSpecifics =
IOSNotificationDetails(presentSound: false);
var macOSPlatformChannelSpecifics =
MacOSNotificationDetails(presentSound: false);
var platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iOSPlatformChannelSpecifics,
macOS: macOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(0, '<b>silent</b> title',
'<b>silent</b> body', platformChannelSpecifics);
}
Future<void> _showSoundUriNotification() async {
// this calls a method over a platform channel implemented within the example app to return the Uri for the default
// alarm sound and uses as the notification sound
String alarmUri = await platform.invokeMethod('getAlarmUri');
final x = UriAndroidNotificationSound(alarmUri);
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'uri channel id', 'uri channel name', 'uri channel description',
sound: x,
playSound: true,
styleInformation: DefaultStyleInformation(true, true));
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, 'uri sound title', 'uri sound body', platformChannelSpecifics);
}
Future<void> _showTimeoutNotification() async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'silent channel id',
'silent channel name',
'silent channel description',
timeoutAfter: 3000,
styleInformation: DefaultStyleInformation(true, true));
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(0, 'timeout notification',
'Times out after 3 seconds', platformChannelSpecifics);
}
Future<void> _showInsistentNotification() async {
// This value is from: https://developer.android.com/reference/android/app/Notification.html#FLAG_INSISTENT
var insistentFlag = 4;
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your channel id', 'your channel name', 'your channel description',
importance: Importance.max,
priority: Priority.high,
ticker: 'ticker',
additionalFlags: Int32List.fromList([insistentFlag]));
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, 'insistent title', 'insistent body', platformChannelSpecifics,
payload: 'item x');
}
Future<String> _downloadAndSaveFile(String url, String fileName) async {
var directory = await getApplicationDocumentsDirectory();
var filePath = '${directory.path}/$fileName';
var response = await http.get(url);
var file = File(filePath);
await file.writeAsBytes(response.bodyBytes);
return filePath;
}
Future<void> _showBigPictureNotification() async {
var largeIconPath = await _downloadAndSaveFile(
'http://via.placeholder.com/48x48', 'largeIcon');
var bigPicturePath = await _downloadAndSaveFile(
'http://via.placeholder.com/400x800', 'bigPicture');
var bigPictureStyleInformation = BigPictureStyleInformation(
FilePathAndroidBitmap(bigPicturePath),
largeIcon: FilePathAndroidBitmap(largeIconPath),
contentTitle: 'overridden <b>big</b> content title',
htmlFormatContentTitle: true,
summaryText: 'summary <i>text</i>',
htmlFormatSummaryText: true);
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'big text channel id',
'big text channel name',
'big text channel description',
styleInformation: bigPictureStyleInformation);
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, 'big text title', 'silent body', platformChannelSpecifics);
}
Future<void> _showBigPictureNotificationHideExpandedLargeIcon() async {
var largeIconPath = await _downloadAndSaveFile(
'http://via.placeholder.com/48x48', 'largeIcon');
var bigPicturePath = await _downloadAndSaveFile(
'http://via.placeholder.com/400x800', 'bigPicture');
var bigPictureStyleInformation = BigPictureStyleInformation(
FilePathAndroidBitmap(bigPicturePath),
hideExpandedLargeIcon: true,
contentTitle: 'overridden <b>big</b> content title',
htmlFormatContentTitle: true,
summaryText: 'summary <i>text</i>',
htmlFormatSummaryText: true);
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'big text channel id',
'big text channel name',
'big text channel description',
largeIcon: FilePathAndroidBitmap(largeIconPath),
styleInformation: bigPictureStyleInformation);
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, 'big text title', 'silent body', platformChannelSpecifics);
}
Future<void> _showNotificationMediaStyle() async {
var largeIconPath = await _downloadAndSaveFile(
'http://via.placeholder.com/128x128/00FF00/000000', 'largeIcon');
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'media channel id',
'media channel name',
'media channel description',
largeIcon: FilePathAndroidBitmap(largeIconPath),
styleInformation: MediaStyleInformation(),
);
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, 'notification title', 'notification body', platformChannelSpecifics);
}
Future<void> _showBigTextNotification() async {
var bigTextStyleInformation = BigTextStyleInformation(
'Lorem <i>ipsum dolor sit</i> amet, consectetur <b>adipiscing elit</b>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
htmlFormatBigText: true,
contentTitle: 'overridden <b>big</b> content title',
htmlFormatContentTitle: true,
summaryText: 'summary <i>text</i>',
htmlFormatSummaryText: true);
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'big text channel id',
'big text channel name',
'big text channel description',
styleInformation: bigTextStyleInformation);
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, 'big text title', 'silent body', platformChannelSpecifics);
}
Future<void> _showInboxNotification() async {
var lines = List<String>();
lines.add('line <b>1</b>');
lines.add('line <i>2</i>');
var inboxStyleInformation = InboxStyleInformation(lines,
htmlFormatLines: true,
contentTitle: 'overridden <b>inbox</b> context title',
htmlFormatContentTitle: true,
summaryText: 'summary <i>text</i>',
htmlFormatSummaryText: true);
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'inbox channel id', 'inboxchannel name', 'inbox channel description',
styleInformation: inboxStyleInformation);
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, 'inbox title', 'inbox body', platformChannelSpecifics);
}
Future<void> _showMessagingNotification() async {
// use a platform channel to resolve an Android drawable resource to a URI.
// This is NOT part of the notifications plugin. Calls made over this channel is handled by the app
String imageUri = await platform.invokeMethod('drawableToUri', 'food');
var messages = List<Message>();
// First two person objects will use icons that part of the Android app's drawable resources
var me = Person(
name: 'Me',
key: '1',
uri: 'tel:1234567890',
icon: DrawableResourceAndroidIcon('me'),
);
var coworker = Person(
name: 'Coworker',
key: '2',
uri: 'tel:9876543210',
icon: FlutterBitmapAssetAndroidIcon('icons/coworker.png'),
);
// download the icon that would be use for the lunch bot person
var largeIconPath = await _downloadAndSaveFile(
'http://via.placeholder.com/48x48', 'largeIcon');
// this person object will use an icon that was downloaded
var lunchBot = Person(
name: 'Lunch bot',
key: 'bot',
bot: true,
icon: BitmapFilePathAndroidIcon(largeIconPath),
);
messages.add(Message('Hi', DateTime.now(), null));
messages.add(Message(
'What\'s up?', DateTime.now().add(Duration(minutes: 5)), coworker));
messages.add(Message(
'Lunch?', DateTime.now().add(Duration(minutes: 10)), null,
dataMimeType: 'image/png', dataUri: imageUri));
messages.add(Message('What kind of food would you prefer?',
DateTime.now().add(Duration(minutes: 10)), lunchBot));
var messagingStyle = MessagingStyleInformation(me,
groupConversation: true,
conversationTitle: 'Team lunch',
htmlFormatContent: true,
htmlFormatTitle: true,
messages: messages);
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'message channel id',
'message channel name',
'message channel description',
category: 'msg',
styleInformation: messagingStyle);
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, 'message title', 'message body', platformChannelSpecifics);
// wait 10 seconds and add another message to simulate another response
await Future.delayed(Duration(seconds: 10), () async {
messages.add(
Message('Thai', DateTime.now().add(Duration(minutes: 11)), null));
await flutterLocalNotificationsPlugin.show(
0, 'message title', 'message body', platformChannelSpecifics);
});
}
Future<void> _showGroupedNotifications() async {
var groupKey = 'com.android.example.WORK_EMAIL';
var groupChannelId = 'grouped channel id';
var groupChannelName = 'grouped channel name';
var groupChannelDescription = 'grouped channel description';
// example based on https://developer.android.com/training/notify-user/group.html
var firstNotificationAndroidSpecifics = AndroidNotificationDetails(
groupChannelId, groupChannelName, groupChannelDescription,
importance: Importance.max,
priority: Priority.high,
groupKey: groupKey);
var firstNotificationPlatformSpecifics =
NotificationDetails(android: firstNotificationAndroidSpecifics);
await flutterLocalNotificationsPlugin.show(1, 'Alex Faarborg',
'You will not believe...', firstNotificationPlatformSpecifics);
var secondNotificationAndroidSpecifics = AndroidNotificationDetails(
groupChannelId, groupChannelName, groupChannelDescription,
importance: Importance.max,
priority: Priority.high,
groupKey: groupKey);
var secondNotificationPlatformSpecifics =
NotificationDetails(android: secondNotificationAndroidSpecifics);
await flutterLocalNotificationsPlugin.show(
2,
'Jeff Chang',
'Please join us to celebrate the...',
secondNotificationPlatformSpecifics);
// Create the summary notification to support older devices that pre-date Android 7.0 (API level 24).
// Recommended to create this regardless as the behaviour may vary as mentioned in
// https://developer.android.com/training/notify-user/group
var lines = List<String>();
lines.add('Alex Faarborg Check this out');
lines.add('Jeff Chang Launch Party');
var inboxStyleInformation = InboxStyleInformation(lines,
contentTitle: '2 messages', summaryText: 'janedoe@example.com');
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
groupChannelId, groupChannelName, groupChannelDescription,
styleInformation: inboxStyleInformation,
groupKey: groupKey,
setAsGroupSummary: true);
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
3, 'Attention', 'Two messages', platformChannelSpecifics);
}
Future<void> _checkPendingNotificationRequests() async {
var pendingNotificationRequests =
await flutterLocalNotificationsPlugin.pendingNotificationRequests();
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Text(
'${pendingNotificationRequests.length} pending notification requests'),
actions: [
FlatButton(
child: Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
Future<void> _cancelAllNotifications() async {
await flutterLocalNotificationsPlugin.cancelAll();
}
Future<void> _showOngoingNotification() async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your channel id', 'your channel name', 'your channel description',
importance: Importance.max,
priority: Priority.high,
ongoing: true,
autoCancel: false);
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(0, 'ongoing notification title',
'ongoing notification body', platformChannelSpecifics);
}
Future<void> _repeatNotification() async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'repeating channel id',
'repeating channel name',
'repeating description');
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.periodicallyShow(0, 'repeating title',
'repeating body', RepeatInterval.everyMinute, platformChannelSpecifics,
androidAllowWhileIdle: true);
}
Future<void> _scheduleDailyTenAMNotification() async {
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
'daily scheduled notification title',
'daily scheduled notification body',
_nextInstanceOfTenAM(),
NotificationDetails(
android: AndroidNotificationDetails(
'daily notification channel id',
'daily notification channel name',
'daily notification description'),
),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
scheduledNotificationRepeatFrequency:
ScheduledNotificationRepeatFrequency.daily);
}
Future<void> _scheduleWeeklyTenAMNotification() async {
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
'weekly scheduled notification title',
'weekly scheduled notification body',
_nextInstanceOfTenAM(),
NotificationDetails(
android: AndroidNotificationDetails(
'weekly notification channel id',
'weekly notification channel name',
'weekly notificationdescription'),
),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
scheduledNotificationRepeatFrequency:
ScheduledNotificationRepeatFrequency.weekly);
}
tz.TZDateTime _nextInstanceOfTenAM() {
var now = tz.TZDateTime.now(tz.local);
var scheduledDate =
tz.TZDateTime(tz.local, now.year, now.month, now.day, 10);
if (scheduledDate.isBefore(now)) {
scheduledDate = scheduledDate.add(Duration(days: 1));
}
return scheduledDate;
}
Future<void> _showNotificationWithNoBadge() async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'no badge channel', 'no badge name', 'no badge description',
channelShowBadge: false,
importance: Importance.max,
priority: Priority.high,
onlyAlertOnce: true);
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, 'no badge title', 'no badge body', platformChannelSpecifics,
payload: 'item x');
}
Future<void> _showProgressNotification() async {
var maxProgress = 5;
for (var i = 0; i <= maxProgress; i++) {
await Future.delayed(Duration(seconds: 1), () async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'progress channel',
'progress channel',
'progress channel description',
channelShowBadge: false,
importance: Importance.max,
priority: Priority.high,
onlyAlertOnce: true,
showProgress: true,
maxProgress: maxProgress,
progress: i);
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'progress notification title',
'progress notification body',
platformChannelSpecifics,
payload: 'item x');
});
}
}
Future<void> _showIndeterminateProgressNotification() async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'indeterminate progress channel',
'indeterminate progress channel',
'indeterminate progress channel description',
channelShowBadge: false,
importance: Importance.max,
priority: Priority.high,
onlyAlertOnce: true,
showProgress: true,
indeterminate: true);
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'indeterminate progress notification title',
'indeterminate progress notification body',
platformChannelSpecifics,
payload: 'item x');
}
Future<void> _showNotificationWithUpdatedChannelDescription() async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your channel id',
'your channel name',
'your updated channel description',
importance: Importance.max,
priority: Priority.high,
channelAction: AndroidNotificationChannelAction.update);
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'updated notification channel',
'check settings to see updated channel description',
platformChannelSpecifics,
payload: 'item x');
}
Future<void> _showPublicNotification() async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your channel id', 'your channel name', 'your channel description',
importance: Importance.max,
priority: Priority.high,
ticker: 'ticker',
visibility: NotificationVisibility.public);
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(0, 'public notification title',
'public notification body', platformChannelSpecifics,
payload: 'item x');
}
Future<void> _showNotificationWithSubtitle() async {
var iOSPlatformChannelSpecifics =
IOSNotificationDetails(subtitle: 'the subtitle');
var macOSPlatformChannelSpecifics =
MacOSNotificationDetails(subtitle: 'the subtitle');
var platformChannelSpecifics = NotificationDetails(
iOS: iOSPlatformChannelSpecifics, macOS: macOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'title of notification with a subtitle',
'body of notification with a subtitle',
platformChannelSpecifics,
payload: 'item x');
}
Future<void> _showNotificationWithIconBadge() async {
var iOSPlatformChannelSpecifics = IOSNotificationDetails(badgeNumber: 1);
var macOSPlatformChannelSpecifics =
MacOSNotificationDetails(badgeNumber: 1);
var platformChannelSpecifics = NotificationDetails(
iOS: iOSPlatformChannelSpecifics, macOS: macOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, 'icon badge title', 'icon badge body', platformChannelSpecifics,
payload: 'item x');
}
Future<void> _showNotificationWithoutTimestamp() async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your channel id', 'your channel name', 'your channel description',
importance: Importance.max, priority: Priority.high, showWhen: false);
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, 'plain title', 'plain body', platformChannelSpecifics,
payload: 'item x');
}
Future<void> _showNotificationWithCustomTimestamp() async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your channel id',
'your channel name',
'your channel description',
importance: Importance.max,
priority: Priority.high,
showWhen: true,
when: DateTime.now().millisecondsSinceEpoch - 120 * 1000,
);
var platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, 'plain title', 'plain body', platformChannelSpecifics,
payload: 'item x');
}
Future<void> _showNotificationWithAttachment() async {
var bigPicturePath = await _downloadAndSaveFile(
'http://via.placeholder.com/600x200', 'bigPicture.jpg');
var iOSPlatformChannelSpecifics = IOSNotificationDetails(
attachments: [IOSNotificationAttachment(bigPicturePath)]);
var macOSPlatformChannelSpecifics = MacOSNotificationDetails(
attachments: [MacOSNotificationAttachment(bigPicturePath)]);
var notificationDetails = NotificationDetails(
iOS: iOSPlatformChannelSpecifics, macOS: macOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'notification with attachment title',
'notification with attachment body',
notificationDetails);
}
Future<void> _createNotificationChannel() async {
var androidNotificationChannel = AndroidNotificationChannel(
'your channel id 2',
'your channel name 2',
'your channel description 2',
);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(androidNotificationChannel);
await showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Text(
'Channel with name \"${androidNotificationChannel.name}\" created'),
actions: [
FlatButton(
child: Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
});
}
Future<void> _deleteNotificationChannel() async {
const channelId = 'your channel id 2';
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.deleteNotificationChannel(channelId);
await showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Text('Channel with id \"$channelId\" deleted'),
actions: [
FlatButton(
child: Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
});
}
}
class SecondScreen extends StatefulWidget {
SecondScreen(this.payload);
final String payload;
@override
State<StatefulWidget> createState() => SecondScreenState();
}
class SecondScreenState extends State<SecondScreen> {
String _payload;
@override
void initState() {
super.initState();
_payload = widget.payload;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Screen with payload: ${(_payload ?? '')}'),
),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Go back!'),
),
),
);
}
}