import 'package:flutter/material.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:path_provider/path_provider.dart'; import 'dart:io' show File, Platform; import 'package:http/http.dart' as http; import 'package:rxdart/subjects.dart'; class NotificationPlugin { // FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin; final BehaviorSubject didReceivedLocalNotificationSubject = BehaviorSubject(); var initializationSettings; NotificationPlugin._() { init(); } init() async { flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); if (Platform.isIOS) { _requestIOSPermission(); } initializePlatformSpecifics(); } initializePlatformSpecifics() { var initializationSettingsAndroid = AndroidInitializationSettings('app_notf_icon'); var initializationSettingsIOS = IOSInitializationSettings( requestAlertPermission: true, requestBadgePermission: true, requestSoundPermission: false, onDidReceiveLocalNotification: (id, title, body, payload) async { ReceivedNotification receivedNotification = ReceivedNotification( id: id, title: title, body: body, payload: payload); didReceivedLocalNotificationSubject.add(receivedNotification); }, ); initializationSettings = InitializationSettings( android: initializationSettingsAndroid, iOS: initializationSettingsIOS); } _requestIOSPermission() { flutterLocalNotificationsPlugin .resolvePlatformSpecificImplementation< IOSFlutterLocalNotificationsPlugin>() .requestPermissions( alert: true, badge: true, sound: true, ); } setListenerForLowerVersions(Function onNotificationInLowerVersions) { didReceivedLocalNotificationSubject.listen((receivedNotification) { onNotificationInLowerVersions(receivedNotification); }); } setOnNotificationClick(Function onNotificationClick) async { await flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: (String payload) async { onNotificationClick(payload); }); } Future showNotification( String title, String body, String payload) async { var androidChannelSpecifics = AndroidNotificationDetails( 'CHANNEL_ID', 'CHANNEL_NAME', channelDescription: "CHANNEL_DESCRIPTION", importance: Importance.max, priority: Priority.high, playSound: true, timeoutAfter: 5000, styleInformation: DefaultStyleInformation(true, true), ); var iosChannelSpecifics = IOSNotificationDetails(); var platformChannelSpecifics = NotificationDetails( android: androidChannelSpecifics, iOS: iosChannelSpecifics); await flutterLocalNotificationsPlugin.show( 0, title, body, //null platformChannelSpecifics, payload: payload, ); } Future showDailyAtTime() async { var time = Time(21, 3, 0); var androidChannelSpecifics = AndroidNotificationDetails( 'CHANNEL_ID 4', 'CHANNEL_NAME 4', channelDescription: "CHANNEL_DESCRIPTION 4", importance: Importance.max, priority: Priority.high, ); var iosChannelSpecifics = IOSNotificationDetails(); var platformChannelSpecifics = NotificationDetails( android: androidChannelSpecifics, iOS: iosChannelSpecifics); // ignore: deprecated_member_use await flutterLocalNotificationsPlugin.showDailyAtTime( 0, 'Test Title at ${time.hour}:${time.minute}.${time.second}', 'Test Body', //null time, platformChannelSpecifics, payload: 'Test Payload', ); } Future showWeeklyAtDayTime() async { var time = Time(21, 5, 0); var androidChannelSpecifics = AndroidNotificationDetails( 'CHANNEL_ID 5', 'CHANNEL_NAME 5', channelDescription: "CHANNEL_DESCRIPTION 5", importance: Importance.max, priority: Priority.high, ); var iosChannelSpecifics = IOSNotificationDetails(); var platformChannelSpecifics = NotificationDetails( android: androidChannelSpecifics, iOS: iosChannelSpecifics); // ignore: deprecated_member_use await flutterLocalNotificationsPlugin.showWeeklyAtDayAndTime( 0, 'Test Title at ${time.hour}:${time.minute}.${time.second}', 'Test Body', //null Day.saturday, time, platformChannelSpecifics, payload: 'Test Payload', ); } Future repeatNotification() async { var androidChannelSpecifics = AndroidNotificationDetails( 'CHANNEL_ID 3', 'CHANNEL_NAME 3', channelDescription: "CHANNEL_DESCRIPTION 3", importance: Importance.max, priority: Priority.high, ); var iosChannelSpecifics = IOSNotificationDetails(); var platformChannelSpecifics = NotificationDetails( android: androidChannelSpecifics, iOS: iosChannelSpecifics); await flutterLocalNotificationsPlugin.periodicallyShow( 0, 'Repeating Test Title', 'Repeating Test Body', RepeatInterval.everyMinute, platformChannelSpecifics, payload: 'Test Payload', ); } Future scheduleNotification() async { var scheduleNotificationDateTime = DateTime.now().add(Duration(seconds: 5)); var androidChannelSpecifics = AndroidNotificationDetails( 'CHANNEL_ID 1', 'CHANNEL_NAME 1', channelDescription: "CHANNEL_DESCRIPTION 1", icon: 'secondary_icon', sound: RawResourceAndroidNotificationSound('my_sound'), largeIcon: DrawableResourceAndroidBitmap('large_notf_icon'), enableLights: true, color: const Color.fromARGB(255, 255, 0, 0), ledColor: const Color.fromARGB(255, 255, 0, 0), ledOnMs: 1000, ledOffMs: 500, importance: Importance.max, priority: Priority.high, playSound: true, timeoutAfter: 5000, styleInformation: DefaultStyleInformation(true, true), ); var iosChannelSpecifics = IOSNotificationDetails( sound: 'my_sound.aiff', ); var platformChannelSpecifics = NotificationDetails( android: androidChannelSpecifics, iOS: iosChannelSpecifics, ); // ignore: deprecated_member_use await flutterLocalNotificationsPlugin.schedule( 0, 'Test Title', 'Test Body', scheduleNotificationDateTime, platformChannelSpecifics, payload: 'Test Payload', ); } Future showNotificationWithAttachment() async { var attachmentPicturePath = await _downloadAndSaveFile( 'https://via.placeholder.com/800x200', 'attachment_img.jpg'); var iOSPlatformSpecifics = IOSNotificationDetails( attachments: [IOSNotificationAttachment(attachmentPicturePath)], ); var bigPictureStyleInformation = BigPictureStyleInformation( FilePathAndroidBitmap(attachmentPicturePath), contentTitle: 'Attached Image', htmlFormatContentTitle: true, summaryText: 'Test Image', htmlFormatSummaryText: true, ); var androidChannelSpecifics = AndroidNotificationDetails( 'CHANNEL ID 2', 'CHANNEL NAME 2', channelDescription: 'CHANNEL DESCRIPTION 2', importance: Importance.high, priority: Priority.high, styleInformation: bigPictureStyleInformation, ); var notificationDetails = NotificationDetails( android: androidChannelSpecifics, iOS: iOSPlatformSpecifics); await flutterLocalNotificationsPlugin.show( 0, 'Title with attachment', 'Body with Attachment', notificationDetails, ); } _downloadAndSaveFile(String url, String fileName) async { var directory = await getApplicationDocumentsDirectory(); var filePath = '${directory.path}/$fileName'; var response = await http.get(Uri.parse(url)); var file = File(filePath); await file.writeAsBytes(response.bodyBytes); return filePath; } Future getPendingNotificationCount() async { List p = await flutterLocalNotificationsPlugin.pendingNotificationRequests(); return p.length; } Future cancelNotification() async { await flutterLocalNotificationsPlugin.cancel(0); } Future cancelAllNotification() async { await flutterLocalNotificationsPlugin.cancelAll(); } } NotificationPlugin notificationPlugin = NotificationPlugin._(); 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, }); }