/
main.dart
580 lines (550 loc) · 23.9 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
/// 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
void main() async {
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
// NOTE: if you want to find out if the app was launched via notification then you could use the following call and then do something like
// change the default route of the app
// var notificationAppLaunchDetails =
// await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
runApp(
new MaterialApp(
home: HomePage(),
),
);
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => new _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
initState() {
super.initState();
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
var initializationSettingsAndroid =
new AndroidInitializationSettings('app_icon');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
selectNotification: onSelectNotification);
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Plugin example app'),
),
body: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: new Padding(
padding: new EdgeInsets.all(8.0),
child: new Center(
child: new Column(
children: <Widget>[
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: new Text(
'Tap on a notification when it appears to trigger navigation'),
),
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: new RaisedButton(
child: new Text('Show plain notification with payload'),
onPressed: () async {
await _showNotification();
},
),
),
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: new RaisedButton(
child: new Text('Cancel notification'),
onPressed: () async {
await _cancelNotification();
},
),
),
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: new RaisedButton(
child: new Text(
'Schedule notification to appear in 5 seconds, custom sound, red colour, large icon'),
onPressed: () async {
await _scheduleNotification();
})),
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: new RaisedButton(
child: new Text('Repeat notification every minute'),
onPressed: () async {
await _repeatNotification();
},
),
),
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: new RaisedButton(
child: new Text(
'Repeat notification every day at approximately 10:00:00 am'),
onPressed: () async {
await _showDailyAtTime();
},
),
),
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: new RaisedButton(
child: new Text(
'Repeat notification weekly on Monday at approximately 10:00:00 am'),
onPressed: () async {
await _showWeeklyAtDayAndTime();
},
),
),
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: new RaisedButton(
child: new Text('Show notification with no sound'),
onPressed: () async {
await _showNotificationWithNoSound();
},
),
),
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: new RaisedButton(
child:
new Text('Show big picture notification [Android]'),
onPressed: () async {
await _showBigPictureNotification();
},
),
),
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: new RaisedButton(
child: new Text('Show big text notification [Android]'),
onPressed: () async {
await _showBigTextNotification();
},
),
),
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: new RaisedButton(
child: new Text('Show inbox notification [Android]'),
onPressed: () async {
await _showInboxNotification();
},
),
),
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: new RaisedButton(
child: new Text('Show grouped notifications [Android]'),
onPressed: () async {
await _showGroupedNotifications();
},
),
),
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: new RaisedButton(
child: new Text('Show ongoing notification [Android]'),
onPressed: () async {
await _showOngoingNotification();
},
),
),
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: new RaisedButton(
child: new Text(
'Show notification with no badge, alert only once [Android]'),
onPressed: () async {
await _showNotificationWithNoBadge();
},
),
),
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: new RaisedButton(
child: new Text(
'Show progress notification - updates every second [Android]'),
onPressed: () async {
await _showProgressNotification();
},
),
),
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: new RaisedButton(
child: new Text(
'Show indeterminate progress notification [Android]'),
onPressed: () async {
await _showIndeterminateProgressNotification();
},
),
),
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: new RaisedButton(
child: new Text('cancel all notifications'),
onPressed: () async {
await _cancelAllNotifications();
},
),
),
],
),
),
),
),
),
);
}
Future _showNotification() async {
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'your channel id', 'your channel name', 'your channel description',
importance: Importance.Max, priority: Priority.High);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, 'plain title', 'plain body', platformChannelSpecifics,
payload: 'item x');
}
Future _cancelNotification() async {
await flutterLocalNotificationsPlugin.cancel(0);
}
/// Schedules a notification that specifies a different icon, sound and vibration pattern
Future _scheduleNotification() async {
var scheduledNotificationDateTime =
new DateTime.now().add(new Duration(seconds: 5));
var vibrationPattern = new Int64List(4);
vibrationPattern[0] = 0;
vibrationPattern[1] = 1000;
vibrationPattern[2] = 5000;
vibrationPattern[3] = 2000;
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'your other channel id',
'your other channel name',
'your other channel description',
icon: 'secondary_icon',
sound: 'slow_spring_board',
largeIcon: 'sample_large_icon',
largeIconBitmapSource: BitmapSource.Drawable,
vibrationPattern: vibrationPattern,
color: const Color.fromARGB(255, 255, 0, 0));
var iOSPlatformChannelSpecifics =
new IOSNotificationDetails(sound: "slow_spring_board.aiff");
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.schedule(
0,
'scheduled title',
'scheduled body',
scheduledNotificationDateTime,
platformChannelSpecifics);
}
Future _showNotificationWithNoSound() async {
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'silent channel id',
'silent channel name',
'silent channel description',
playSound: false,
styleInformation: new DefaultStyleInformation(true, true));
var iOSPlatformChannelSpecifics =
new IOSNotificationDetails(presentSound: false);
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(0, '<b>silent</b> title',
'<b>silent</b> body', platformChannelSpecifics);
}
Future _showBigPictureNotification() async {
var directory = await getApplicationDocumentsDirectory();
var largeIconResponse = await http.get('http://via.placeholder.com/48x48');
var largeIconPath = '${directory.path}/largeIcon';
var file = new File(largeIconPath);
await file.writeAsBytes(largeIconResponse.bodyBytes);
var bigPictureResponse =
await http.get('http://via.placeholder.com/400x800');
var bigPicturePath = '${directory.path}/bigPicture';
file = new File(bigPicturePath);
await file.writeAsBytes(bigPictureResponse.bodyBytes);
var bigPictureStyleInformation = new BigPictureStyleInformation(
bigPicturePath, BitmapSource.FilePath,
largeIcon: largeIconPath,
largeIconBitmapSource: BitmapSource.FilePath,
contentTitle: 'overridden <b>big</b> content title',
htmlFormatContentTitle: true,
summaryText: 'summary <i>text</i>',
htmlFormatSummaryText: true);
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'big text channel id',
'big text channel name',
'big text channel description',
style: AndroidNotificationStyle.BigPicture,
styleInformation: bigPictureStyleInformation);
var platformChannelSpecifics =
new NotificationDetails(androidPlatformChannelSpecifics, null);
await flutterLocalNotificationsPlugin.show(
0, 'big text title', 'silent body', platformChannelSpecifics);
}
Future _showBigTextNotification() async {
var bigTextStyleInformation = new 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 = new AndroidNotificationDetails(
'big text channel id',
'big text channel name',
'big text channel description',
style: AndroidNotificationStyle.BigText,
styleInformation: bigTextStyleInformation);
var platformChannelSpecifics =
new NotificationDetails(androidPlatformChannelSpecifics, null);
await flutterLocalNotificationsPlugin.show(
0, 'big text title', 'silent body', platformChannelSpecifics);
}
Future _showInboxNotification() async {
var lines = new List<String>();
lines.add('line <b>1</b>');
lines.add('line <i>2</i>');
var inboxStyleInformation = new InboxStyleInformation(lines,
htmlFormatLines: true,
contentTitle: 'overridden <b>inbox</b> context title',
htmlFormatContentTitle: true,
summaryText: 'summary <i>text</i>',
htmlFormatSummaryText: true);
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'inbox channel id', 'inboxchannel name', 'inbox channel description',
style: AndroidNotificationStyle.Inbox,
styleInformation: inboxStyleInformation);
var platformChannelSpecifics =
new NotificationDetails(androidPlatformChannelSpecifics, null);
await flutterLocalNotificationsPlugin.show(
0, 'inbox title', 'inbox body', platformChannelSpecifics);
}
Future _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 = new AndroidNotificationDetails(
groupChannelId, groupChannelName, groupChannelDescription,
importance: Importance.Max,
priority: Priority.High,
groupKey: groupKey);
var firstNotificationPlatformSpecifics =
new NotificationDetails(firstNotificationAndroidSpecifics, null);
await flutterLocalNotificationsPlugin.show(1, 'Alex Faarborg',
'You will not believe...', firstNotificationPlatformSpecifics);
var secondNotificationAndroidSpecifics = new AndroidNotificationDetails(
groupChannelId, groupChannelName, groupChannelDescription,
importance: Importance.Max,
priority: Priority.High,
groupKey: groupKey);
var secondNotificationPlatformSpecifics =
new NotificationDetails(secondNotificationAndroidSpecifics, null);
await flutterLocalNotificationsPlugin.show(
2,
'Jeff Chang',
'Please join us to celebrate the...',
secondNotificationPlatformSpecifics);
// create the summary notification required for older devices that pre-date Android 7.0 (API level 24)
var lines = new List<String>();
lines.add('Alex Faarborg Check this out');
lines.add('Jeff Chang Launch Party');
var inboxStyleInformation = new InboxStyleInformation(lines,
contentTitle: '2 new messages', summaryText: 'janedoe@example.com');
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
groupChannelId, groupChannelName, groupChannelDescription,
style: AndroidNotificationStyle.Inbox,
styleInformation: inboxStyleInformation,
groupKey: groupKey,
setAsGroupSummary: true);
var platformChannelSpecifics =
new NotificationDetails(androidPlatformChannelSpecifics, null);
await flutterLocalNotificationsPlugin.show(
3, 'Attention', 'Two new messages', platformChannelSpecifics);
}
Future _cancelAllNotifications() async {
await flutterLocalNotificationsPlugin.cancelAll();
}
Future onSelectNotification(String payload) async {
if (payload != null) {
debugPrint('notification payload: ' + payload);
}
await Navigator.push(
context,
new MaterialPageRoute(builder: (context) => new SecondScreen(payload)),
);
}
Future _showOngoingNotification() async {
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'your channel id', 'your channel name', 'your channel description',
importance: Importance.Max,
priority: Priority.High,
ongoing: true,
autoCancel: false);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(0, 'ongoing notification title',
'ongoing notification body', platformChannelSpecifics);
}
Future _repeatNotification() async {
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'repeating channel id',
'repeating channel name',
'repeating description');
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.periodicallyShow(0, 'repeating title',
'repeating body', RepeatInterval.EveryMinute, platformChannelSpecifics);
}
Future _showDailyAtTime() async {
var time = new Time(10, 0, 0);
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'repeatDailyAtTime channel id',
'repeatDailyAtTime channel name',
'repeatDailyAtTime description');
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.showDailyAtTime(
0,
'show daily title',
'Daily notification shown at approximately ${_toTwoDigitString(time.hour)}:${_toTwoDigitString(time.minute)}:${_toTwoDigitString(time.second)}',
time,
platformChannelSpecifics);
}
Future _showWeeklyAtDayAndTime() async {
var time = new Time(10, 0, 0);
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'show weekly channel id',
'show weekly channel name',
'show weekly description');
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.showWeeklyAtDayAndTime(
0,
'show weekly title',
'Weekly notification shown on Monday at approximately ${_toTwoDigitString(time.hour)}:${_toTwoDigitString(time.minute)}:${_toTwoDigitString(time.second)}',
Day.Monday,
time,
platformChannelSpecifics);
}
Future _showNotificationWithNoBadge() async {
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'no badge channel', 'no badge name', 'no badge description',
channelShowBadge: false,
importance: Importance.Max,
priority: Priority.High,
onlyAlertOnce: true);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, 'no badge title', 'no badge body', platformChannelSpecifics,
payload: 'item x');
}
Future _showProgressNotification() async {
var maxProgress = 5;
for (var i = 0; i <= maxProgress; i++) {
await Future.delayed(Duration(seconds: 1), () async {
var androidPlatformChannelSpecifics = new 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 iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'progress notification title',
'progress notification body',
platformChannelSpecifics,
payload: 'item x');
});
}
}
Future _showIndeterminateProgressNotification() async {
var androidPlatformChannelSpecifics = new 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 iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'indeterminate progress notification title',
'indeterminate progress notification body',
platformChannelSpecifics,
payload: 'item x');
}
String _toTwoDigitString(int value) {
return value.toString().padLeft(2, '0');
}
}
class SecondScreen extends StatefulWidget {
final String payload;
SecondScreen(this.payload);
@override
State<StatefulWidget> createState() => new SecondScreenState();
}
class SecondScreenState extends State<SecondScreen> {
String _payload;
@override
void initState() {
super.initState();
_payload = widget.payload;
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Second Screen with payload: " + _payload),
),
body: new Center(
child: new RaisedButton(
onPressed: () {
Navigator.pop(context);
},
child: new Text('Go back!'),
),
),
);
}
}