Awesome Notifications
package: awesome_notifications
Android Setup
android/app/build.gradle
android {
compileSdkVersion 34
defaultConfig {
minSdkVersion 23
targetSdkVersion 34
...
}
...
}
android/app/src/main/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application>
...
<activity
android:name=".MainActivity"
android:exported="true"
...>
...
</activity>
...
</application>
</manifest>
iOS Setup
ios/Runner/AppDelegate.swift
import Flutter
import UIKit
import awesome_notifications
import shared_preferences_ios
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
// This function registers the desired plugins to be used within a notification background action
SwiftAwesomeNotificationsPlugin.setPluginRegistrantCallback { registry in
SwiftAwesomeNotificationsPlugin.register(
with: registry.registrar(forPlugin: "io.flutter.plugins.awesomenotifications.AwesomeNotificationsPlugin")!)
FLTSharedPreferencesPlugin.register(
with: registry.registrar(forPlugin: "io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin")!)
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
ios/Podfile
# Uncomment the next line to define a global platform for your project
# platform :ios, '11.0'
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings target
end
################ Awesome Notifications pod modification 1 ###################
awesome_pod_file = File.expand_path(File.join('plugins', 'awesome_notifications', 'ios', 'Scripts', 'AwesomePodFile'), '.symlinks')
require awesome_pod_file
update_awesome_pod_build_settings(installer)
################ Awesome Notifications pod modification 1 ###################
end
################ Awesome Notifications pod modification 2 ###################
awesome_pod_file = File.expand_path(File.join('plugins', 'awesome_notifications', 'ios', 'Scripts', 'AwesomePodFile'), '.symlinks')
require awesome_pod_file
update_awesome_main_target_settings('Runner', File.dirname(File.realpath(__FILE__)), flutter_root)
################ Awesome Notifications pod modification 2 ###################
Sample Code
config/awesome_notif.dart
import 'package:flutter/material.dart';
import 'package:awesome_notifications/awesome_notifications.dart';
import '../main.dart';
void _handleReceivedAction(ReceivedAction receivedAction) {
final payload = receivedAction.payload;
if (payload == null) return;
if (payload.containsKey('navigate_to')) {
navigatorKey.currentState?.pushNamed(
payload['navigate_to']!,
arguments: payload['arguments'],
);
}
}
class AwesomeNotif {
static final _notifPlugin = AwesomeNotifications();
static Future<void> init() async {
_notifPlugin.isNotificationAllowed().then((isAllowed) {
if (!isAllowed) {
// This is just a basic example. For real apps, you must show some
// friendly dialog box before call the request method.
// This is very important to not harm the user experience
AwesomeNotifications().requestPermissionToSendNotifications();
}
});
await _notifPlugin.initialize(
// set the icon to null if you want to use the default app icon
'resource://drawable/ic_notif',
[
NotificationChannel(
channelGroupKey: 'basic_channel_group',
channelKey: 'basic_channel',
channelName: 'Basic notifications',
channelDescription: 'Notification channel for basic tests',
defaultColor: Color(0xFF9D50DD),
ledColor: Colors.white,
)
],
// Channel groups are only visual and are not required
channelGroups: [
NotificationChannelGroup(
channelGroupKey: 'basic_channel_group',
channelGroupName: 'Basic group',
)
],
debug: true,
);
}
static initialMessage() async {
await _notifPlugin.getInitialNotificationAction();
}
static setListener() async {
await _notifPlugin.setListeners(
onActionReceivedMethod: NotificationController.onActionReceivedMethod,
onDismissActionReceivedMethod:
NotificationController.onDismissActionReceivedMethod,
onNotificationCreatedMethod:
NotificationController.onNotificationCreatedMethod,
onNotificationDisplayedMethod:
NotificationController.onNotificationDisplayedMethod,
);
}
static showNotif({
int id = 0,
String channelKey = 'basic_channel',
String? title,
String? body,
Map<String, String?>? payload,
}) {
_notifPlugin.createNotification(
content: NotificationContent(
id: id,
channelKey: channelKey,
title: title,
body: body,
payload: payload,
),
);
}
static cancelNotif() async {
await _notifPlugin.cancelAll();
}
}
class NotificationController {
/// Use this method to detect when a new notification or a schedule is created
("vm:entry-point")
static Future<void> onNotificationCreatedMethod(
ReceivedNotification receivedNotification) async {
// Your code goes here
}
/// Use this method to detect every time that a new notification is displayed
("vm:entry-point")
static Future<void> onNotificationDisplayedMethod(
ReceivedNotification receivedNotification) async {
// Your code goes here
}
/// Use this method to detect if the user dismissed a notification
("vm:entry-point")
static Future<void> onDismissActionReceivedMethod(
ReceivedAction receivedAction) async {
// Your code goes here
}
/// Use this method to detect when the user taps on a notification or action button
("vm:entry-point")
static Future<void> onActionReceivedMethod(
ReceivedAction receivedAction) async {
_handleReceivedAction(receivedAction);
}
}